My Blog

My learnings and etc.

A Boilerplate Nodejs Twitter Bot That Responds to Twitter Mentions.

Start by forking this github repo

Config:

Make a copy of config.template.json named config.json, and fill your secret keys of your twitter bot that you obtain from here and make sure your twitter app has both read and write access in the “permissions” tab.

Installation

Install all the node dependencies.

1
> npm install

Your bot code.

The bot is written in coffeescript, and the compiled javascript is also provided in case if you prefer that.

At the least you need to fill the function solve whose only argument is the tweet text, include all the mentions. Not the Tweet Object, its just the tweet text.

You are also provided with a function isOfWrongFormat that defaults to false which checks the validity of the tweet text. And its only argument is the tweet text as well. Not the tweet object.

Running the bot.

If you wrote the bot in coffee-script do this

1
> coffee twitterBot.coffee

Or if you wrote it in plain javascript do this

1
> node twitterBot.js

If you want to compile your coffee-script to plain javascript you can run the coffee command with a -c flag

1
> coffee -c twitterBot.coffee

Sample Bot

You can find a sample twitter bot that I wrote based on this, albeit slightly modified is countdownbot

It responds with a solution of the numbers game from the game show Countdown, if someone mentions the bot along with the target number and the rest of the numbers..

After you fill the config in the countdownbot as shown above, and ran it, Anyone mentioning your bot along with a set of numbers, with the first one being the target number

@someRandomPerson: @countdownbot 347 2 3 10 75 23 12

it responds like this.

@countdownbot: @someRandomPerson One possible solution for 347 is: (10*(12+23))-3