【发布时间】:2017-12-18 21:23:58
【问题描述】:
我用 Python 编写了一个 Telegram 机器人,它在我的 Raspberry Pi (Raspbian) 上运行。在机器人运行了几个小时后,我最终看到了一个错误。
在我发布完整代码之前,有人可以帮我理解错误吗?在我需要重新启动它之前,我想无限期地运行该机器人,或者至少运行几天。
错误如下:
Traceback (most recent call last):
File "/home/pi/Schreibtisch/StreamrPreisBot/telepot/loop.py", line 37, in run_forever
self._handle(msg)
File "/home/pi/Schreibtisch/StreamrPreisBot/streamrinfobot.py", line 32, in handle
command = msg['text']
KeyError: 'text'
编辑: 使用以下代码:
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
这段代码能解决问题吗?
def handle(msg):
chat_id = msg['chat']['id']
command = msg.get('text')
【问题讨论】:
-
您还应该发布产生此错误的代码。
-
如果您至少在
loop.py中发布_handle(msg)的代码,我很乐意帮助您找出错误原因。
标签: python bots telegram keyerror