How I Created a Twitterbot to Help Bring More Awareness to Depression

A Twitterbot in Node.js

Emmilie Estabillo
Chingu
3 min readJun 10, 2018

--

Photo by Chris Barbalis on Unsplash

This week, many mourned the loss of two beloved icons, who, by all public appearances, seemed to have it all. It is further proof that no one is immune from feelings of hopelessness and inadequacy. I can personally relate to the struggle, and to show my support in bringing more awareness to the #depressionisreal campaign, I created my first Twitterbot: @UreNotAlone0608. Its basic function is to search for tweets which contain keywords I defined in the params and subsequently retweets it.

I found inspiration in setting up the bot through this tutorial and this other article. They include instructions on how to configure Node, secret keys, and later deploying to Heroku. Basically, the bot uses a Node module called twit which interacts with Twitter API giving it REST endpoints for queries and retweets. The following code defines the parameters:

let retweet = function() {
let params = {
q: 'katespade OR anthonybourdain OR depression',
result_type: 'mixed',
lang: 'en'
}

For the keywords, I used ‘depression’ and the two high-profile names, believing the latter will continue to be mentioned in tweets in bringing increased attention to the mental health crisis. result_type: ‘mixed’ will return a combination of popular and recent tweets.

I limited the query to 4 matching tweets every time the bot runs. Because of the mixed result type, I sometimes get an error saying message: ‘You have already retweeted this Tweet. Presumably, that particular tweet was a popular type that it had already encountered earlier. The app will then skip retweeting it and I won’t get exactly 4 retweets on a single run. To conclude, I added the setInterval method to serve as a timer to run the app once every hour (3600000ms).

Here is the completed source code:

const twit = require('twit');const config = {
consumer_key: process.env.consumer_key,
consumer_secret: process.env.consumer_secret,
access_token: process.env.access_token,
access_token_secret: process.env.access_token_secret
}
const Twitter = new twit(config);let retweet = function() {
let params = {
q: 'katespade OR anthonybourdain OR depression',
result_type: 'mixed',
lang: 'en'
}
Twitter.get('search/tweets', params, function(err,data) {
if (!err) {
//if there is no error, return all 4 tweets and retweet
for (let i = 0; i < 4; i++) {
let rtId = data.statuses[i].id_str;
Twitter.post('statuses/retweet/:id', {
id: rtId
}, function(err,response) {
if (response) {
console.log('Successfully retweeted');
}
if (err) {
console.log(err);
}
});
}
}
else {
console.log('Could not search tweets');
}
});
}
retweet();
setInterval(retweet, 3600000); //run the bot every 60min

My twitterbot’s been doing great so far and has even garnered a few followers. If you’re wondering about the debate regarding depression or just generally curious about the bot, check it out on Twitter.

--

--

Emmilie Estabillo
Chingu
Writer for

frontend developer | UI/UX enthusiast | tooth fairy | always curious 🇵🇭🇺🇸 emestabillo.com