如何确保chat_id存在? [英] How to ensure that chat_id exists?

查看:58
本文介绍了如何确保chat_id存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一段向 Telegram 机器人发送消息的代码.为此,我使用通过 getUpdates 方法.

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

我的理解是,如果有人通过 /start 与机器人开始对话,则存在对话.

如何从我的代码发起对话以确保 chat_id 可用?(= 我可以查询转换).>

我也确信对话(如果存在)不会无限期地保留(这是请求更新会产生空结果的另一个原因)

解决方案

我的理解是,如果有人通过/start 与机器人开始对话,则存在对话.

是的,对话总是由用户发起:

<块引用>

机器人无法发起与用户的对话.用户必须先将他们添加到组或向他们发送消息.人们可以使用 telegram.me/链接或用户名搜索来查找您的机器人.

请注意,/start 不是此处的唯一选项.

如果您尝试向未开始与机器人对话的用户发送消息,您将收到如下消息:{"ok":false,"error_code":400,"description":"错误请求:找不到聊天"}.

<小时><块引用>

如何从我的代码发起对话以确保 chat_id 可用?(= 我可以查询转换).

通常情况下,您不必担心这一点.Bot 不会使用 getUpdates 查询特定的用户操作/请求,它会查询所有用户的所有交互,然后根据您提供的内部逻辑决定要做什么.

您可能希望每次在 getUpdates 中收到来自特定用户的 Update 时,将有关用户和/或其请求的信息存储在数据库中.

基于此,机器人可以做出决定,例如向他发送消息.

<小时><块引用>

我也确信对话(如果存在)不会无限期保留(这是请求更新会产生空结果的另一个原因)

是的,文档明确指出

<块引用>

传入的更新存储在服务器上,直到机器人以任何一种方式接收它们,但它们的保存时间不会超过 24 小时.

Telegram 服务器上的 Update 是一个生命周期较短的实体.

如果您尚未保存有关现有用户的信息,或者丢失了数据库,就无法从电报服务器检索这些数据.

<小时>

PS:作为旁注,我建议使用长轮询,如 Telegram Bot API设计长轮询一起使用,如果您使用<代码>获取更新.最重要的是getUpdates方法的timeout请求参数:

<块引用>

(timeout is) 长轮询的超时时间(以秒为单位).默认为 0,即通常的短轮询.应该是肯定的,短轮询应仅用于测试目的.

如问题所述,您使用的是短轮询.

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)

解决方案

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.

这篇关于如何确保chat_id存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆