Repository: line/line-bot-sdk-python
Pull request #187 (2019-06-21 07:59:02)

Fix destination field
#186


Patch:
diff --git a/linebot/webhook.py b/linebot/webhook.py
index f9d1f0f..6e9aa41 100644
--- a/linebot/webhook.py
+++ b/linebot/webhook.py
@@ -172,7 +172,7 @@ class WebhookParser(object):
                 LOGGER.warn('Unknown event type. type=' + event_type)
 
         if as_payload:
-            return WebhookPayload(events=events, destination=body_json['destination'])
+            return WebhookPayload(events=events, destination=body_json.get('destination'))
         else:
             return events
 


Test patch:
diff --git a/tests/test_webhook.py b/tests/test_webhook.py
index 57cf54c..0a4fa0e 100644
--- a/tests/test_webhook.py
+++ b/tests/test_webhook.py
@@ -48,17 +48,19 @@ class TestSignatureValidator(unittest.TestCase):
 
 
 class TestWebhookParser(unittest.TestCase):
+    def setUp(self):
+        parser = WebhookParser('channel_secret')
+        # mock
+        parser.signature_validator.validate = lambda a, b: True
+        self.parser = parser
+
     def test_parse(self):
         file_dir = os.path.dirname(__file__)
         webhook_sample_json_path = os.path.join(file_dir, 'text', 'webhook.json')
         with open(webhook_sample_json_path) as fp:
             body = fp.read()
 
-        parser = WebhookParser('channel_secret')
-        # mock
-        parser.signature_validator.validate = lambda a, b: True
-
-        events = parser.parse(body, 'channel_secret')
+        events = self.parser.parse(body, 'channel_secret')
 
         # MessageEvent, SourceUser, TextMessage
         self.assertIsInstance(events[0], MessageEvent)
@@ -387,6 +389,45 @@ class TestWebhookParser(unittest.TestCase):
         self.assertIsInstance(events[24].things.result.action_results[1], ActionResult)
         self.assertEqual(events[24].things.result.action_results[1].type, 'void')
 
+    def test_parse_webhook_req_without_destination(self):
+        body = """
+        {
+            "events": [
+                {
+                    "replyToken": "00000000000000000000000000000000",
+                    "type": "message",
+                    "timestamp": 1561099010135,
+                    "source": {
+                        "type": "user",
+                        "userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
+                    },
+                    "message": {
+                        "id": "100001",
+                        "type": "text",
+                        "text": "Hello, world"
+                    }
+                },
+                {
+                    "replyToken": "ffffffffffffffffffffffffffffffff",
+                    "type": "message",
+                    "timestamp": 1561099010135,
+                    "source": {
+                        "type": "user",
+                        "userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
+                    },
+                    "message": {
+                        "id": "100002",
+                        "type": "sticker",
+                        "packageId": "1",
+                        "stickerId": "1"
+                    }
+                }
+            ]
+        }
+        """
+        payload = self.parser.parse(body=body, signature='channel_secret', as_payload=True)
+        self.assertEqual(None, payload.destination)
+
 
 class TestWebhookHandler(unittest.TestCase):
     def setUp(self):