Skip to content

Commit

Permalink
Quick reply payload (#1202)
Browse files Browse the repository at this point in the history
* 1. Added the code related to quick reply button, where we capture payload.
2. Added and fixed test cases regarding the same.

* 1. Added the code related to quick reply button, where we capture payload.
2. Added and fixed test cases regarding the same.

* 1. Added the code related to quick reply button, where we capture payload.
2. Added and fixed test cases regarding the same.
  • Loading branch information
GMayank0310 authored May 14, 2024
1 parent d5da3d3 commit 55536fa
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
5 changes: 4 additions & 1 deletion kairon/chat/handlers/channels/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
170 changes: 169 additions & 1 deletion tests/integration_test/chat_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
Expand Down

0 comments on commit 55536fa

Please sign in to comment.