diff --git a/kairon/chat/handlers/channels/whatsapp.py b/kairon/chat/handlers/channels/whatsapp.py index 1f2a373d1..4f89f291b 100644 --- a/kairon/chat/handlers/channels/whatsapp.py +++ b/kairon/chat/handlers/channels/whatsapp.py @@ -51,7 +51,10 @@ async def message( elif message.get("type") == "text": text = message["text"]['body'] elif message.get("type") == "button": - text = message["button"]['text'] + if message["button"].get("payload"): + text = f"/k_quick_reply_msg{{\"{'quick_reply'}\": \"{message['button']['payload']}\"}}" + else: + text = message["button"]["text"] elif message.get("type") in {"image", "audio", "document", "video", "voice"}: if message['type'] == "voice": message['type'] = "audio" diff --git a/tests/integration_test/chat_service_test.py b/tests/integration_test/chat_service_test.py index f5913ac76..13f88bf7a 100644 --- a/tests/integration_test/chat_service_test.py +++ b/tests/integration_test/chat_service_test.py @@ -1972,7 +1972,7 @@ def _mock_validate_hub_signature(*args, **kwargs): assert actual == "success" time.sleep(5) assert len(whatsapp_msg_handler.call_args[0]) == 5 - assert whatsapp_msg_handler.call_args[0][1] == "buy now" + assert whatsapp_msg_handler.call_args[0][1] == '/k_quick_reply_msg{"quick_reply": "buy kairon for 1 billion"}' assert whatsapp_msg_handler.call_args[0][2] == "910123456789" metadata = whatsapp_msg_handler.call_args[0][3] metadata.pop("timestamp") @@ -1993,6 +1993,174 @@ def _mock_validate_hub_signature(*args, **kwargs): assert whatsapp_msg_handler.call_args[0][4] == bot +@responses.activate +def test_whatsapp_valid_button_message_request_without_payload_value(): + def _mock_validate_hub_signature(*args, **kwargs): + return True + responses.add("POST", "https://graph.facebook.com/v13.0/12345678/messages", json={}) + + with patch.object( + MessengerHandler, "validate_hub_signature", _mock_validate_hub_signature + ): + with mock.patch( + "kairon.chat.handlers.channels.whatsapp.Whatsapp._handle_user_message", + autospec=True, + ) as whatsapp_msg_handler: + response = client.post( + f"/api/bot/whatsapp/{bot}/{token}", + headers={"hub.verify_token": "valid"}, + json={ + "object": "whatsapp_business_account", + "entry": [ + { + "id": "WHATSAPP_BUSINESS_ACCOUNT_ID", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "910123456789", + "phone_number_id": "12345678", + }, + "contacts": [ + { + "profile": {"name": "udit"}, + "wa_id": "wa-123456789", + } + ], + "messages": [ + { + "context": { + "from": "910123456789", + "id": "wamid.HBgMOTE4MDk1MTAzMDIyFQIAERgSMDA3RkQTQxN0RBMDZEAA==" + }, + "from": "910123456789", + "id": "wappmsg.ID", + "timestamp": "21-09-2022 12:05:00", + "button": { + "text": "buy now", + "payload": None, + }, + "type": "button", + } + ], + }, + "field": "messages", + } + ], + } + ], + }, + ) + actual = response.json() + assert actual == "success" + time.sleep(5) + assert len(whatsapp_msg_handler.call_args[0]) == 5 + assert whatsapp_msg_handler.call_args[0][1] == 'buy now' + assert whatsapp_msg_handler.call_args[0][2] == "910123456789" + metadata = whatsapp_msg_handler.call_args[0][3] + metadata.pop("timestamp") + assert metadata == { + "context": {"from": "910123456789", "id": "wamid.HBgMOTE4MDk1MTAzMDIyFQIAERgSMDA3RkQTQxN0RBMDZEAA=="}, + "from": "910123456789", + "id": "wappmsg.ID", + "button": {"text": "buy now", "payload": None}, + "type": "button", + "is_integration_user": True, + "bot": bot, + "account": 1, + "channel_type": "whatsapp", + "tabname": "default", + "bsp_type": "meta", + "display_phone_number": "910123456789", + "phone_number_id": "12345678", + } + assert whatsapp_msg_handler.call_args[0][4] == bot + + +@responses.activate +def test_whatsapp_valid_button_message_request_without_payload_key(): + def _mock_validate_hub_signature(*args, **kwargs): + return True + responses.add("POST", "https://graph.facebook.com/v13.0/12345678/messages", json={}) + + with patch.object( + MessengerHandler, "validate_hub_signature", _mock_validate_hub_signature + ): + with mock.patch( + "kairon.chat.handlers.channels.whatsapp.Whatsapp._handle_user_message", + autospec=True, + ) as whatsapp_msg_handler: + response = client.post( + f"/api/bot/whatsapp/{bot}/{token}", + headers={"hub.verify_token": "valid"}, + json={ + "object": "whatsapp_business_account", + "entry": [ + { + "id": "WHATSAPP_BUSINESS_ACCOUNT_ID", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "910123456789", + "phone_number_id": "12345678", + }, + "contacts": [ + { + "profile": {"name": "udit"}, + "wa_id": "wa-123456789", + } + ], + "messages": [ + { + "context": { + "from": "910123456789", + "id": "wamid.HBgMOTE4MDk1MTAzMDIyFQIAERgSMDA3RkQTQxN0RBMDZEAA==" + }, + "from": "910123456789", + "id": "wappmsg.ID", + "timestamp": "21-09-2022 12:05:00", + "button": { + "text": "buy now", + }, + "type": "button", + } + ], + }, + "field": "messages", + } + ], + } + ], + }, + ) + actual = response.json() + assert actual == "success" + time.sleep(5) + assert len(whatsapp_msg_handler.call_args[0]) == 5 + assert whatsapp_msg_handler.call_args[0][1] == 'buy now' + assert whatsapp_msg_handler.call_args[0][2] == "910123456789" + metadata = whatsapp_msg_handler.call_args[0][3] + metadata.pop("timestamp") + assert metadata == { + "context": {"from": "910123456789", "id": "wamid.HBgMOTE4MDk1MTAzMDIyFQIAERgSMDA3RkQTQxN0RBMDZEAA=="}, + "from": "910123456789", + "id": "wappmsg.ID", + "button": {"text": "buy now"}, + "type": "button", + "is_integration_user": True, + "bot": bot, + "account": 1, + "channel_type": "whatsapp", + "tabname": "default", + "bsp_type": "meta", + "display_phone_number": "910123456789", + "phone_number_id": "12345678", + } + assert whatsapp_msg_handler.call_args[0][4] == bot + @responses.activate def test_whatsapp_valid_attachment_message_request(): def _mock_validate_hub_signature(*args, **kwargs):