Rust: How to create Telegram bot

Pudding Entertainment
4 min readJan 17, 2023

--

Photo by Luke Hodde on Unsplash

Telegram is so much more than your regular messaging app. It has limitless server storage, media compression, excellent privacy and by far the most powerful feature of all — free bots for all. Seriously, if Telegram isn’t your primary texting method in 2023, you are missing out!

This is the second part of a two-part tutorial where we are building a fully functional Rust binary Telegram bot that will read two API endpoints, process the responses, and send a message with an image to the given chat.

Prerequisites

I’m using Rust version 1.66.0. This tutorial assumes prior knowledge of Rust and concentrates more on practical usage of the language. Also, you should check out the first part — Rust: How to consume REST API — beforehand. We will continue working on the same project here.

As always, project sources are available at GitHub, find the link at the end of the page.

There are 3 parts in this tutorial: text message sending, image processing, and a very basic build and run process

Part 1. Text message sending

I’m assuming prior knowledge and experience with Telegram bots. But if you are completely new to this topic — check out the official FAQ.

All the required crates were added previously, thus we can go ahead and start coding. Let’s create a new module telegram_bot_sender where all the bot related logic will reside. We will start simple and only send a text message at first.

Note! You can disregard the build_message and get_price methods — they are implemented as per API response specifications and are not very relevant for the bot itself.

With that in place you should now be able to see a message in your Telegram chat:

This very simple yet powerful implementation can already be used to automate a vast number of alerts you might be dealing with on a daily basis.

Part 2. Image processing

Now that we were able to successfully send a text message to the chat, we can focus on image processing. One of the current limitations of Telegram API is that it only supports PNG and JPG as photo formats. At the same time ImmutableX API returns a SVG image via the image_url field and it won’t be accepted. Thus we should transform it to one of the supported formats.

Luckily, there is a crate that can help us with it — resvg. With its help we can easily create a PNG file out of a SVG and send it to the chat:

A few notes about the implementation. Telegram send_photo API call requires an image file and there are various ways to feed one to it. In this case, since resvg is only capable of creating a new PNG file (and not reading it into the memory), a temporary image is created. That image is then sent to the chat and subsequently deleted. On top of that, in case of an error during image generation the application will fallback to the text message created in the previous section. Lastly, you might be wondering why the USER_AGENT const was added. The reason is rather straightforward — the given SVG is served from AWS CloudFront and it is rejecting the call with 403 forbidden status code without the header.

Part 3. Build and run process

Our application is now production ready! In order to build the release binary we will use the following cargo command:
cargo build --release

You can find the built binary under the target folder:

Assuming that you are running one of the Linux distributions on your server, we will use crontab to run the binary on regular intervals.
Execute crontab -e and add */1 * * * * /illuvium-land-runner >> /illuvium-land-runner.log 2>&1 — that will make it run every minute.
As you can see we are redirecting the output to the log file, take a look there, it should contain the expected log lines:

Afterwards

Telegram bots are an extremely powerful piece of technology that is relatively easy to set up, available for anybody completely free of charge! It can be used for your own home projects as well as for bigger enterprise applications.

All of the code can be found at this GitHub repository.

Special thanks to my dearest friend and comrade Andrei Kochemirovskii for code review and valuable inputs.

Support

If you like the content you read and want to support the author — thank you very much!

Here is my Ethereum wallet for tips:

0xB34C2BcE674104a7ca1ECEbF76d21fE1099132F0

--

--

Pudding Entertainment
Pudding Entertainment

Written by Pudding Entertainment

Serious software engineer with everlasting passion for GameDev. Dreaming of next big project. https://pudding.pro

No responses yet