【问题标题】:Python Telegram API raises unhelpful error when trying to edit message media尝试编辑消息媒体时,Python Telegram API 引发了无用的错误
【发布时间】:2021-07-17 21:58:20
【问题描述】:

我正在用 python 编写一个电报机器人,根据命令,它每隔几秒钟就会开始向聊天室发送我电脑上的一个窗口的照片。当然,我不希望聊天中充斥着照片,所以我想返回并编辑我使用 edit_media 发送的第一条消息。

目前我对该命令的回调函数如下所示:

def callback(update: Update, context: CallbackContext):
    first = True
    while True:
        image = screenshot('Snake')     # Gets a screenshot of the window
        bytesio = io.BytesIO()
        image.save(bytesio, format='PNG')
        bytes_image = bytes(bytesio.getvalue())     # Convert to bytes object
        if first:       # First time send a full message 
            context.bot.send_photo(update.message.chat_id, bytes_image)
            first = False
        else:       
            update.message.edit_media(media=InputMediaPhoto(media=bytes_image)) # Edit the message with the next photo
        time.sleep(2)

此函数运行时,第一条消息发送成功,但尝试编辑消息会引发错误: telegram.error.BadRequest: Message can't be edited

如何解决这个问题才能成功编辑图像?

错误的完整回溯是:

Traceback (most recent call last):
  File "C:\Users\...\telegram\ext\dispatcher.py", line 555, in process_update
    handler.handle_update(update, self, check, context)
  File "C:\Users\...\telegram\ext\handler.py", line 198, in handle_update
    return self.callback(update, context)
  File "C:\Users\...\TelegramGameHub\main.py", line 56, in snake
    update.message.edit_media(media=InputMediaPhoto(media=bytes_image))
  File "C:\Users\...\telegram\message.py", line 2016, in edit_media
    return self.bot.edit_message_media(
  File "C:\Users\...\telegram\bot.py", line 130, in decorator
    result = func(*args, **kwargs)
  File "C:\Users\...\telegram\bot.py", line 2723, in edit_message_media
    return self._message(
  File "C:\Users\...\telegram\ext\extbot.py", line 199, in _message
    result = super()._message(
  File "C:\Users\...\telegram\bot.py", line 332, in _message
    result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)
  File "C:\Users\...\telegram\bot.py", line 295, in _post
    return self.request.post(
  File "C:\Users\...\telegram\utils\request.py", line 354, in post
    result = self._request_wrapper('POST', url, fields=data, **urlopen_kwargs)
  File "C:\Users\...\telegram\utils\request.py", line 279, in _request_wrapper
    raise BadRequest(message)

【问题讨论】:

    标签: python python-requests telegram telegram-bot python-telegram-bot


    【解决方案1】:

    问题是你的线

    update.message.edit_media(media=InputMediaPhoto(media=bytes_image))
    

    不会编辑您发送的照片消息。相反,它会尝试编辑最初触发回调的消息。

    要获取您发送的消息,您需要执行类似的操作

    message = context.bot.send_photo(update.message.chat_id, bytes_image)
    

    请注意,PTB 为此类事情提供了快捷方式。所以你可以把它缩写为

    message = update.message.reply_photo(bytes_image)
    

    这是(几乎)等价的。

    现在您可以拨打message.edit_media(...)更新照片了。

    另外需要注意的是你的循环

    while True:
       ...
       time.sleep(2)
    

    正在阻塞。这意味着在输入函数callback 后,您的机器人将不会处理任何其他更新。 (如果您知道run_async 做了什么,请注意:即使您异步运行此回调,它也会阻塞其中一个工作线程,并且一旦有足够的更新触发了此回调,所有工作线程都会被阻止并且您的机器人获胜'不再处理任何更新)

    我强烈建议使用内置的JobQueue 来处理这些事情。另请参阅 wiki pageexample


    免责声明:我目前是python-telegram-bot 的维护者。

    【讨论】:

    • 感谢您的回答,您是对的,我错误地尝试编辑错误消息。解决问题后,我遇到了你提到的循环阻塞的另一件事,我使用threading 创建了另一个线程来解决这个问题,可以吗,还是应该使用JobQueue 代替?
    • 另外,如果您有时间和耐心,我会很高兴您能查看其余的程序代码并告诉我可以改进的地方。当然,没有义务。我发布了到目前为止我所做的事情here
    • 启动线程听起来也很合理,当然
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2018-05-24
    • 2016-09-29
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多