Telegram bot initiate conversation with a user
Question:
I am writing a Telegram bot, to simplify the issue let’s say you can ask the bot questions in a channel and it replies to you in private. When the bot receives the command it takes the chat_id
of the user from: update.message.from_user.id
But when I do this I get an error:
Unauthorized: Forbidden: bot can't initiate conversation with a user
This sounds quite logical, as I assume this prevents from writing spam bots. The question is how do I overcome it? How can I let the user enable private messages from the bot?
Note: The bot does succeed sending messages to me but not to other users.
Answers:
Telegram Bots can’t initiate a conversation with a user.
Only an user can initiate it by clicking on the start button. There is no way to around this.
In your case, you can ask the user to start the bot before asking questions in the channel, for example by attaching a button containing the bot URL (https://t.me/bot_username).
message.from_user.id
try instead
message.chat.id
As @MarcoBuster answered, you can’t initiate a conversation with a user, unless there is already an existing conversation between him and your bot!
So you can overcome your issue by having your bot answer in the channel : "Give me a Hello and I’ll answer you in private!".
Once the user has started a private conversation, your bot can store this chatId and use it to send private messages to the user whenever you want.
I am writing a Telegram bot, to simplify the issue let’s say you can ask the bot questions in a channel and it replies to you in private. When the bot receives the command it takes the chat_id
of the user from: update.message.from_user.id
But when I do this I get an error:
Unauthorized: Forbidden: bot can't initiate conversation with a user
This sounds quite logical, as I assume this prevents from writing spam bots. The question is how do I overcome it? How can I let the user enable private messages from the bot?
Note: The bot does succeed sending messages to me but not to other users.
Telegram Bots can’t initiate a conversation with a user.
Only an user can initiate it by clicking on the start button. There is no way to around this.
In your case, you can ask the user to start the bot before asking questions in the channel, for example by attaching a button containing the bot URL (https://t.me/bot_username).
message.from_user.id
try instead
message.chat.id
As @MarcoBuster answered, you can’t initiate a conversation with a user, unless there is already an existing conversation between him and your bot!
So you can overcome your issue by having your bot answer in the channel : "Give me a Hello and I’ll answer you in private!".
Once the user has started a private conversation, your bot can store this chatId and use it to send private messages to the user whenever you want.