Browse Source

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

pull/149/head
Pēteris Caune 7 years ago
parent
commit
c6e35c9b39
3 changed files with 22 additions and 5 deletions
  1. +1
    -1
      hc/front/schemas.py
  2. +19
    -3
      hc/front/tests/test_add_telegram.py
  3. +2
    -1
      hc/front/views.py

+ 1
- 1
hc/front/schemas.py View File

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


+ 19
- 3
hc/front/tests/test_add_telegram.py 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)

+ 2
- 1
hc/front/views.py 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"]


Loading…
Cancel
Save