Building a twitter bot with Node

Building a twitter bot with Node

Creating a Twitter bot was something on my to-build list and I definitely didn't start building it because I thought Twitter was done(credits to Sir Elon Musk). So let's walk through how I built this Twitter bot.

About Project

The project was creating a bot that would help you to give contact cards to folks on Twitter. You simply have to tag the bot @GiveContactCard on Twitter and the bot would reply to that tweet with your contact card.

The card can also be customized through a web application. There would also be a custom template which would retrieve the author's details and create a template for them.

Disclaimer: If you're thinking of using the bot I would recommend customizing the contact card as the template-generated contact card is not that eye-pleasing.

Twitter Bot

This is the High-Level architecture of different components of the bot. A database is not something that is required for all the bots but for this use case, there is a need for a Database.

Now let's look into what happens inside the script. The script does the following tasks.

  • Authentication: For requesting the Twitter server you will need some kind of authentication which will be done using keys and secrets that will be provided to you while you're creating your app in the Twitter Developer Platform.

  • Getting Mentions: The bot needs to retrieve all the mentions (mentions means the tweets where the bot has been tagged). After getting the mentions the bot iterates over the mentions and replies to them with the contact card.

    Now can you identify what problem will the bot face while getting the mentions?

    As you can see when we fetch mentions it will retrieve all the mentions due to which it will also reply to the already replied tweet.

    So we store the id of the last tweet and only reply to the next tweet from the last one. Fortunately, the Twitter API also takes a param named since_id which fetches tweets from the given since_id

    So for this, I created a Redis database which will store the last id in key-value pair.

    redis database schema

  • Database lookup/retrieval: While replying to the tweet we also need to check if there exists a custom contact card for the author's Twitter id in our database. If there is a custom contact card we will retrieve and reply with the custom card otherwise we will create a create contact card from the standard template for the author.

  • Image creation: There are two ways we are creating images, let's look at them:

    • The first one is with a standard template where we get the author's information and create an image with the author's profile photo and information embedded in it.

    • The second way of retrieving information is from the database. We store the image in base64 format which is a string and after that, we convert it into an actual image.

      After you have created the image you need to upload this image to the Twitter server and you will get a "mediaId" which can be accessed later for adding an image to the tweet replies.

These are the main tasks done by the script, now let's look at the overall flow of the script.

Web Application

This web application requires a Twitter social login and has a '/customize' page which consists of a form for updating/adding a custom contact card.

'/customize' page:

The image is stored in base64 format which is then stored in the database. Image transfer can be optimized.

Currently, I am directly transferring the whole base64string, I tried using lzstring algorithm to compress the image but the results were not that impressive let me know if you know a better way of doing this.

Note: The database is shared between this application and the bot.

Technologies

  • T3 stack: Next application with typescript, tRpc and tailwind for creating the web application.

  • Prisma: ORM for Node that is used for interacting with the Postgres database.

  • Twitter-api-v2: Node library for interacting with the Twitter APIs

  • Jimp: Node library used for creating custom images.

  • Redis: Key-value database for storing the last visited tweet id.

Conclusion

This was a fun project to build and got to learn a lot of things about the Twitter APIs. Let me know what you think about this app and how can this be improved.

You can connect with me on Twitter, LinkedIn and Instagram.