我在电报漫游器API中找不到任何内容

时间:2019-05-30 22:19:15

标签: telegram telegram-bot

我正在尝试获取电报机器人API中的所有chat_id

当我打电话

https://api.telegram.org/botmybot_token/getUpdates

我得到这个{"ok":true,"result":[]}

1 个答案:

答案 0 :(得分:1)

You get empty updates because there were no messages (or other actions) from users.

To obtain user's chat_id follow these steps:

  1. User must first find the bot and send it any message, because

    Bots can't initiate conversations with users. A user must either add them to a group or send them a message first. People can use telegram.me/ links or username search to find your bot.

  2. Get the following URL: https://api.telegram.org/botmybot_token/getUpdates?timeout=300. Note that you should use timeout= because this is a long polling method (i.e. you will receive response only when there is a new incoming update or timeout is reached):

    (timeout is) Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. Should be positive, short polling should be used for testing purposes only.

You can still receive updates without timeout setting, but that's not the right way to read user interaction. Instead you can you just run long-polling (i.e. with timeout setting) requests in a loop.

You can read more on getting infromation about user's chat_id here:
https://stackoverflow.com/a/56093268/2315573