How to obtain Telegram chat_id for a specific user?

前端 未结 7 1529
自闭症患者
自闭症患者 2020-12-01 02:36

How to obtain user chat_id in Telegram bot API? The documentation says:

Integer | Unique identifier for the message recipient — User or G

7条回答
  •  孤街浪徒
    2020-12-01 03:10

    Straight out from the documentation:

    Suppose the website example.com would like to send notifications to its users via a Telegram bot. Here's what they could do to enable notifications for a user with the ID 123.

    1. Create a bot with a suitable username, e.g. @ExampleComBot
    2. Set up a webhook for incoming messages
    3. Generate a random string of a sufficient length, e.g. $memcache_key = "vCH1vGWJxfSeofSAs0K5PA"
    4. Put the value 123 with the key $memcache_key into Memcache for 3600 seconds (one hour)
    5. Show our user the button https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA
    6. Configure the webhook processor to query Memcached with the parameter that is passed in incoming messages beginning with /start. If the key exists, record the chat_id passed to the webhook as telegram_chat_id for the user 123. Remove the key from Memcache.
    7. Now when we want to send a notification to the user 123, check if they have the field telegram_chat_id. If yes, use the sendMessage method in the Bot API to send them a message in Telegram.

提交回复
热议问题