如何解决Telepot 错误不断出现
我为电报群组创建了一个机器人,但是每当有人发送民意调查时,此错误就会一次又一次地出现..
不断出现错误的 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",{"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',{'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。也许其中之一实际上仍然有效。
这个问题很可能是由于电报端 API 的变化,加上 Telepot 中对意外值的不干净处理造成的。
由于这样的消息,我自己的机器人最近也停止了工作。我在没有进行太多审查的情况下切换到 this fork - 我不再投资于我的机器人,所以我必须这样做。根据您的使用情况,这可能是也可能不是一条好的路线。
如果你选择这样做,那只是一个
python2.7 -m pip install --user https://github.com/MoumenKhadr/telepot.git
或者只是
pip install https://github.com/MoumenKhadr/telepot.git
取决于您的设置。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。