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:
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)
Also, if you need to make your bot support multiple languages of communication, then you should also read this article, where I show in detail how to do this.
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.
In this article you will see how I get the telegram bot token (via bot father), where I store the token and how I set it up
Used termins
Telegram bot ⟶ This is a program that, using the Telegram **API**, can perform various actions in chats without a person.
aiogram ⟶ It is a framework that build upon asyncio and aiohttp python modules, fully asynchronous, for creating telegram bots.
Python programming language ⟶ It is interpreted, build upon object-oriented approach, with dynamic semantics, and yes it s high-level programming language. Actively using for rapid development, scripting and gluing part of existing apps.
Search bot ⟶ A search bot, also known as a web crawler or spider, is a software program designed to automatically browse the internet and index content for search engines. These bots visit web pages, read their content, and follow links to discover new pages. The data they gather helps search engines like Google, Bing, or others.
Related questions
Is python best option to create a telegram bots
Python is one of the most popular programming languages for developing bots. It is very simple and intuitive. It is a great choice for beginners. Python also has a huge number of libraries in its collection that will make your life and work easier.
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.
Which language to choose to create a Telegram bot
I know 5 languages in which you can write Telegram bots. Python (aiogram), JS (Node.js), PHP (MadelineProto), Java (TelegramBots) and Go (tgbotapi). The choice of which language to write is depends only on what you already know and what preferences in languages you have.