Twitter Bot — Day 2 — #100DaysOfCode

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:

It was exactly what I wanted to accomplish with this bot, interacting 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.

QUERY_STRING=370z,nissan370z,zociety,#370z,#nissan370z,#zocietyRANDOM_REPLY=Hi @${screenName} thanks for the follow! Share a picture of your favorite 370z!RESULT_TYPE=mixedTWITTER_RETWEET_RATE=1

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:

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:

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 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 RANDO ID: 22

This is the end result!

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 conversations / replies.

This article was cross posted on my personal blog:

Wanna-be Pizzaiolo. Productivity Nerd. @todoist Enlightened. Photographer. Auto enthusiast. Summoner-of-gifs. #ROC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store