Handle Telegram callbacks that are not text messages. Fixes #147

This commit is contained in:
Pēteris Caune 2018-01-18 00:39:44 +02:00
parent 1392226538
commit c6e35c9b39
3 changed files with 22 additions and 5 deletions

View File

@ -16,7 +16,7 @@ telegram_callback = {
},
"text": {"type": "string"}
},
"required": ["chat", "text"]
"required": ["chat"]
}
},
"required": ["message"]

View File

@ -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)

View File

@ -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"]