Skip to content

Commit

Permalink
Database action fix for stories (#1100)
Browse files Browse the repository at this point in the history
* 1. Added database action in story.
2. Added unit test case for the same.

* 1. Added database action in story.
2. Added unit test case for the same.
3. Added 201 status code in execute_http_request for 360Dialog Template creation.

* 1. Added database action in story.
2. Added unit test case for the same.
3. Added 201 status code in execute_http_request for 360Dialog Template creation.

* 1. Added database action in story.
2. Added unit test case for the same.
3. Added 201 status code in execute_http_request for 360Dialog Template creation.

* Fixed test case.

---------

Co-authored-by: Nupur Khare <[email protected]>
Co-authored-by: udit_pandey <[email protected]>
  • Loading branch information
3 people authored Dec 28, 2023
1 parent cc42c90 commit ef69c62
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
3 changes: 2 additions & 1 deletion kairon/shared/channels/whatsapp/bsp/dialog360.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def add_template(self, data: Dict, bot: Text, user: Text):
headers = {"Authorization": BSP360Dialog.get_partner_auth_token()}
url = f"{base_url}{template_endpoint}"
resp = Utility.execute_http_request(request_method="POST", http_url=url, request_body=data, headers=headers,
validate_status=True, err_msg="Failed to add template: ")
validate_status=True, err_msg="Failed to add template: ",
expected_status_code=201)
UserActivityLogger.add_log(a_type=UserActivityType.template_creation.value, email=user, bot=bot, message=['Template created!'])
return resp
except DoesNotExist as e:
Expand Down
3 changes: 3 additions & 0 deletions kairon/shared/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,7 @@ def get_stories(self, bot: Text):
razorpay_actions = set(RazorpayAction.objects(bot=bot, status=True).values_list('name'))
email_actions = set(EmailActionConfig.objects(bot=bot, status=True).values_list('action_name'))
prompt_actions = set(PromptAction.objects(bot=bot, status=True).values_list('name'))
database_actions = set(DatabaseAction.objects(bot=bot, status=True).values_list('name'))
web_search_actions = set(WebSearchAction.objects(bot=bot, status=True).values_list('name'))
forms = set(Forms.objects(bot=bot, status=True).values_list('name'))
data_list = list(Stories.objects(bot=bot, status=True))
Expand Down Expand Up @@ -2654,6 +2655,8 @@ def get_stories(self, bot: Text):
step['type'] = StoryStepType.two_stage_fallback_action.value
elif event['name'] in prompt_actions:
step['type'] = StoryStepType.prompt_action.value
elif event['name'] in database_actions:
step['type'] = StoryStepType.database_action.value
elif event['name'] in web_search_actions:
step['type'] = StoryStepType.web_search_action.value
elif str(event['name']).startswith("utter_"):
Expand Down
1 change: 1 addition & 0 deletions kairon/shared/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class StoryStepType(str, Enum):
two_stage_fallback_action = "TWO_STAGE_FALLBACK_ACTION"
pyscript_action = "PYSCRIPT_ACTION"
prompt_action = "PROMPT_ACTION"
database_action = "DATABASE_ACTION"
web_search_action = "WEB_SEARCH_ACTION"


Expand Down
2 changes: 1 addition & 1 deletion tests/integration_test/chat_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ def _mock_validate_hub_signature(*args, **kwargs):
def test_whatsapp_valid_attachment_message_request():
def _mock_validate_hub_signature(*args, **kwargs):
return True

responses.reset()
responses.add(
"POST", "https://graph.facebook.com/v13.0/12345678/messages", json={}
)
Expand Down
24 changes: 12 additions & 12 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4372,9 +4372,9 @@ def test_add_story_invalid_event_type():
assert actual["error_code"] == 422
assert (
actual["message"]
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION']},
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION']},
'loc': ['body', 'steps', 0, 'type'],
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION'",
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION'",
'type': 'type_error.enum'}]
)

Expand Down Expand Up @@ -4632,11 +4632,11 @@ def test_add_multiflow_story_invalid_event_type():
"'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', "
"'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', "
"'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', "
"'PROMPT_ACTION', 'WEB_SEARCH_ACTION'",
"'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION'",
'type': 'type_error.enum', 'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT',
'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION',
'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION',
'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION']}
'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION']}
}]
)

Expand Down Expand Up @@ -4702,9 +4702,9 @@ def test_update_story_invalid_event_type():
assert actual["error_code"] == 422
assert (
actual["message"]
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION']},
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION']},
'loc': ['body', 'steps', 0, 'type'],
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION'",
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION'",
'type': 'type_error.enum'}]
)

Expand Down Expand Up @@ -4840,12 +4840,12 @@ def test_update_multiflow_story_invalid_event_type():
"'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', "
"'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', "
"'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', "
"'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION'",
"'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION'",
'type': 'type_error.enum', 'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END',
'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION',
'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION',
'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION',
'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION']}
'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION']}
}]
)

Expand Down Expand Up @@ -9257,9 +9257,9 @@ def test_add_rule_invalid_event_type():
assert actual["error_code"] == 422
assert (
actual["message"]
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION']},
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION']},
'loc': ['body', 'steps', 0, 'type'],
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION'",
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION'",
'type': 'type_error.enum'}]
)

Expand Down Expand Up @@ -9322,9 +9322,9 @@ def test_update_rule_invalid_event_type():
assert actual["error_code"] == 422
assert (
actual["message"]
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION']},
== [{'ctx': {'enum_values': ['INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION']},
'loc': ['body', 'steps', 0, 'type'],
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'WEB_SEARCH_ACTION'",
'msg': "value is not a valid enumeration member; permitted: 'INTENT', 'SLOT', 'FORM_START', 'FORM_END', 'BOT', 'HTTP_ACTION', 'ACTION', 'SLOT_SET_ACTION', 'FORM_ACTION', 'GOOGLE_SEARCH_ACTION', 'EMAIL_ACTION', 'JIRA_ACTION', 'ZENDESK_ACTION', 'PIPEDRIVE_LEADS_ACTION', 'HUBSPOT_FORMS_ACTION', 'RAZORPAY_ACTION', 'TWO_STAGE_FALLBACK_ACTION', 'PYSCRIPT_ACTION', 'PROMPT_ACTION', 'DATABASE_ACTION', 'WEB_SEARCH_ACTION'",
'type': 'type_error.enum'}]
)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit_test/channels/bsp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def _get_partners_auth_token(*args, **kwargs):

base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
url = f"{base_url}/v1/partners/{partner_id}/waba_accounts/{waba_account_id}/waba_templates"
responses.add("POST", json=api_resp, url=url)
responses.add("POST", json=api_resp, url=url, status=201)
template = BSP360Dialog(bot, "test").add_template(data, bot, "test")
assert template == {'category': 'MARKETING', 'id': '594425479261596', 'status': 'PENDING'}
count = AuditLogData.objects(attributes=[{"key": "bot", "value": bot}], user="test", action="activity").count()
Expand Down
21 changes: 21 additions & 0 deletions tests/unit_test/data_processor/data_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9622,6 +9622,27 @@ def test_add_vector_embedding_action_config_op_embedding_search(self):
assert actual_vectordb_action['query_type'] == 'embedding_search'
assert actual_vectordb_action['response']['value'] == '0'

def test_add_vector_embedding_action_with_story(self):
processor = MongoProcessor()
bot = 'test_vector_bot'
user = 'test_vector_user'
steps = [
{"name": "helu", "type": "INTENT"},
{"name": "test_vectordb_action_op_embedding_search", "type": "DATABASE_ACTION"},
]
story_dict = {'name': "story with vector embedding action", 'steps': steps, 'type': 'STORY', 'template_type': 'CUSTOM'}
story_id = processor.add_complex_story(story_dict, bot, user)
story = Stories.objects(block_name="story with vector embedding action", bot=bot,
events__name='test_vectordb_action_op_embedding_search', status=True).get()
assert story.events[1].type == 'action'
stories = list(processor.get_stories(bot))
story_with_form = [s for s in stories if s['name'] == 'story with vector embedding action']
assert story_with_form[0]['steps'] == [
{"name": "helu", "type": "INTENT"},
{"name": "test_vectordb_action_op_embedding_search", "type": "DATABASE_ACTION"},
]
processor.delete_complex_story(story_id, 'STORY', bot, user)

def test_add_vector_embedding_action_config_op_payload_search(self):
processor = MongoProcessor()
bot = 'test_vector_bot'
Expand Down

0 comments on commit ef69c62

Please sign in to comment.