From fc176cd832c6c34a0cc7259a0fda1d723ddc5fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 18 Jan 2018 00:58:19 +0200 Subject: [PATCH] `settelegramwebhook` now sends a correct "allowed_updates" field. --- .../management/commands/settelegramwebhook.py | 8 ++++++-- hc/front/schemas.py | 2 +- hc/front/tests/test_add_telegram.py | 20 ++----------------- hc/front/views.py | 3 +-- 4 files changed, 10 insertions(+), 23 deletions(-) diff --git a/hc/api/management/commands/settelegramwebhook.py b/hc/api/management/commands/settelegramwebhook.py index 0da3ed19..f0f668be 100644 --- a/hc/api/management/commands/settelegramwebhook.py +++ b/hc/api/management/commands/settelegramwebhook.py @@ -14,9 +14,13 @@ class Command(BaseCommand): if settings.TELEGRAM_TOKEN is None: return "Abort: settings.TELEGRAM_TOKEN is not set" - form = {"url": settings.SITE_ROOT + reverse("hc-telegram-webhook")} + form = { + "url": settings.SITE_ROOT + reverse("hc-telegram-webhook"), + "allowed_updates": ["message"] + } + url = SETWEBHOOK_TMPL % settings.TELEGRAM_TOKEN - r = requests.post(url, data=form) + r = requests.post(url, json=form) if r.status_code != 200: return "Fail: status=%d, %s" % (r.status_code, r.content) diff --git a/hc/front/schemas.py b/hc/front/schemas.py index f940bf08..7bbd976b 100644 --- a/hc/front/schemas.py +++ b/hc/front/schemas.py @@ -16,7 +16,7 @@ telegram_callback = { }, "text": {"type": "string"} }, - "required": ["chat"] + "required": ["chat", "text"] } }, "required": ["message"] diff --git a/hc/front/tests/test_add_telegram.py b/hc/front/tests/test_add_telegram.py index b72ffd47..d92f6a9e 100644 --- a/hc/front/tests/test_add_telegram.py +++ b/hc/front/tests/test_add_telegram.py @@ -56,12 +56,9 @@ class AddTelegramTestCase(BaseTestCase): def test_bot_handles_bad_message(self, mock_get): samples = ["", "{}"] - # text is not string + # text is missing samples.append(json.dumps({ - "message": { - "chat": {"id": 123, "type": "invalid"}, - "text": 123 - } + "message": {"chat": {"id": 123, "type": "group"}} })) # bad message type @@ -77,16 +74,3 @@ 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) diff --git a/hc/front/views.py b/hc/front/views.py index d3091f8e..9f8eb298 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -823,8 +823,7 @@ def telegram_bot(request): except jsonschema.ValidationError: return HttpResponseBadRequest() - text = doc["message"].get("text", "") - if "/start" not in text: + if "/start" not in doc["message"]["text"]: return HttpResponse() chat = doc["message"]["chat"]