Twitter bot


I’ve been wanting to make a twitter bot for some time now, and I decided to accomplish it today.

I had created a repo for this awhile back and never finihsed it. The goal was to create a twitter bot to retweet and interact with tweets related to applicable hashtags. Specifically Nissan 370z related hashtags.

Instead of building the bot from scratch, I came across the article: Why you should have your own Twitter bot, and how to build one in less than 30 minutes by Scott Spence. It was exactly what I wanted to accomplish with this bot, intetacting with user’s that follow it and retweet related content based on a set of strings.

I took Scott’s .env file example and added my credentials and modified the rate at which the bot retweets and what the bot searches for to retweet.

TWITTER_CONSUMER_KEY=hd************df
TWITTER_CONSUMER_SECRET=d0************jd
TWITTER_ACCESS_TOKEN=8d************hg
TWITTER_ACCESS_TOKEN_SECRET=dl************84

QUERY_STRING=370z,nissan370z,zociety,#370z,#nissan370z,#zociety

RANDOM_REPLY=Hi @${screenName} thanks for the follow! Share a picture of your favorite 370z!

RESULT_TYPE=mixed

TWITTER_RETWEET_RATE=1
TWITTER_SEARCH_COUNT=20

I did however want to dockerize the bot for easier deployment in the future.

FROM node:8

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Bundle app source
COPY . /usr/src/app

# Install dependencies
RUN npm install

# Install forever globally (only way I've gotten it to work)
RUN npm install forever -g

CMD ["forever", "bot.js"]

But this is where I ran into a problem with the bot. When trying to start the bot I kept getting the following error message:

370z-bot_1  | Unhandled rejection Error: Twit: Params object is missing a required parameter for this request: `id`
370z-bot_1  |     at /usr/src/app/node_modules/twit/lib/helpers.js:39:13

Intially I thought that this error was related to something I did when dockerizing the app.

To narrow down where the issue was coming from I went back to the repo that I pulled from as the base and started from scratch. After adding my .env file I was getting the same error.

The next step I took was to go to the issues section of the repo and start reading through the issues and the first issue reported was the exact error that I was running into. Which can be found here: https://github.com/spences10/twitter-bot-bootstrap/issues/180

I dropped in the fix (which comprised of a simple else addition to the if statement):

if (!isReply(data.statuses[rando])) {
          retweetId = data.statuses[rando].id_str;
        } else {
          retweetId = data.statuses[rando].id_str;
        }

And then bot instantly spun up and retweeted the first 370z related tweet. Success!

I have TWITTER_RETWEET_RATE set to 1 which means that every minute the bot will try to retweet a tweet that matches one of the QUERY_STRINGS. Now, Twitter does have rate limits, if it found a result every minute it would tweet out 1440 times in a 24 hour period. 1440 tweets is still within Twitter’s limits for API usage.

You can read into twitters rate limiting for API calls here: Twitter Rate Limits

The good thing about the way the retweets work, is that if it finds a tweet that it already retweeted, it will try and retweet it again, but it won’t cause a duplicate post and won’t remove the previous retweet.

 ( •_•) ( •_•)>⌐■-■ (⌐■_■) ERRORDERP: Retweet!
370z-bot_1  | (╯︵╰,) SUCCESS: RT:  RT @Nissan: Saturday style brought to you by: #370Z
370z-bot_1  | #Nissan370Z #Zociety
370z-bot_1  | 📷: austin_spyark
370z-bot_1  | 🚗: azn_cspann https://t.co/ENQRmAHPVP RANDO ID:  76
370z-bot_1  | ʕʘ̅͜ʘ̅ʔ ERRORDERP: Retweet!
370z-bot_1  | (╯︵╰,) SUCCESS: RT:  RT @Nissan: Saturday style brought to you by: #370Z
370z-bot_1  | #Nissan370Z #Zociety
370z-bot_1  | 📷: austin_spyark
370z-bot_1  | 🚗: azn_cspann https://t.co/ENQRmAHPVP RANDO ID:  22

After letting the bot run overnight, some fine tuning is needed on the query string results. It looks like the bot is finding users with 370z in their names and retweeting random coinverstaions / replies.

Related Posts

Remote work — don’t forget what got you here.

Makefiles and Boto3

Happiness