How to obtain Telegram chat_id for a specific user?
The message updates you receive via getUpdates
or your webhook will contain the chat ID for the specific message. It will be contained under the message.chat.id
key.
This seems like the only way you are able to retrieve the chat ID. So if you want to write something where the bot initiates the conversation you will probably have to store the chat ID in relation to the user in some sort of key->value store like MemCache or Redis.
I believe their documentation suggests something similar here, https://core.telegram.org/bots#deep-linking-example. You can use deep-linking to initiate a conversation without requiring the user to type a message first.
I created a bot to get User or GroupChat id,
just send the /my_id
to telegram bot @get_id_bot
.
It does not only work for user chat ID, but also for group chat ID.
To get group chat ID, first you have to add the bot to the group,
then send /my_id
in the group.
Here's the link to the bot.