How to Write a Simple Chatbot (part I)

We’ll be designing basic chatbots in the next few weeks. You learned about these in this week’s readings, so you know you’ve already interacted with a chatbot on your phone, in your home, and probably lots of other places. The bots we’re going to make will be simple, rule-based call-and-response setups, but they’ll be enough to put together a simple explainer for your personal or group blog. Let’s get started!

FIRST: Create an account on rundexter.com (Dexter is a free platform for building chatbots) and log in

First contact

  1. Select a Blank theme and create a new bot (there are lots of possible themes to browse, but we’re starting simple).
  2. Rename your bot (top left) and in the big window, delete all the default code – we’re starting from scratch!
  3. On the first line, enter “+ hi” (don’t use quotation marks, and don’t forget the space)
  4. Hit return, and on line 2 enter “– Hello There!” (again, no quotes and remember the space)
  5. On the right, click the big friendly green button that says “Publish Current Topic”
  6. There’s a phone-shaped window on the right with an entry blank at the bottom. Type in “Hi” – your chatbot should respond “Hello There!

What did you just do? You created a trigger (the + part) and told the chatbot how to react (the part) when it encountered that trigger. It’s a call-and-response, like how when you say “Let’s Go!” people respond “Mountaineers!” This is a rule-based chatbot: It doesn’t really use artificial intelligence to “think,” it just reacts to preprogrammed stimuli.

Now try the following:

  • Tell your chatbot both “hi” and “Hi” and “hi!” – what happens?
  • Tell your chatbot “hello” – what happens? Why?

Does my chatbot hate me?

If “hi” didn’t get you a response, there’s two likely reasons. The code for your chatbot triggers (the code after +) cannot contain either of the following:

  • Capital letters (“+ Hi“)
  • Punctuation (“+ hi!“)
  • Make sure you include a space after the + or – in your code

Look at your code. Does the trigger contain either of those? If so, there’s your problem. Also note that the chatbot responses CAN contain caps and punctuation. Also ALSO note that you can include these when talking to your chatbot, so either “hi” or “Hi” or “”HI!!!!!!!” will work just fine. “Hello” or “Hi there” won’t, however, because right now your chatbot is only coded to respond to versions of “hi”.

Sorry, didn’t get that

As you’ve just seen, chances are users are going to say things to your bot that it’s not prepared for. It’s good practice to have a catch-all response for this.

  1. In your code, hit return twice (to leave a blank line) then add a new trigger: “+ *”
  2. Below that, add an error response such as “– Sorry, I don’t understand that command.
  3. You’ll have to republish the bot (green button) before you can test out the new code

That asterisk (*) trigger is a wildcard, meaning it stands in for any trigger not specified in your code. Right now, that means anything other than a variation on “hi” will return the catch-all message.

There are other uses for the wildcard operator too:

  • Add it in brackets before or after a trigger to allow additional words – so “+ hi [*]” would recognize both “hi” and “hi there”
  • Bots commonly also recognize requests for help by including the trigger “+ [*] help [*]” (along with a response that gives the user a list of help commands)

Publishing your bot

We’ve tested our bot in Dexter, but it’s not the best way for seeing your work in action. To try it out, we’re going to copy the code to an HTML pastebin that will show what it’d look like in Messenger or on your phone.

  1. Click the green Publish Topic button when your bot is ready
  2. At the top of the screen, click “Deploy” (the paper airplane icon)
  3. You’ll get a window that says “Embed Code” with a block of code under it. Select that code and copy it.
  4. In a new tab, go to pste.edu
  5. Paste your code in the box and click submit
  6. Go to the link you get and click the chat bubble in the bottom right to activate your bot!
  7. NOTE: In previous classes, we’ve found this often doesn’t work on the first try. If you don’t get the bubble icon (bottom right), try repasting the code and reloading, or republishing the code in rundexter and pasting that. It always works eventually, it’s just weird sometimes.

That’s it! You now have a functioning – if somewhat dull – bot. Next, try building in some additional triggers and responses. What should this bot do? How can you use it to tell a simple story? We’ll do more with this in our next session.

One Response to How to Write a Simple Chatbot (part I)

  1. […] time you learned the basics of making a rule-based chatbot that can respond to some basic triggers (hi, yes, no, maybe, what is the meaning of life). Nifty […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: