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, on Facebook, and probably lots of other places. The ability of chatbots to engage makes them powerful tools – you can even use one for your resume!
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
- Select a Blank theme and create a new bot (there are lots of possible themes to browse, but we’re starting simple).
- Rename your bot (top left) and in the big window, delete all the default code – we’re starting from scratch!
- On the first line, enter “+ hi” (don’t use quotation marks, and don’t forget the space)
- Hit return, and on line 2 enter “– Hello There!” (again, no quotes and remember the space)
- On the right, click the big friendly green button that says “Publish Current Topic”
- 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.
- In your code, hit return twice (to leave a blank line) then add a new trigger: “+ *”
- Below that, add an error response such as “– Sorry, I don’t understand that command.“
- 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.
- Click the green Publish Topic button when your bot is ready
- At the top of the screen, click “Deploy” (the paper airplane icon)
- You’ll get a window that says “Embed Code” with a block of code under it. Select that code and copy it.
- In a new tab, go to pste.edu
- Paste your code in the box and click submit
- Go to the link you get and click the chat bubble in the bottom right to activate your bot!
- 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.