很多tg机器人,运行一段时间后出现消息问题。
如果机器人是基于telepot开发的,可能错误是这样的:
Faced with the same problem:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\telepot\aio\loop.py", line 38, in run_forever
self._update_handler(update)
File "C:\Program Files\Python37\lib\site-packages\telepot\aio\loop.py", line 82, in <lambda>
self._handle(_extract_message(update)[1]))
File "C:\Program Files\Python37\lib\site-packages\telepot\loop.py", line 111, in _extract_message
'pre_checkout_query'])
File "C:\Program Files\Python37\lib\site-packages\telepot\__init__.py", line 68, in _find_first_key
raise KeyError('No suggested keys %s in %s' % (str(keys), str(d))
)
Environment:
Python 3.7.7
telepot==12.7
Attempts:
Using @Carlog suggestion raised for me the exception:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\telepot\aio\loop.py", line 38, in run_forever
self._update_handler(update)
File "C:\Program Files\Python37\lib\site-packages\telepot\aio\loop.py", line 82, in <lambda>
self._handle(_extract_message(update)[1]))
File "C:\Program Files\Python37\lib\site-packages\telepot\aio\__init__.py", line 912, in handle
id = calculate_seed(msg)
File "C:\Program Files\Python37\lib\site-packages\telepot\delegate.py", line 303, in f
seed = fn(msg)
File "C:\Program Files\Python37\lib\site-packages\telepot\delegate.py", line 9, in w
return fn(*args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\telepot\delegate.py", line 24, in <lambda>
if types == 'all' or msg['chat']['type'] in types
TypeError: 'int' object is not subscriptable
这是因为telepot库已经不更新了,有时候tg官方的更新,会导致一些消息接收失败。
这里有个解决方案可以尝试:
找到文件 Lib\site-packages\telepot\loop.py 修改这个函数 _extract_message. (修改前最好备份)
def _extract_message(update):
key = _find_first_key(update, ['update_id',
'message',
'edited_message',
'channel_post',
'edited_channel_post',
'callback_query',
'inline_query',
'chosen_inline_result',
'shipping_query',
'pre_checkout_query'])
if key != 'update_id':
return key, update[key]
if 'message' in update.keys():
return 'message', update['message']
if 'my_chat_member' in update.keys():
return 'message', {'message_id': update['update_id'],
'from': update['my_chat_member']['from'],
'chat': update['my_chat_member']['chat'],
'date': update['my_chat_member']['date'],
'text': f"It's update_id {update['update_id']}"
}
raise Exception('The hotfix for update_id bug needs to upgrade')
参考网址: