删除投票 python-telegram-bot

Deleting a poll python-telegram-bot

我正在使用 python-telegram-bot 库开发电报机器人 python 我希望我的机器人向用户发送测验这是代码:

 question = dict_api.get_question()
    word = question['word']
    answer = question['answer']
    options = question['options']
    q = f"What is the translation of the word '{word}'"
    context.bot.send_poll(chat_id=person_id, question=q, options=options, type=Poll.QUIZ, correct_option_id=answer)

我有 PollHandler 来处理响应,当用户响应时我希望机器人为用户删除测验,我该怎么做?

我试过

删除它
context.bot.delete_message(message_id=update.poll.id, chat_id=chat_id)

因为有人会删除一条消息,但它没有用,我想是因为电报对民意调查的处理方式不同。 提前致谢

bot.delete_messagemessage_id 参数需要消息 ID,而不是轮询 ID。请注意,民意调查更新不包含 message_id,因为民意调查可以例如被转发,因此他们可以有多个 message_ids 即使在您的机器人甚至不是成员的聊天中也是如此。

但是,如果您只想删除原始消息,您可以将映射轮询 ID -> (chat_id, message_id) 存储在例如context.bot_data 然后使用 chat_id, message_id = context.bot_data[update.poll.id] 取回存储的值以删除消息。请注意,context.user/chat_data 在这里不起作用,因为轮询更新与特定的 user/chat 无关,因此 context.user/chat_dataPollHandler 回调中将是 None .

除了存储 (chat_id, message_id),您还可以只存储 bot.send_poll returns 的 Message 对象 - 然后您只需调用 context.bot_data[update.poll.id].delete()

参考文献:


免责声明:我目前是 python-telegram-bot.

的维护者