Saturday 9 March 2013

How to Write a Twitter Bot in 5 Minutes

Meet @DearAssistant, a Twitter bot that is like a mini version of Siri. You can tweet your questions in plan English and the bot will reply with an answer.
The Twitter bot is internally using Wolfram Alpha so there’s a whole range of questions that it can 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)
  • Who directed the film M (link)
  • What is the price of Kindle Paperwhite.
Twitter Bot
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.

Writing a Twitter Bot – The Basic Ingredients

Writing a Twitter bot is surprisingly simple and you can get one up and running in 5 minutes. It helps if you know little bit of coding (simple JavaScript) but that’s certainly not a requirement for writing a basic 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

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. Go to developer.wolfram.com, create an account and then choose “Get an App ID” to create your new app.
  3. 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.

Step C: Host the Twitter bot on Google Drive

  1. While you are signed-in to your Gmail account, go to script.google.com and then choose File -> Make a copy to duplicate the Twitter bot into your Google Drive.
  2. 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.
  3. Go to Run -> Start to initialize the Twitter bot. Say yes if the script requires you to authorize access to certain Google Script services.
  4. 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.

5 comments:

  1. I νisited various blogs but the audіo fеaturе for auԁio
    songѕ curгent аt this site is in fact еxcellent.


    Feel free to suгf to my site: treatment for hemorrhoids

    ReplyDelete
  2. Attractіve ρortion of content. I just stumbled upon your site
    аnd in accession capital to claіm that I асquire actually loved account yοur weblog
    posts. Anу way I will be subѕcribing to your fеeds and even I fulfillment you access ρersistently
    quickly.

    Also visit my webρagе; Hemroids-Piles.Com

    ReplyDelete
  3. Hеllo, Neat post. Theге's a problem with your web site in web explorer, may check this? IE still is the marketplace chief and a large element of people will pass over your excellent writing due to this problem.

    Feel free to surf to my homepage; kate dircksen

    ReplyDelete
  4. Good ԁay! Wοuld you mind if I share уour blоg with my facebook grouρ?
    There's a lot of people that I think would really enjoy your content. Please let me know. Many thanks

    Feel free to surf to my web blog ... alopezie

    ReplyDelete
  5. What's Happening i am new to this, I stumbled upon this I'νе fοund ӏt absolutеly useful and it has helped me out loads.

    I am hoping to give a contributiοn & hеlp dіffeгent usегs like іts
    aіded me. Good ϳob.

    Feel fгeе to visіt my
    weblog: die Abnehm Lösung - http://c-university.com/blogs/226740/1688084/excess-fat-loss-program-who-can -

    ReplyDelete