【问题标题】:How to delete telegram bot own messages?如何删除电报机器人自己的消息?
【发布时间】:2020-10-22 04:50:07
【问题描述】:

我想要实现的是,在我通过 bot X 发布新消息之前,我删除了同一个 bot X 发布的以前的消息。我正在查看方法 bot.delete_message 但这需要 message_id 这意味着什么我需要将 id some-have 存储在本地。有没有办法解决?喜欢获取机器人自己的消息?

【问题讨论】:

    标签: telegram-bot python-telegram-bot


    【解决方案1】:
    import python-telegram....
    import time
    
    current_message = None
    
    
    def current_message_updater(new_message):
        global current_message
        # delete last message
        if not current_message:
            current_message.delete()
        # send new message will return its message object,
        # we assign it into current_message variable
        new = bot.send_message(....., text=new_message)
        current_message = new
    
    
    # if all of this is happening in the same function
    # you can do like this
    
    def my handler():
        message = bot.send_message(...., text="hi new user")
        time.sleep(1)
        message.delete()
        another_one = bot.send_message(..., text="How are u?")
    

    【讨论】:

    • 对我不起作用。由于脚本由 cron 作业运行,因此我无法将内容存储在内存中。我最终将最后一条消息存储在一个文件中然后读取它。
    • tellegram 库中有一个方法返回 message history,当用户向机器人发送消息时,机器人会检查历史记录并获取机器人发送的最后一条消息——如果没有找到消息,机器人会忽略以下else取最后一条消息将其删除并发送新消息
    • 是的,但我需要机器人自己的消息历史记录,这似乎是不可能的。
    • 如果你的意思是这个,Bot 没有保存消息,除非它可能。
    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2021-04-21
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多