How Personal Dialogs with Telegram Bots Work
You want your bot to answer users in personal dialogs.
Even if your bot is designed to work in groups or channels, it is a good idea to implement a greeting message in PM. This way, people who get interested in the bot and open its profile will receive a response rather than encountering no interaction. You can include an "Add to your group" link using deep links for groups.
Starting a personal dialog
A bot cannot send messages to a user until the user has initiated the dialog. Once the dialog is started, the bot can send messages at any time. Note that bots cannot send messages to other bots.
The /start
command
Here is how a personal dialog usually starts:
A user opens the bot with a link or by searching in the app. They then see the intro text and the "Start" button.
When the user clicks the button, the /start
command is sent, which signals that the private chat has begun. The bot should respond to this command with a greeting or usage instructions.
@dp.message(CommandStart())
async def handle_start(message: Message):
await message.answer('Hi!')
Just like any other chat, the dialog with the bot appears in the user's recent chat list.
WARNING
The /start
command does not necessarily mean this is the user's first interaction with the bot. It's important to ensure your bot handles situations where a user sends /start
after they have already initiated a dialog previously.
Extra input
You can use deep links so that the /start
message contains additional information.
Alternative ways to start a dialog
Sometimes a bot may send messages to a user even if they haven't started the dialog explicitly. This happens in one of the following cases:
- The user requested to join a group or channel where the bot manages join requests.
- The user authorized on a site with Telegram Login Widget through the bot.
When this occurs, the Telegram app shows the user an explanation of why the bot is contacting them.
Stopping the dialog
A user can stop the dialog by blocking the bot. The bot will not be able to send personal messages to the user until they unblock it.
How to check if the bot may text to a user
If you need to determine whether a user has blocked your bot, you can use the following method.
Attempt to show a "Bot is typing..." status in the dialog. If Telegram servers return an error, it means the bot cannot send messages to the user — so either the user has blocked the bot or the dialog has never started.
This action has minimal rate limiting, so you can do it frequently.
try:
await bot.send_chat_action(chat, 'typing')
except TelegramForbiddenError:
print("Can't send messages")