Twitter bots have been in the news in recent years due to election fatigue, not only in the United States, but also by stretching out over the world. However, there are good and logical reasons for making Twitter bots. To see how easy it was to make a Twitter bot, for good or bad, I decided to make my own Twitter bot. Five minutes of work and I had a working bone – let's see how it's done!
The first step in creating a Node.js Twitter bot is to create an app on the Twitter website:
Provide the required information and you can create access token and consumer information.
The next step is to download the twit Node.js source:
yarn install twit
Make Twit available with a copy of Twit with the access token-consumer information you received from the Twitter-app-website:
const Twit = require (& # 39; twit & # 39;) const T = new Twit ();
Now the action can happen. Here are a few examples of the basic functionality of the Twitter bot:
// Post a tweet T.post ( & # 39; Statuses / update & # 39 ;, , (err, data, response) => ) // Retweet a certain tweet T.post (& # 39; statuses / retweet /: id & # 39 ;, )
Let's think of a more practical example: using the Stream API to "like" every tweet you mention:
const stream = T.stream (& # 39; statuses / filter & # 39 ;, ); stream.on (& # 39; tweet & # 39 ;, tweet => );