How I wrote a bot for the summary of telegram channels

Hi all!
My name is Azret, I am CTO in AAA (pronounced triple-ey). We develop LLM applications for business. We solve various problems – optimization of internal processes through AI, marketing research and launch of new products. But the request for the bot I want to talk about suddenly came from the founder of our agency.

The situation is clear – I subscribe to a bunch of telegram channels, I don’t have time to read them, it makes me feel bad, I feel FOMO. And I would like not to miss anything important, spend as little time as possible and have everything in one place.

From this request came the idea bot summary. I encountered a lot of interesting problems while working on it and thought it would be helpful if I shared the solutions.

This overview article contains details of the architecture and stack. If anyone is interested, please see cat.

What's the difficulty?

Everything is clear with the core of the application – this is LLM.
Replies from GigaСhat And YandexGPT I was not satisfied with the server configuration for LLAMA costs more, so I settled on ChatGPT API. One of the inconveniences is that you need to set up a proxy, but for the 2nd year now this has not been a problem for any of us.

The main difficulty was in receiving messages from channels.
Let's say we have a bot into which the user sends a channel from which he wants to receive a summary. What should the bot do next?
In order for the bot to read messages, it must be added to the channel. He cannot add himself. The bot also cannot read messages that were received before it was added to the channel.

Poking around this situation, I came to the conclusion that this cannot be done without a person.
Human-bot.
A bot that manages a human account.

About Telegram API and Telegram Bot API

Telegram Bot API is part of Telegram API.

Telegram API is the complete telegram API. With its help you can create your own telegram clients.
Python libraries – Telethon, pyrogram.

Telegram Bot API is an API for interacting with bots. You can use it to create bots. The most popular python implementations are aiogram, pyTelegramBotAPI, python-telegram-bot.

Architecture

I got the following diagram:

  1. Client bot – with which the user interacts. It receives channels and produces a summary.

  2. A bot on a custom TG – which, under a human account, goes to the specified channels, subscribes and monitors messages.

  3. A wrapper over the LLM model – it accepts messages as input and produces a summary.

  4. Message storage – for originals and summary text.

  5. Payment server – we use it to catch webcookies from the payment system.

  6. A message broker that connects all components.

As usual, the client does not even suspect what is happening on the back

As usual, the client does not even suspect what is happening on the back

To simplify very much, the flow is like this:

  1. User adds a channel in the bot

  2. The bot account subscribes to this channel, takes messages from there and forwards them to the storage

  3. At moment X, a request to create a summary is sent from the storage to LLM

  4. The resulting summaries are sent to the user in the bot.

Of course, there are nuances, but more on them in the following articles.

Stack

All components are written in python.

  1. Custom bot written in aiogram. It uses aiogram-dialog to work with the menu, APscheduler for the summary planner, SQLAlchemy for storing user data, redis for state caching

  2. Custom telegram client written in Telethon. There is nothing additional in it, since all data is stored in an automatically created session file.

  3. Message storage is an application without a framework. It also uses SQLAlchemy.

  4. LLM wrapper is also an application without a framework. Works through official library from open ai. A good prompt was enough; using langchain would have been an overkill. The client works through a proxy.

  5. The payment server is written in FastAPI. Literally a couple of files and a couple of methods. But to get it going, I had to tinker with domains, certificates, and a reverse proxy.

  6. Message Broker – NATS. Asynchronous, fast, with support for pub-sub and request-response mechanics. More convenient than http requests.

All this stuff runs on a VPS server from Selectel under ubuntu.

Conclusion

It's better to see once

The bot calculates the time saved at an average reading speed of 180 words per minute

The bot calculates the time saved at an average reading speed of 180 words per minute

The bot can be launched using the link – https://t.me/aaa_scout_bot.
I'm glad that the pet project has grown in detail and reached a wide audience.
It has a free plan, you can poke around without worry, and you can also leave comments and reviews.

The name “scout” is an omen of future features. I plan to add filters so that the user can receive only messages that interest him from a ton of channels.

As for the next articles, I definitely want to talk about connecting the payment system, namely, how the “bot-webhook-payment” combination works.

Questions, remarks, comments are welcome.
Thank you.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *