How to ensure that chat_id exists?

戏子无情 提交于 2019-12-24 08:59:14

问题


I wrote a piece of code which sends messages to a Telegram bot. In order to do so, I use the chat_id of the last conversation retrieved via the getUpdates method.

id = requests.get(f"https://api.telegram.org/bot{token}/getUpdates").json()['result'][-1]['message']['chat']['id']

My understanding is that the a conversation exists if someone started one with the bot via /start.

How can I initiate, from my code, a conversation to make sure a chat_id is available? (= that there is a conversion I can query).

I am also sure that the conversations, should they exist, are not kept indefinitely (this is another reason why requesting an update can yield empty results)


回答1:


My understanding is that the a conversation exists if someone started one with the bot via /start.

Yes, conversation is always initiated by a user:

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.

Note that /start is not the only option here.

If you try to send a message for user, who didn't start conversation with bot, you will receive something like this: {"ok":false,"error_code":400,"description":"Bad Request: chat not found"}.


How can I initiate, from my code, a conversation to make sure a chat_id is available? (= that there is a conversion I can query).

Normally, you shouldn't worry about that. Bot does not query specific user actions/requests with getUpdates, it queries all interactions from all users and then decides what to do according to internal logic you provide.

You may want to store information about users and/or their requests in a database every time you receive an Update from particular user in getUpdates.

Based on that, bot can make a decision to send for example, a message to him.


I am also sure that the conversations, should they exist, are not kept indefinitely (this is another reason why requesting an update can yield empty results)

Yes, docs clearly state that

Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.

An Update on a Telegram servers is an entity with a short life period.

If you haven't saved information about existing users, or lost a database, there's no way to retrieve that data from telegram servers.


P.S.: as a side note, I'd suggest using long polling, as Telegram Bot API is designed to be used with long polling if you're using getUpdates. The most important thing is the timeout request parameter of getUpdates method:

(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.

As written in question, you're using short polling.



来源:https://stackoverflow.com/questions/56090678/how-to-ensure-that-chat-id-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!