Friday, September 3, 2021

Serverless Twitter Bot using IBM Cloud

A Twitterbot at work
Some months ago, I discussed cron-like scheduling on IBM Cloud. One of my personal use cases is to send out tweets. In this post, I am going to look into details, into how I implemented a Twitter bot, deployed it to IBM Cloud Code Engine, and how it is managed.

Twitter bot

Internet bots, bots in short, are software applications that are used to perform automated tasks over the Internet. Twitter bots use the Twitter API to control account activities like sending out tweets, to like or retweet messages, or to reply to tweets.

I have developed or experimented with different types of Twitter bots. In the following, I discuss a simple bot to send out tweets based on a configured schedule like twice a day from Monday through Friday. It is written in Go and its source code is available on GitHub. There are only few lines of code which I am going to discuss.

Compose and send tweets

For my bot I am using an RSS feed as input. The feed provides a summary of recent blog articles for a blog site with metadata like the titles and post URLs. Check the feed for my blog or the IBM Cloud blog. The Twitter bot is fetching the configured blog feed, picks a random article, and composes a tweet.

Next, the Twitter client is set up based on the configured credentials. Thereafter, the composed messages is posted as Twitter status update, a.k.a. tweet.

Serverless deployment on IBM Cloud

Because the bot is only active few minutes a day, it is not worth running a server all day. Thus, serverless computing makes sense. The compute resources are shared with others and I only pay for the actual consumption, i.e., usually cents a day for an app like my bot.

I picked Code Engine on IBM Cloud for the deployment of my Twitter bot. It allows to build and run containers and supports cron subscriptions. I use a Dockerfile with two stages (multi-stage Dockerfile): The first stage to download all required code modules and build the application, the second to only copy over the executable into a small base image. My container image is stored in the IBM Cloud Container Registry. From there it is pulled for the actual deployment, to run the Twitter bot.

Configuration and scheduling

For the Twitter bot to tweet it needs credentials to access the Twitter API and it needs to know how to compose the message. I configured the Twitter credentials as app secrets and individually pass in the message configuration for each tweet as part of the cron-based invocation.

Something like the following is the JSON data stored with the cron subscription which is used when invoking the Twitter bot on schedule:

{"secret_key":"TWEET_SECRET","tweet_string1":"Check out this #blog post: %s. Read it at %s ", "tweet_string2":" #Bot on #IBMCloud #CodeEngine. #news #IBM #cloud ", "item_range":10, "feed":"https://www.ibm.com/cloud/blog/rss"}

It contains a known secret for some basic authentication, strings with text and placeholders to announce the randomly selected blog post, an item range to specify how many feed items to consider, and the actual feed URL to use.

Summary

The Twitter bot which I have described is pretty simple. The Go app is deployed as container to IBM Cloud Code Engine. The cron scheduler invokes the bot with some configuration payload. The app composes the tweet and sends it as status update to Twitter. If you want to see the bot in action, follow this account: https://twitter.com/db2bm.

Architecture: Twitter bot on IBM Cloud
Architecture: Twitter bot on IBM Cloud

If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or LinkedIn.