forked from GithubBackups/healthchecks
Handle Telegram callbacks that are not text messages. Fixes #147
This commit is contained in:
parent
1392226538
commit
c6e35c9b39
@ -16,7 +16,7 @@ telegram_callback = {
|
||||
},
|
||||
"text": {"type": "string"}
|
||||
},
|
||||
"required": ["chat", "text"]
|
||||
"required": ["chat"]
|
||||
}
|
||||
},
|
||||
"required": ["message"]
|
||||
|
@ -56,12 +56,15 @@ class AddTelegramTestCase(BaseTestCase):
|
||||
def test_bot_handles_bad_message(self, mock_get):
|
||||
samples = ["", "{}"]
|
||||
|
||||
# text is missing
|
||||
# text is not string
|
||||
samples.append(json.dumps({
|
||||
"message": {"chat": {"id": 123, "type": "group"}}
|
||||
"message": {
|
||||
"chat": {"id": 123, "type": "invalid"},
|
||||
"text": 123
|
||||
}
|
||||
}))
|
||||
|
||||
# bad type
|
||||
# bad message type
|
||||
samples.append(json.dumps({
|
||||
"message": {
|
||||
"chat": {"id": 123, "type": "invalid"},
|
||||
@ -74,3 +77,16 @@ class AddTelegramTestCase(BaseTestCase):
|
||||
content_type="application/json")
|
||||
|
||||
self.assertEqual(r.status_code, 400)
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_it_handles_missing_text(self, mock_get):
|
||||
data = {
|
||||
"message": {
|
||||
"chat": {"id": 123, "title": "My Group", "type": "group"}
|
||||
}
|
||||
}
|
||||
r = self.client.post("/integrations/telegram/bot/", json.dumps(data),
|
||||
content_type="application/json")
|
||||
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertFalse(mock_get.called)
|
||||
|
@ -823,7 +823,8 @@ def telegram_bot(request):
|
||||
except jsonschema.ValidationError:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
if "/start" not in doc["message"]["text"]:
|
||||
text = doc["message"].get("text", "")
|
||||
if "/start" not in text:
|
||||
return HttpResponse()
|
||||
|
||||
chat = doc["message"]["chat"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user