Twitter bots have been in the news in recent years due to election interference, not only in the United States, but extending around the world. There are, however, good and logical reasons for creating Twitter robots. In order to see how easy it was to create a Twitter bot, for better or for worse, I decided to create my own Twitter bot. Five minutes of work and I had a bot that worked – let's see how it gets done!
The first step in creating a Twitter bot Node.js is by creating an application on the Twitter site :
Provide the required information and you will be able to create an access token and consumer information.
The next step is to download the resource twit Node.js:
wire install twit
With available twit, create an instance of Twit with the access token consumer information that was provided to you by the Twitter application website:
const Twit = require (& # 39; twit & # 39;)
const T = new Twit ({
consumer_key: & # 39; YOUR_INFO_HERE & # 39;
consumer_secret: & # 39; YOUR_INFO_HERE & # 39;
access_token: & # 39; YOUR_INFO_HERE & # 39;
access_token_secret: & # 39; YOUR_INFO_HERE & # 39;
timeout_ms: 60 * 1000,
});
Now, the action can happen. Here are some examples of basic Bot features Twitter:
// Post a tweet
T.post (
& # 39; status / update & # 39 ;,
{status: & # 39; This is an automated test! & # 39; }
(error, data, answer) => {
console.log (error, data, response);
}
)
// Retweet a given tweet
T.post ('statuses / retweet /: id', {id: 697162548957700096})
Let's think of a more practical example: use the Stream API to "like" the tweets you're talking about:
constant stream = T.stream ('statuses / filter', {track: [‘@davidwalshblog’]});
stream.on ('tweet',
tweet => {
console.log (& # 39; tweet received! & # 39; tweet)
T.post (
& # 39; statuses / retweet /: id,
{id: tweet.id},
(error, data, answer) => {
console.log (error, data, response);
}
)
}
)
Getting an operational Twitter bot requires only minimal effort, which is why it's important that services like Twitter protect its users from criminals. Bad guys aside, there are plenty of good reasons to create a Twitter bot, whether for internal analysis, promotion, or even creating your own Twitter application. Thanks to Tolga Tezel for creating amazing JavaScript resources to interact with Twitter!