mirror of https://github.com/caronc/apprise
Telegram: enhanced bot owner detection to avoid exception (#517)
parent
e5945e0be1
commit
aa3d30f7b7
|
@ -483,13 +483,13 @@ class NotifyTelegram(NotifyBase):
|
||||||
# "text":"/start",
|
# "text":"/start",
|
||||||
# "entities":[{"offset":0,"length":6,"type":"bot_command"}]}}]
|
# "entities":[{"offset":0,"length":6,"type":"bot_command"}]}}]
|
||||||
|
|
||||||
if 'ok' in response and response['ok'] is True \
|
if response.get('ok', False):
|
||||||
and 'result' in response and len(response['result']):
|
for entry in response.get('result', []):
|
||||||
entry = response['result'][0]
|
if 'message' in entry and 'from' in entry['message']:
|
||||||
_id = entry['message']['from'].get('id', 0)
|
_id = entry['message']['from'].get('id', 0)
|
||||||
_user = entry['message']['from'].get('first_name')
|
_user = entry['message']['from'].get('first_name')
|
||||||
self.logger.info('Detected Telegram user %s (userid=%d)' % (
|
self.logger.info(
|
||||||
_user, _id))
|
'Detected Telegram user %s (userid=%d)' % (_user, _id))
|
||||||
# Return our detected userid
|
# Return our detected userid
|
||||||
return _id
|
return _id
|
||||||
|
|
||||||
|
|
|
@ -349,9 +349,30 @@ def test_plugin_telegram_general(mock_post, mock_get):
|
||||||
mock_post.return_value.content = dumps({
|
mock_post.return_value.content = dumps({
|
||||||
"ok": True,
|
"ok": True,
|
||||||
"result": [{
|
"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,
|
"update_id": 645421321,
|
||||||
"message": {
|
"message": {
|
||||||
"message_id": 1,
|
"message_id": 2,
|
||||||
"from": {
|
"from": {
|
||||||
"id": 532389719,
|
"id": 532389719,
|
||||||
"is_bot": False,
|
"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 obj.notify(title='hello', body='world') is False
|
||||||
assert len(obj.targets) == 0
|
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
|
# Detect the bot with a bad response
|
||||||
mock_post.return_value.content = dumps({})
|
mock_post.return_value.content = dumps({})
|
||||||
obj.detect_bot_owner()
|
obj.detect_bot_owner()
|
||||||
|
|
Loading…
Reference in New Issue