How to make an inline telegram bot
10.01.2025
40
0
0
0
0
Introduction
In this article I will show how the inline telegram bot works, how to send responses, and how to set up these responses. You will also learn how to set up translations for your bot, and of course you will be able to test my bot, @joker_gut_bot
What is the point of the inline mode of bots? The point is that the user can send bot responses on his behalf to any chats. Some bots are embedded in the telegram itself, for example:
And of course you can make your own if needed.
Preparing and installing the necessary packages
We will need the following packages:
- aiogram library
- Babel library
The first one is needed for communication and work with the Telegram API. The Babel library is required for translations.
I store the bot token in a separate file called .env, make it. It contains only one line: BOT_TOKEN=1111111111:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
You can see the complete, basic setup of the telegram bot (and token too) in this article.
Writing an inline bot
Let's move on to writing the bot itself. In the main.py file, import the following modules:
After that, at the end, we add an entry point and standard handlers to launch the application and to help with its functionality:
Now in the config.py file we add the token importer and the default router, bot_dispatcher:
The bot skeleton is ready. And it can even be launched, but it does not provide any functionality yet. Now let's start adding inline bot functionality to it. Telegram supports 20 inline responses in total:
- InlineQueryResultCachedAudio
- InlineQueryResultCachedDocument
- InlineQueryResultCachedGif
- InlineQueryResultCachedMpeg4Gif
- InlineQueryResultCachedPhoto
- InlineQueryResultCachedSticker
- InlineQueryResultCachedVideo
- InlineQueryResultCachedVoice
- InlineQueryResultAudio
- InlineQueryResultDocument
- InlineQueryResultGif
- InlineQueryResultMpeg4Gif
- InlineQueryResultPhoto
- InlineQueryResultVideo
- InlineQueryResultVoice
- InlineQueryResultContact
- InlineQueryResultGame
- InlineQueryResultLocation
- InlineQueryResultArticle
- InlineQueryResultVenue
As you can see, there are three groups of responses. The first with the Cached root, which means the bot will take data from the database. The second group without the Cached root. They use links as a data source. And the third group consists of those responses that have no analogue with Cached.
I will show the work of inline bots using the example of InlineQueryResultArticle and InlineQueryResulltPhoto. Let's add the following functions to main.py:
The send_greetings handler is called when the user writes greeting after the bot name. All it does is combine 3 InlineQueryResultArticles into the list and return the user's answer to choose from. Like this:
The send_user_images handler does the same thing, only with images, InlineQueryResultArticle.
The Telegram bot is ready. All that's left is to deploy it on the server, and voila, your new assistant is ready. I wrote about how to do this in a separate article, ooo, here it is. You can read more about it here.
Translate (optional)
Conclusions
I tried to make the article about inline bots as simple as possible and not overloaded with any databases or translations. But I eventually included translations ◑﹏◐. You can find all the bot files and resources on the corresponding tools page. Cheers.
Comments
(0)
Send
It's empty now. Be the first (o゚v゚)ノ
Used termins
- Telegram bot ⟶ This is a program that, using the Telegram **API**, can perform various actions in chats without a person.
Related questions
- What сhatbots сan't вo Despite chatgpt capturing more and more users under its influence, chatbots are still poor at solving individual user queries and complaints. They won't find you new customers, and they won't help you save money either.
- What is a telegram bot for? Telegram bots can be used for various reasons. They are universal assistants in business, can provide a convenient format for interaction with clients or be an excellent platform for hosting a website or tool.
- What is the difference between chat and channel in Telegram? There is a difference between a chat and a channel, and they are quite significant. In a channel, there is one-way communication between the creator and the audience, in chats, everyone communicates as equals. In all other respects, chats are lighter than channels. So there is no audience limit in channels, in chats up to 200 thousand.