Meet @DearAssistant, a Twitter bot that is like a online version of Google Now, Cortana or 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 Mahatma 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, 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 case, it processes the command and sends a response as another tweet.
Create a Twitter Bot – Step by Step Guide
What do you need to write a Twitter bot? Earlier, you would need a web server to run the bot while the bot itself would be written in languages like PHP or Python. That’s complicated so we will use Google Scripts to create the Twitter bot and host it on the Google Cloud. The cost for the whole setup would be $0.
Step A: Setup a Twitter App (screenshot)
- Create a new account at Twitter that will your 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 and put any URL in the website field. Keep the callback URL field blank, agree to the terms and submit the form to create your first Twitter application.
- Once the Twitter app has been created, click the Keys and Access Tokens tab and click the Create my Access Token button. Twitter will generate the Consumer Keys and Access tokens that we will need in a later step.
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 compute answers to questions that Twitter users will be asking your Twitter bot.
- Go to developer.wolfram.com, create an account and then choose “Get an App ID” to create your new app. We will need this App ID in the Google Script.
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_ACCESS_TOKEN, TWITTER_ACCESS_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 services.
That’s it. Close the Google Scripts website 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.
If you need help in customizing your Twitter bot, or are trying to build something more complex, contact me via ctrlq.org.