loading...

Creation Of Twitter Bot — Retweets & Searches

jacobmgevans profile image Jacob Evans ・3 min read

The pains and learning from using the Twitter API to create a Twitter Bot

So I first started off those this “simple” side project as something connected to one of my duties at the startup I work at; managing the social media for the company. I figured hey I am a developer and this couldn’t be that hard to automate. Well, I was sort of wrong and sort of right, which pretty much sums up most of my coding experiences.

I decided to start off this project with some simple research to answer some question, is there a library that could help with interacting with Twitter API? Are there any big roadblocks? Do any examples exist that do something similar? Should I take this opportunity to try technologies I haven’t used? Now I answered these questions with a little Googling and except for the last one which I decided yes, I will use Koa a framework for Node.js that is similar to Express in some ways and vastly different in others.

Issues figured out...Nope.

So I thought I had answered the other questions, a lot of people seemed to be using Twit an NPM package that helps interface with the API a little easier. Maybe I should have just dealt with the API directly… I learned some hard lessons with Twits finicky initialization not able to take in variables from .dotenv believe me I tried. I will discuss how I worked around this later. The question of whether other similar things exist, yes and no lots of people have made bots not quite doing what I was looking to do. The roadblocks came mainly from Twitter API, some from Twit and some from my own bad coding.

  • You need to create a brand new account for the bot → The bot wants to be autonomous.

  • You need to apply for access to the Twitter API, they will ask lots of questions. I even got emailed for more thorough answers. Here is the website to get the ball rolling at Twitter Developer Platform: https://developer.twitter.com/

  • Then you will be able to create an “App” it will be in the drop-down from the profile name Dashboard → ProfileName → ProfilePic. There are better examples with pictures out there if you get lost.

  • Once you have done that you can add the App to Dev environments and go to the Apps settings to generate API keys.

Bot Code

The whole code For Now :)
This is where the fun begins, you actually get to code. My code currently only finds statuses/tweets, I plan on passing them dynamically to be retweeted and liked.

The entire code here will at the least give you id for all 5 of the statuses/tweets that are received. I then create an array from of them, I might change that to be an array which is turned into a Set().

At some point, once I have completed the rest of the bot's functionality I will also break down the different parts of the code and API, to give a more detailed description of each section.

Shortly after writing this article I was able to accomplish the retweet within the search. I still need to refine the tweets that are found and retweeted. However, the bot is now capable of finding and RTing…Soon liking them as well. The goal is to have the Bot on a schedule, like a Cron Job or something, of some kind that triggers these events periodically throughout the day.

Here is the bot as it is currently:

Retweet Bot portion completed

Posted on by:

jacobmgevans profile

Jacob Evans

@jacobmgevans

Associate Software Engineer @ JP Morgan & Chase | Air Force Veteran | Hardware Enthusiast | Outdoorsman | INTJ | Create React App contributor

Discussion

markdown guide