First steps to a chatterbot with AIML


Git-Repo with ALICE aiml and minimal python3 aiml bot (not exactly the code from this older post, but very similar) .

Who isn’t impressed by all these real cool computers in the sciene fictions movies which can be controlled by voice commands, and which talk back. So am I and I thought about how cool it would be if this would be possible today. Totally aware that this is way out of my skill range I thought about an easier solution to make the computer actually talk to me. Remembering an article about chatterbots I searched a bit the net and found AIML ( ) the “Artificial Intelligence Markup Language”. It’s a xml dialect for creating chatterbot databases. And luckily some programmers already put together some tools. I decided to use “PyAIML” because it seems to be really easy to use. But I can still hear no talking, you think? Just wait a bit, here it comes: espeak. It’s a speech synthesizer which reads out text you pass to it. So let’s get started!

Getting the tools


I have ubuntu linux and used apt-get to install some tools, it surely works almost the same on other systems with their package systems.

apt-get install espeak espeak-data


Download it here and extract to a directory

Some Basic AIML files

Just download and extract as well. I started with the “Standard” AIML file.

A little chatterbot

The PyAIML Page gives pretty clear instructions how to create your own chatbot. Here is mine:

import aiml
import commands
k = aiml.Kernel()
# load the aiml file
# set a constant
k.setBotPredicate("name", "Chatty")
while True:
    input = raw_input("> ")
    response = k.respond(input)
    # print out on the shell
    print response
    # and as speech
    print commands.getoutput("/usr/bin/espeak -v en+f4 -p 99 -s 160 \"" + response + "\"")

and here the firsttry.aiml

<?xml version="1.0" encoding="utf-8"?>
<aiml version="1.0.1" xmlns=""
	<template>Hello,   I'm glad to see you</template>
        <pattern>WHAT IS YOUR NAME</pattern>
        <template>My name is <bot name="name"></bot></template>

Invoking the bot

Now you just have to start the program


And it already gives some answers.

Loading jgk.aiml... done (0.02 seconds)
> Hello
Hello, I'm glad to see you

Of course you have to add additional patterns to really let the bot talk a bit. The example files (look in the upper section of this tutorial) are a good start.

pixelstats trackingpixel

Comments (9)

GibbySeptember 19th, 2011 at %I:%M %p

How to exit a conversation in pyaiml or end the program

adminSeptember 19th, 2011 at %I:%M %p

You can easily add a quit-functionality by checking about some keyword in the while loop. I used “quit” for example. you have to import sys, for the exit function:

import sys   # add this to the top of the program
while True:
    input = raw_input("> ")
# here comes the new code
    if input == "quit":
# end of new code
    response = k.respond(input)
    # print out on the shell
    print response
    # and as speech
    print commands.getoutput("/usr/bin/espeak -v en+f4 -p 99 -s 160 "" + response + """)
jessMarch 15th, 2012 at %I:%M %p

is there any way of delaying the responses of the bot? some seconds or even 1 minute

adminApril 20th, 2012 at %I:%M %p

It should be very easy. Just add a sleep command between the input and the output of the bot (response = …) . to make it more “human like” you could add a random time and make it wait longer if the input text was also long.


Suspend execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

Abhimanyu BhartiSeptember 15th, 2015 at %I:%M %p

Some of my pattern in aiml work but some are not working..plz help

JonasSeptember 15th, 2015 at %I:%M %p

Hi Abhimanyu,

I only played around a bit with AIML some years ago, so I don’t have too much experience, also without the file I think it is difficult to make assumptions about what is going wrong.

Maybe these links help you:

The best would be, if you add some debug option into the bot if possible, or if that is no option you can try the first link which offers some simple debugging using a modified AIML file.

I would be happy if you share your experience, when you solved your problem.


Influx SwaggaOctober 1st, 2015 at %I:%M %p

hello, i am trying to create bot that would compare two patterns and print out the percentage match using AIML, kindly tell me how do do that…

RaghuMarch 13th, 2017 at %I:%M %p

after running python im getting nothing please help me

JonasMarch 16th, 2017 at %I:%M %p

difficult without any code or else, please provide more information

Leave a comment

Your comment

Time limit is exhausted. Please reload the CAPTCHA.