Telegram: enhanced bot owner detection to avoid exception (#517)

pull/521/head
Stephane Desneux 3 years ago committed by GitHub
parent e5945e0be1
commit aa3d30f7b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -483,13 +483,13 @@ class NotifyTelegram(NotifyBase):
# "text":"/start",
# "entities":[{"offset":0,"length":6,"type":"bot_command"}]}}]
if 'ok' in response and response['ok'] is True \
and 'result' in response and len(response['result']):
entry = response['result'][0]
if response.get('ok', False):
for entry in response.get('result', []):
if 'message' in entry and 'from' in entry['message']:
_id = entry['message']['from'].get('id', 0)
_user = entry['message']['from'].get('first_name')
self.logger.info('Detected Telegram user %s (userid=%d)' % (
_user, _id))
self.logger.info(
'Detected Telegram user %s (userid=%d)' % (_user, _id))
# Return our detected userid
return _id

@ -349,9 +349,30 @@ def test_plugin_telegram_general(mock_post, mock_get):
mock_post.return_value.content = dumps({
"ok": True,
"result": [{
"update_id": 645421319,
# Entry without `message` in it
}, {
# Entry without `from` in `message`
"update_id": 645421320,
"message": {
"message_id": 2,
"chat": {
"id": 532389719,
"first_name": "Chris",
"type": "private"
},
"date": 1519694394,
"text": "/start",
"entities": [{
"offset": 0,
"length": 6,
"type": "bot_command",
}],
}
}, {
"update_id": 645421321,
"message": {
"message_id": 1,
"message_id": 2,
"from": {
"id": 532389719,
"is_bot": False,
@ -428,6 +449,54 @@ def test_plugin_telegram_general(mock_post, mock_get):
assert obj.notify(title='hello', body='world') is False
assert len(obj.targets) == 0
# Do the test again, but with ok not set to True
mock_post.return_value.content = dumps({
"ok": False,
"result": [{
"update_id": 645421321,
"message": {
"message_id": 2,
"from": {
"id": 532389719,
"is_bot": False,
"first_name": "Chris",
"language_code": "en-US"
},
"chat": {
"id": 532389719,
"first_name": "Chris",
"type": "private"
},
"date": 1519694394,
"text": "/start",
"entities": [{
"offset": 0,
"length": 6,
"type": "bot_command",
}],
}},
],
})
# No user will be detected now
obj = plugins.NotifyTelegram(bot_token=bot_token, targets=None)
# No user detected; this happens after our firsst notification
assert len(obj.targets) == 0
assert obj.notify(title='hello', body='world') is False
assert len(obj.targets) == 0
# An edge case where no results were provided; this will probably never
# happen, but it helps with test coverage completeness
mock_post.return_value.content = dumps({
"ok": True,
})
# No user will be detected now
obj = plugins.NotifyTelegram(bot_token=bot_token, targets=None)
# No user detected; this happens after our firsst notification
assert len(obj.targets) == 0
assert obj.notify(title='hello', body='world') is False
assert len(obj.targets) == 0
# Detect the bot with a bad response
mock_post.return_value.content = dumps({})
obj.detect_bot_owner()

Loading…
Cancel
Save