Meet @DearAssistant, a Twitter bot that is like a online version of Apple Siri. You can tweet your questions in plan English and the bot will reply with an answer. Here are some questions that people have asked @DearAssistant so far:
- How many calories are in Diet Coke? (link)
- When was Mahatama Gandhi born? (link)
- What is the distance between city A and city B (link)
- What is the price of Kindle Paperwhite (link)
You can also ask the bot for word meanings, weather conditions, language translation, to convert between time zones, date calculations (how many days until Christmas) and more. The Twitter bot is internally using Wolfram Alpha so there’s a whole range of questions that it can answer.
How to Write a Twitter Bot
A bot is essentially a program that is always running in the background and whenever it encounters a command (tweets in this), it processes it (based on the text of the tweeet) and sends a reply to the tweeter in another tweet.
Earlier, you would need a web server to run the bot while the bot itself would be written in languages like PHP, Perl or Python. That’s complicated so we will use Google Scripts to write the Twitter bot and host it on our Google Drive.
How to Write a Twitter Bot – Step by Step
Step A: Setup a Twitter App (screenshot)
- Create a new account at Twitter that will become the bot. Then go to dev.twitter.com, sign-in with your new Twitter account and create a Twitter app. Give your app a name, description, website (any URL) and callback URL (https://spreadsheets.google.com/macros/). Agree to the terms, fill in the CAPTCHA and submit the form to create your first Twitter application.
- Once the Twitter app has been created, click the Settings tab and choose Read and Write under Application Type. This is important since we want the bot to read tweets as well as post tweets. Click the Update button to save your changes.
- Switch to the OAuth tool tab and make note of the Consumer Key and Consumer Secret. We will need these later in our Google Apps Script.
Step B: Create a Wolfram Alpha App (screenshot)
- While the Twitter app will monitor and respond to tweets, the Wolfram App will be used to determine answers that users will be posing to your Twitter bot.
- Go to developer.wolfram.com, create an account and then choose “Get an App ID” to create your new app. [See update below]
- We will need this App ID in the Google Script. Remember that your free Wolfram Alpha App can only be used for a non-commercial purpose.
Update [3/19/2013] – If you need to create a Twitter bot with Wolfram|Alpha, you should send an email to firstname.lastname@example.org to discuss licensing. The @DearAssistant Twitter bot has permission from Wolfram to run 2,000 queries per month.
Step C: Host the Twitter bot on Google Drive
- Clear here to create a copy of the Twitter bot into your Google Drive.
- Edit the values of TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_HANDLE (your new Twitter account) and WOLFRAM_API_ID – you know them all from the previous steps.
- Go to Run -> Start to initialize the Twitter bot. Say yes if the script requires you to authorize access to certain Google Script services.
- Go to Run -> Start to actually run the Twitter bot. It will show an “Authorization Required” dialog and will redirect you to Twitter where you can grant the script access to your Twitter account.
That’s it. Close Google Scripts and your Twitter bot is now up and running, ready to take commands. As always, you are free to use, modify and distribute the source code with attribution.
Also see: How to Use Twitter as Gmail Notifier