A small Telegram Bot in Go
I started using Telegram a few years ago. Most of the time I don’t use it much to have 1 to 1 conversations but rather chat in a small group of friends which I’ve known for a while now. Every now and then, we share some links to Twitter on that group, and unfortunately the Telegram official clients preview mode don’t support previewing Twitter messages with more than one image.
Take this test message as an example:
This is a test pic.twitter.com/E24MwvjOj0— Bernat Ràfales (@brafales) May 20, 2017
The moment I link this to a Telegram chat, this is the result:
Which is not ideal, as sometimes the message doesn’t make much sense when only one of the images is displayed.
Turns out I’ve been learning some Go over the last few months as well, so I wondered if I could write a small Telegram Bot to help me with that. I needed something to which I could send a Twitter link, and gave me back either the default Twitter preview in Telegram, or a custom made one with all the images of the message, in case there were more than one.
Introducing the new Bot
I started this more to learn a bit more of Go rather than to learn much about the Telegram and Twitter APIs, so I decided to use already built packages to deal with both the Telegram Bot API and the Twitter REST API. The following flow chart describes in a nutshell what the bot does:
The way the bot code is structured is through the following packages:
- configuration: holds general bot configuration options
- twitter: deals with the Twitter REST API, mostly though go-twitter
- message: builds messages that telegram-bot-api understands
- main: entry point. Initialises all the things and controls the main program flow
The main program runs as follows:
We set up a new configuration, which will hold some information that we will need throughout the rest of the program (more on how the configuration reads its values later). We then create a new Telegram Bot using the supplied bot key, and lastly we do the same with a Twitter client, supplying the API key and secret needed when setting up Application Only Auth.
After the initialisation code, we set up the bot in Webhook mode. The line
updates := bot.ListenForWebhook("/") will return a new channel of Telegram Updates (think of them as Telegram messages sent to the bot) that will be updated by an
http.Handler listening to requests made to
/. After this, we can range over the updates, passing them to the method
processUpdate, which will do all the heavy work.
Processing the messages
processUpdate method reads as follows:
It basically follows the general flow chart shown above:
- We try to get a Twitter Status ID from the message the bot has received, and return immediately with an error if we can't
- If we find a proper ID, we go and talk to Twitter to get all the information we need about the Tweet. Again, if we can't do that, we just return with an error
- We try to retrieve the Tweet Extended Entities (which is the name the Twitter API gives to the multiple images you can attach to a Tweet), returning an error if something goes wrong in the process.
- We send the message
messagepackage with the retrieved Entities. This will get us a slice of messages that are ready to be sent to Telegram through our bot.
- For each one of the messages returned by the message builder, we send the
Sendmessage to our Telegram Bot, which will ensure that the message reaches its destination.
A few of these steps are worth digging into, so let’s go and do that.
Talking to Twitter
Twitter uses OAuth to manage authentication. In our case we are writing an application that needs to access Twitter Statuses (the name Twitter gives to a Tweet). For our bot, we rely on the go-twitter package, which leverages the oauth2 Go packages to handle the authentication layer. A
We expose a
Client type and a method
NewClient that will create the internal Twitter client that go-twitter provides.
Apart from a client, we also expose our own
Tweet type, and provide a few utility methods to create them and ask useful questions. Amongst those utility methods, we have one that gets us the Extended Entities for a given Tweet, one that gives us the original Tweet URL, and one that will provide a pretty printed version of the Tweet contents for our Telegram messages:
Finally, the package also has the method that will look for Twitter links for a given text string, making use of Go regular expressions:
Building the messages for Telegram
As described at the flow chart, we have essentially 2 use cases here:
- The Tweet does not have more than one Extended Entity
- The Tweet has 2 or more Extended Entities
In case number (1) the current Telegram preview is good enough for us, so the message builder will simply send the same link again.
Case number (2) is more interesting, as we want the bot to be sending the multiple Extended Entities as inline messages, and also send the Tweet text (but disabling the web preview flag, so we don’t send duplicate information).
The Telegram Bot API allows us to do this in a relatively easy way:
Configuring our application
Last but not least, our application requires of some information to run. In our case:
- Twitter authentication information
- Telegram Bot authentication information
- Telegram Chat where we want to send our messages to
- What HTTP port should our web server listen to (needed for heroku deploys)
- What URL should Telegram send the messages sent to the bot (the webhook or callback URL)
Because I decided to deploy the bot to heroku, I went the environment variable route to get all the sensitive data the application needs. Through heroku, you can make certain values available as OS environment variables, which our application can access. This is how I made my configuration package load all the values the application needs to run properly:
Once the bot has been deployed, a message can be sent to it with a link to Twitter, and the bot will proxy the message with all the needed images to the channel I’m interested to:
Which makes a lot more sense for Tweets with multiple images.
Find all the code in the piulades-bot Github repository.