Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whatsapp flows #1110

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion kairon/chat/handlers/channels/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ async def message(
# so quick reply should be checked first
if message.get("type") == "interactive":
interactive_type = message.get("interactive").get("type")
text = message["interactive"][interactive_type]["id"]
if interactive_type == "nfm_reply":
logger.debug(message["interactive"][interactive_type])
response_json = json.loads(message["interactive"][interactive_type]['response_json'])
response_json.update({"type": interactive_type})
entity = json.dumps({"flow_reply": response_json})
text = f"/k_interactive_msg{entity}"
else:
text = message["interactive"][interactive_type]["id"]
elif message.get("type") == "text":
text = message["text"]['body']
elif message.get("type") == "button":
Expand Down
1 change: 1 addition & 0 deletions kairon/shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class KaironSystemSlots(str, Enum):
document = "document"
doc_url = "doc_url"
order = "order"
flow_reply = "flow_reply"


class VectorEmbeddingsDatabases(str, Enum):
Expand Down
76 changes: 76 additions & 0 deletions tests/integration_test/chat_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,82 @@ def _mock_validate_hub_signature(*args, **kwargs):
assert whatsapp_msg_handler.call_args[0][4] == bot


@responses.activate
def test_whatsapp_valid_flows_message_request():
responses.reset()

def _mock_validate_hub_signature(*args, **kwargs):
return True

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:
request_json = {
'object': 'whatsapp_business_account',
'entry': [{
'id': '147142368486217',
'changes': [{
'value': {
'messaging_product': 'whatsapp',
'metadata': {
'display_phone_number': '918657011111',
'phone_number_id': '142427035629239'
},
'contacts': [{
'profile': {
'name': 'Mahesh'
},
'wa_id': '919515991111'
}],
'messages': [{
'context': {
'from': '918657011111',
'id': 'wamid.HBgMOTE5NTE1OTkxNjg1FQIAERgSMjVGRjYwODI3RkMyOEQ0NUM1AA=='
},
'from': '919515991111',
'id': 'wamid.HBgMOTE5NTE1OTkxNjg1FQIAEhggQTRBQUYyODNBQkMwNEIzRDQ0MUI1ODkyMTE2NTMA',
'timestamp': '1703257297',
'type': 'interactive',
'interactive': {
'type': 'nfm_reply',
'nfm_reply': {
'response_json': '{"flow_token":"AQBBBBBCS5FpgQ_cAAAAAD0QI3s.","firstName":"Mahesh ","lastName":"Sattala ","pincode":"523456","district":"Bangalore ","houseNumber":"5-6","dateOfBirth":"1703257240046","source":"SOCIAL_MEDIA","landmark":"HSR Layout ","email":"[email protected]"}',
'body': 'Sent',
'name': 'flow'
}
}
}]
},
'field': 'messages'
}]
}]
}
response = client.post(
f"/api/bot/whatsapp/{bot}/{token}",
headers={"hub.verify_token": "valid"},
json=request_json
)
actual = response.json()
assert actual == 'success'
assert len(whatsapp_msg_handler.call_args[0]) == 5
assert whatsapp_msg_handler.call_args[0][1] == '/k_interactive_msg{\"flow_reply\": {\"flow_token\": \"AQBBBBBCS5FpgQ_cAAAAAD0QI3s.\", \"firstName\": \"Mahesh \", \"lastName\": \"Sattala \", \"pincode\": \"523456\", \"district\": \"Bangalore \", \"houseNumber\": \"5-6\", \"dateOfBirth\": \"1703257240046\", \"source\": \"SOCIAL_MEDIA\", \"landmark\": \"HSR Layout \", \"email\": \"[email protected]\", \"type\": \"nfm_reply\"}}'
assert whatsapp_msg_handler.call_args[0][2] == '919515991111'
metadata = whatsapp_msg_handler.call_args[0][3]
metadata.pop("timestamp")
assert metadata == {
'context': {'from': '918657011111', 'id': 'wamid.HBgMOTE5NTE1OTkxNjg1FQIAERgSMjVGRjYwODI3RkMyOEQ0NUM1AA=='},
'from': '919515991111', 'id': 'wamid.HBgMOTE5NTE1OTkxNjg1FQIAEhggQTRBQUYyODNBQkMwNEIzRDQ0MUI1ODkyMTE2NTMA',
'type': 'interactive',
'interactive': {
'type': 'nfm_reply', 'nfm_reply': {
'response_json': '{"flow_token":"AQBBBBBCS5FpgQ_cAAAAAD0QI3s.","firstName":"Mahesh ","lastName":"Sattala ","pincode":"523456","district":"Bangalore ","houseNumber":"5-6","dateOfBirth":"1703257240046","source":"SOCIAL_MEDIA","landmark":"HSR Layout ","email":"[email protected]"}',
'body': 'Sent', 'name': 'flow'}},
'is_integration_user': True, 'bot': bot, 'account': 1, 'channel_type': 'whatsapp',
'bsp_type': 'meta', 'tabname': 'default', 'display_phone_number': '918657011111',
'phone_number_id': '142427035629239'}
assert whatsapp_msg_handler.call_args[0][4] == bot


@responses.activate
def test_whatsapp_valid_statuses_with_sent_request():
from kairon.shared.chat.data_objects import ChannelLogs
Expand Down
7 changes: 4 additions & 3 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2911,7 +2911,7 @@ def test_list_entities_empty():
)
actual = response.json()
assert actual["error_code"] == 0
assert len(actual['data']) == 8
assert len(actual['data']) == 9
assert actual["success"]


Expand Down Expand Up @@ -3036,7 +3036,8 @@ def test_list_entities():
assert actual["error_code"] == 0
assert {e['name'] for e in actual["data"]} == {'bot', 'file', 'category', 'file_text', 'ticketid', 'file_error',
'priority', 'requested_slot', 'fdresponse', 'kairon_action_response',
'audio', 'image', 'doc_url', 'document', 'video', 'order'}
'audio', 'image', 'doc_url', 'document', 'video', 'order',
'flow_reply'}
assert actual["success"]


Expand Down Expand Up @@ -3437,7 +3438,7 @@ def test_get_slots():
)
actual = response.json()
assert "data" in actual
assert len(actual["data"]) == 15
assert len(actual["data"]) == 16
assert actual["success"]
assert actual["error_code"] == 0
assert Utility.check_empty_string(actual["message"])
Expand Down
Loading