From 5f908a01e4a37723e6d52235df047ee4e096ab3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 30 May 2018 15:24:12 +0300 Subject: [PATCH] When we don't recognize a message from Telegram, respond with 200 OK so Telegram doesn't keep retrying. --- hc/front/tests/test_add_telegram.py | 10 ++++++++-- hc/front/views.py | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/hc/front/tests/test_add_telegram.py b/hc/front/tests/test_add_telegram.py index d92f6a9e..f410bc7f 100644 --- a/hc/front/tests/test_add_telegram.py +++ b/hc/front/tests/test_add_telegram.py @@ -61,7 +61,7 @@ class AddTelegramTestCase(BaseTestCase): "message": {"chat": {"id": 123, "type": "group"}} })) - # bad message type + # bad chat type samples.append(json.dumps({ "message": { "chat": {"id": 123, "type": "invalid"}, @@ -73,4 +73,10 @@ class AddTelegramTestCase(BaseTestCase): r = self.client.post("/integrations/telegram/bot/", sample, content_type="application/json") - self.assertEqual(r.status_code, 400) + if sample == "": + # Bad JSON payload + self.assertEqual(r.status_code, 400) + else: + # JSON decodes but message structure not recognized + self.assertEqual(r.status_code, 200) + diff --git a/hc/front/views.py b/hc/front/views.py index 727fcb07..89313c06 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -838,7 +838,9 @@ def telegram_bot(request): except ValueError: return HttpResponseBadRequest() except jsonschema.ValidationError: - return HttpResponseBadRequest() + # We don't recognize the message format, but don't want Telegram + # retrying this over and over again, so respond with 200 OK + return HttpResponse() if "/start" not in doc["message"]["text"]: return HttpResponse()