python-3.x - 删除 Telegram Bot 的回复

标签 python-3.x telegram telegram-bot python-telegram-bot

我有一个用 Python 编写的 Telegram Bot 。它根据代码中提到的特定命令发送消息。我想在 X 秒后删除此机器人发送的回复。有一个 Telegram Bot API 可以删除消息

https://api.telegram.org/botBOTID/deleteMessage?chat_id=?&message_id=?

要删除消息,我们需要聊天 ID消息 ID。要获取机器人回复消息的聊天 ID消息 ID,我需要继续阅读所有消息(甚至来自用户)并找到这些 ID。这会增加机器人的大量开销。

有没有其他方法可以在不阅读所有消息的情况下找到这些id

最佳答案

这是Chat对象。它包含聊天的标识符。

partial screenshot of chat object

这是Message对象。它包含该消息的标识符和表示其所在对话的 Chat 对象。

partial screenshot of message object

sendMessage REST 函数返回您成功发送的消息。

partial screenshot of sendMessage documentation

因此,这里的解决方案是存储发送消息时获取的 Message 对象,然后使用存储对象中的参数调用删除 api (Message.message_id) > 和Message.chat.id)。

关于Python,您可以使用pickle在文件中存储对象的模块。

关于python-3.x - 删除 Telegram Bot 的回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50912849/

相关文章:

telegram-bot - python telegram bot(Telepot)群聊

javascript - 如果用户同时发送多张照片,如何让机器人响应一次?

python - 通过添加相应的值在 2D numpy 数组中创建唯一行

python - 将文本文件转换为字典

python - 优化使用python 3生成大量随机数

python - 更改上下文的背景图

telegram botfather 不允许制作更多的机器人

json - 如何与 Telegram API 交互

Telegram API 编辑消息

telegram-bot - 使用 telegram 和 webhook 发送多条消息