Make Every Week: Question Bot

Texting is a quick way to exchange information with another person:


But what about with a robot?


In that little exchange, there are a couple of hitches to solve, which I took on for this week’s #MakeEveryWeek.

(To skip ahead to the results of my experiment, text the word hello to 646-887-6253.)

One hitch in the bot conversation above is that the bot must keep track of which answer goes with which question.

Another is that the robot must keep track of this one conversation while potentially exchanging questions and answers with, say, eight thousand three hundred sixteen others at the same time.

I’ve made a some texting projects using a cool phone system called Twilio, but none involved more than a simple reply.

Basic Twilio Texting

With Twilio, you rent a phone number (for a dollar a month) and then you provide a URL Twilio should visit whenever your new number gets a text message.

I set up a number that, essentially, sends the contents of a form to a server on my web site — a lot like when you enter your shipping address on a shopping site and click “Submit.” But instead of submitting a street, city and zip, Twilio submits the text message, your phone number and some other info.

And instead of responding with “Thank you for shopping with us,” the server can respond with a reply text.

And here’s where we get stuck.

Say the reply is “Yes.” The bot gets a new text of “Yes,” but Yes to what? The only way to know it was the answer to the dog question is to keep track of each phone number we get texts from, the last question we asked of each phone number and, probably, the answers provided for each question. That could be a lot of work.

Except that Twillio makes it easy.

Please Pass the Cookies

Back when the bot on my site replied, through Twilio, with the question, “Do you like dogs?” it can also send back a little note called a “cookie.” Twilio then remembers the cookie note along with the texter’s phone number.

If Twilio sees a fresh text from the same number, it will pass along the new text message and the contents of any cookies it's remembering.

Aha! The last time we talked, the dog cookie was set to “waiting,” so we can surmise that the “Yes!” message in response to the open dog question. We can then save that answer, and ask another question:

And when the next text comes in, we can guess it was a reply to the cat question. And so on.

The cookie concept is the same one websites (and advertisers) use to keep track of when you visit — except that in this case, the cookie isn’t stored on your device. It’s kept by Twillo under your phone number. Twilio’s system holds cookie info for four hours; after that it’s like you’ve started a new conversation.

Try It Out

This is the basis for the bot I made, which you can try by texting Hello to 646-887-6253 which is (646-887-MAKE).

The code is here, but don’t peek until you give it a try! :-)

For more on how to do this, there's a great post on the Twilio site. The example there is in Python, using Flask, but I wanted to use Node instead, so I drew from the Twilio Node documentation here and here.



#MakeEveryWeek is a challenge to myself to do just that for all of 2015. The original post on the idea is here, and the running list of projects so far is here.





1 response
Hi