Nodejs Twitter bot
Node.js provides multiple features and built-in library for making all bots and other functionals more easy. Now bots are the current trend and each bot has its own specific usage for user specific requirement. Now we are going to build a Twitter bot in Nodejs
Create a new directory anywhere in your system and create a new project using the following command:
Create a new directory anywhere in your system and create a new project.
If you have installed Node.js by manual build then there is a chance that crypto library is not shipped with it. You can run this command to install a crypto dependency.
npm install twit --save
create a envirnonment file .env and place your specific values,
APPLICATION_CONSUMER_KEY_HERE=XXXXXXXXXXXXXXXX,
APPLICATION_CONSUMER_SECRET_HERE=XXXXXXXXXXXXXXXX,
ACCESS_TOKEN_HERE=XXXXXXXXXXXXXXXX,
ACCESS_TOKEN_SECRET_HERE=XXXXXXXXXXXXXXXX
Create a new file index.js and load the twitter config as follows:
const Twit = require(‘twit’);
const twitter = new Twit({
consumer_key: process.env.APPLICATION_CONSUMER_KEY_HERE,
consumer_secret: process.env.APPLICATION_CONSUMER_SECRET_HERE,
access_token: process.env.ACCESS_TOKEN_HERE,
access_token_secret: process.env.ACCESS_TOKEN_SECRET_HERE
});
Search Bot
Let’s write a function expression that search twitter for all tweets containing the word ‘Nojde.js’ since July 11, 2019.
twitter.get('search/tweets', { q: 'Node.js since:2011-07-19', count: 100 }, function(err, data, response) {
console.log(data)
})
List followers for account
Let’s write a function expression that get list of all users with account @twitteraccount .
twitter.get('followers/ids', { screen_name: 'twitteraccount' },function (err, data, response) {
console.log(data)
})
Retweet Bot
Let’s write a function expression that finds the latest tweets according to the tweet id passed as a parameter.
twitter.post('statuses/retweet/:id', { id: '343360866131001345' },function (err, data, response) {
console.log(data);
})
Destroy a tweet
Let’s write a function expression that delete the latest tweets according to the tweet id passed as a parameter.
twitter.post('statuses/destroy/:id', { id: '343360866131001345' }, function (err, data, response) {
console.log(data)
})
get the complete source on github.
If you find this article is helpful, it would be greatly appreciated if you could tip Ether to the address below. Thank you!
0xe8312ec868303fc3f14DeA8C63A1013608038801
For more info reach me on my telegram id @chigovera .