Telepot 错误不断出现

Telepot error that keeps appearing again and again

我为 telegram 群组创建了一个机器人,但是每当有人发送 poll 时,这个错误就会一次又一次地出现..

API 消息不断报错:

{"update_id":869921319,
"poll":{"id":"5427146762856956026","question":"[4/10] Qui \u00e9tait le ma\u00eetre de Grey","options":[{"text":"Oul","voter_count":6},{"text":"Grandin\u00e9","voter_count":0},{"text":"Leon","voter_count":0},{"text":"Mavis","voter_count":2}],"total_voter_count":8,"is_closed":true,"is_anonymous":false,"type":"quiz","allows_multiple_answers":false,"correct_option_id":0}}

我的代码:

import telepot
def on_chat_message(msg):
    try:
        content_type, chat_type, chat_id = telepot.glance(msg)
    except Exception as e:
        return "Oops!", e.__class__, "occurred."
def on_callback_query(msg):
    query_id, chat_id, query_data = telepot.glance(msg, flavor='callback_query')

TOKEN = "BOT TOKEN"
bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message,
                  'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

while 1:
    time.sleep(10)

错误:

Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\telepot\loop.py", line 67, in run_forever
    self._update_handler(update)
  File "C:\Python38\lib\site-packages\telepot\loop.py", line 153, in <lambda>
    collectloop.input_queue.put(_extract_message(update)[1]))
  File "C:\Python38\lib\site-packages\telepot\loop.py", line 103, in _extract_message
    key = _find_first_key(update, ['message',
  File "C:\Python38\lib\site-packages\telepot\__init__.py", line 68, in _find_first_key
    raise KeyError('No suggested keys %s in %s' % (str(keys), str(d)))
KeyError: "No suggested keys ['message', 'edited_message', 'channel_post', 'edited_channel_post', 'callback_query', 'inline_query', 'chosen_inline_result', 'shipping_query', 'pre_checkout_query'] in {'update_id': 869921319, 'poll': {'id': '5427146762856956026', 'question': '[4/10] Qui était le maître de Grey', 'options': [{'text': 'Oul', 'voter_count': 6}, {'text': 'Grandiné', 'voter_count': 0}, {'text': 'Leon', 'voter_count': 0}, {'text': 'Mavis', 'voter_count': 2}], 'total_voter_count': 8, 'is_closed': True, 'is_anonymous': False, 'type': 'quiz', 'allows_multiple_answers': False, 'correct_option_id': 0}}"

如您所知,telepot 已经有一段时间没有维护了。在我看来 this pull request 试图解决这个问题,但从未被合并。可能不建议继续使用telepot。

不过,你可以看看forks。也许其中之一实际上仍然有效。

这个问题很可能是由于 telegram 端 API 的变化,以及 telepot 中意外值的不干净处理造成的。

我自己的机器人最近也因为这样的消息而停止工作。我已经在没有进行太多审查的情况下切换到 this fork - 我不再投资于我的机器人,所以必须这样做。根据您的使用情况,这可能是也可能不是一个好的途径。

如果你选择去做,那简直就是一个

python2.7 -m pip install --user https://github.com/MoumenKhadr/telepot.git

或者只是

pip install https://github.com/MoumenKhadr/telepot.git

取决于您的设置。