Skip to content

Instantly share code, notes, and snippets.

@debashisbarman
Last active February 15, 2024 02:19
Show Gist options
  • Save debashisbarman/bffe0f6cd3c0fd2fe40e to your computer and use it in GitHub Desktop.
Save debashisbarman/bffe0f6cd3c0fd2fe40e to your computer and use it in GitHub Desktop.
A boilerplate Twitter bot that can retweet in response to the tweets matching particluar keyword (https://goo.gl/4whEIt)

Creating a Twitter bot with Node.js

Learn how you can create your own Twitter bot using Node.js and the new Twitter API. The bot will auto retweet in response to tweets with some particular hashtags. (https://goo.gl/4whEIt)

Tools we need

Here are the tools we’ll be using to create the bot — 

  • Node.js installed in your machine
  • A registered Twitter account

Create a Twitter application

  1. Create a new account at Twitter that will become your bot. Then go to apps.twitter.com, sign-in with your new Twitter account and create a Twitter application. Give your application 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.
  2. Once the Twitter application 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.

Initialize the Twitter bot

  1. Click here to get the source code and save it to your machine. Make sure the source code include package.json and bot.js file.
  2. In the bot.js file edit the values of TWITTER_CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN and ACCESS_SECRET — you know them all from the previous steps.
  3. Set the TWITTER_SEARCH_PHRASE and all matching tweets will get processed by the Twitter bot, one at a time.

Test the bot

  1. Open your terminal and go to the directory where you have saved the source files — both package.json and bot.js file.
  2. Type npm install in the terminal and required packages will be installed on your system.
  3. Type npm start or node bot.js to start the bot and if you get the message The bot is happily running… in the terminal, that’s good. The bot is listening for tweets coming in. You can even hop to your Twitter bot account to check whether the bot is working or not.

Author

Written by Debashis Barman

License

Licensed under http://creativecommons.org/licenses/by-sa/3.0

Bug Report

Report error and bugs to [email protected]

/*!
* Bot.js : A Twitter bot that can retweet in response to the tweets matching particluar keyword
* Version 1.0.0
* Created by Debashis Barman (http://www.debashisbarman.in)
* License : http://creativecommons.org/licenses/by-sa/3.0
*/
/* Configure the Twitter API */
var TWITTER_CONSUMER_KEY = '';
var TWITTER_CONSUMER_SECRET = '';
var TWITTER_ACCESS_TOKEN = '';
var TWITTER_ACCESS_TOKEN_SECRET = '';
/* Set Twitter search phrase */
var TWITTER_SEARCH_PHRASE = '#technology OR #design';
var Twit = require('twit');
var Bot = new Twit({
consumer_key: TWITTER_CONSUMER_KEY,
consumer_secret: TWITTER_CONSUMER_SECRET,
access_token: TWITTER_ACCESS_TOKEN,
access_token_secret: TWITTER_ACCESS_TOKEN_SECRET
});
console.log('The bot is running...');
/* BotInit() : To initiate the bot */
function BotInit() {
Bot.post('statuses/retweet/:id', { id: '669520341815836672' }, BotInitiated);
function BotInitiated (error, data, response) {
if (error) {
console.log('Bot could not be initiated, : ' + error);
}
else {
console.log('Bot initiated : 669520341815836672');
}
}
BotRetweet();
}
/* BotRetweet() : To retweet the matching recent tweet */
function BotRetweet() {
var query = {
q: TWITTER_SEARCH_PHRASE,
result_type: "recent"
}
Bot.get('search/tweets', query, BotGotLatestTweet);
function BotGotLatestTweet (error, data, response) {
if (error) {
console.log('Bot could not find latest tweet, : ' + error);
}
else {
var id = {
id : data.statuses[0].id_str
}
Bot.post('statuses/retweet/:id', id, BotRetweeted);
function BotRetweeted(error, response) {
if (error) {
console.log('Bot could not retweet, : ' + error);
}
else {
console.log('Bot retweeted : ' + id.id);
}
}
}
}
}
/* Set an interval of 30 minutes (in microsecondes) */
setInterval(BotRetweet, 30*60*1000);
/* Initiate the Bot */
BotInit();
{
"name": "Bot",
"version": "1.0.0",
"description": "A boilerplate Twitter bot that can retweet in response to the tweets matching some particluar keyword(s)",
"main": "bot.js",
"dependencies": {
"twit": "^2.1.1"
},
"devDependencies": {},
"scripts": {
"start": "node bot.js",
"test": "node bot.js"
},
"repository": {
"type": "",
"url": ""
},
"keywords": [
"node",
"js",
"twitter",
"bot",
"debashis",
"barman"
],
"author": "Debashis Barman",
"license": "http://creativecommons.org/licenses/by-sa/3.0",
"bugs": {
"url": "https://gist.github.com/debashisbarman/bffe0f6cd3c0fd2fe40e#file-readme-md"
},
"homepage": "https://goo.gl/4whEIt"
}
@vedipen
Copy link

vedipen commented Jan 31, 2017

@santosh It is possible that your bot didn't find any tweets with your hashtags. It is always wise to check if the parent of id_str is not undefined.

if (error) {
	console.log('Bot could not find latest tweet, : ' + error);
}
else if (data && data.statuses[0]) {
	 var id = {
		id : data.statuses[0].id_str
         }
         ...
         ...
else {
        console.log('Bot could not find latest tweet');
}

Copy link

ghost commented Apr 18, 2017

@santosh this is because the code author automatically retweets his guide ;) clever, not?

@oscarcornejo
Copy link

Friend, by query, where this ID comes from: {id: '669520341815836672'}
Thanks!

@PingOfD32th
Copy link

how can i get the bot to just tweet. not retweet. i want it to read my updates and tweet for me.....

@zemelax
Copy link

zemelax commented Nov 1, 2017

this bot can reetwit of my subscribers???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment