我正在与电报机器人合作。
我在BotFather中注册了我的机器人,并在Google Cloud上建立了“服务器”,并设置了webHook。
我的机器人运行正常,当我向机器人发送一些命令时,它会回复预期的方式。我无法理解的是来自“服务器”(谷歌云)的消息。
当我分析它时,我感觉到电报会不断向我的API发送请求,而不是我以前发送的每条消息,而是其中的一些消息。
这是日志:
2019-04-28 07:47:51 default[20190428t121301] [2019-04-28 07:47:51,838] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:47:51 default[20190428t121301] Traceback (most recent call last): File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/vmagent/app/main.py", line 96, in telegram_notification chat_id = data['message']['chat']['id'] KeyError: 'message'
2019-04-28 07:47:53 default[20190428t121301] {'update_id': ...., 'edited_message': {'message_id': 360, 'from': {'id': ..., 'is_bot': False, 'first_name': '...', 'language_code': 'ru'}, 'chat': {'id': ..., 'first_name': '...', 'type': 'private'}, 'date': 1556389741, 'edit_date': 1556389827, 'text': '.......'}}
2019-04-28 07:47:53 default[20190428t121301] [2019-04-28 07:47:53,191] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:47:53 default[20190428t121301] Traceback (most recent call last): File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/vmagent/app/main.py", line 96, in telegram_notification chat_id = data['message']['chat']['id'] KeyError: 'message'
2019-04-28 07:47:54 default[20190428t121301] {'update_id': ....., 'edited_message': {'message_id': 360, 'from': {'id': ..., 'is_bot': False, 'first_name': '....', 'language_code': 'ru'}, 'chat': {'id': ...., 'first_name': '....', 'type': 'private'}, 'date': 1556389741, 'edit_date': 1556389812, 'text': 'Сафмуллин Данил: 3400.0,\nРаевская Вероника: 4000.0'}}fa
2019-04-28 07:47:54 default[20190428t121301] [2019-04-28 07:47:54,123] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:47:54 default[20190428t121301] Traceback (most recent call last): File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/vmagent/app/main.py", line 96, in telegram_notification chat_id = data['message']['chat']['id'] KeyError: 'message'
2019-04-28 07:48:00 default[20190428t121301] {'update_id': 448395744, 'message': {'message_id': 490, 'from': {'id': ..., 'is_bot': False, 'first_name': '...', 'language_code': 'ru'}, 'chat': {'id': ..., 'first_name': '...', 'type': 'private'}, 'date': 1556430040, 'voice': {'duration': 1, 'mime_type': 'audio/ogg', 'file_id': 'AwADAgADFgMAAjgJKEqOtfGGrxO9KgI', 'file_size': 4400}}}
2019-04-28 07:48:00 default[20190428t121301] [2019-04-28 07:48:00,395] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:48:00 default[20190428t121301] Traceback (most recent call last): File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/vmagent/app/main.py", line 97, in telegram_notification text = data['message']['text'] KeyError: 'text'
如您所见,有些消息发送无数次,有时请求只是丢弃,而我收到了Traceback KeyError。
我不了解这种行为。
1)为什么我收到一些相同的消息? 我自己没有将其发送给我的机器人。没有人可以发送它,因为可以检查chat_id。
2)为什么有一些带有KeyError的回溯? 根据API电报文档,我认为Webhook仅在某些用户发送消息时才允许发送请求?这就像Telegram不断向我的服务器发送请求。
@app.route('/telegram_notification', methods=['POST'])
def telegram_notification():
if request.method == "POST":
data = request.get_json()
print(data)
if data is None:
return jsonify({'success': True})
# structure of data
# {'update_id': <int>,
# 'message':
# {'message_id': 138,
# 'from':
# {'id': <int>,
# 'is_bot': False,
# 'first_name': '<str>',
# 'language_code': 'ru'},
# 'chat':
# {'id': <int>,
# 'first_name': '<str>',
# 'type': 'private'},
# 'date': 1556302822,
# 'text': 'hi'}
# }
# print(data)
chat_id = data['message']['chat']['id']
text = data['message']['text']
if chat_id == <int>:
# processing
return jsonify({'success': True})
我也不明白,为什么当我删除Webhook然后再次设置它,然后重新启动“服务器”时,我立即收到了来自我的机器人的许多消息。
编辑一个
在telegram_notification()
下的# proccessing
中,我正在请求外部API
答案 0 :(得分:0)
我想我明白了。
问题如下。我已经与telegram_notification()
中的外部API建立了连接,并且当我向机器人发送消息后导致请求外部API时,我期望得到响应,但是外部API有时返回500错误或404错误,并且请求被丢弃。
我以为Telegram经过合理的尝试次数后就放弃了,并停止了请求我的Google Cloud Server,但是Telegram继续尝试并发送请求,他们放弃了,在telegram_notification()
中我没有得到任何回报。
因此,我得到了KeyError和Telegram不断向服务器发送请求,直到服务器正确响应为止。
现在,我修复了来自机器人的数十个消息的外部API。
目前,我看到了预期的行为。