Exemplo n.º 1
0
    async def __process_action(dispatcher: CollectingDispatcher,
                               tracker: Tracker,
                               domain: Dict[Text, Any], action) -> List[Dict[Text, Any]]:
        slots = {}
        action_type = None
        try:
            logger.info(tracker.current_slot_values())
            intent = tracker.get_intent_of_latest_message()
            logger.info("intent: " + str(intent))
            logger.info("tracker.latest_message: " + str(tracker.latest_message))
            bot_id = tracker.get_slot("bot")
            if ActionUtility.is_empty(bot_id) or ActionUtility.is_empty(action):
                raise ActionFailure("Bot id and action name not found in slot")

            action_config, action_type = ActionUtility.get_action_config(bot=bot_id, name=action)
            if action_type == ActionType.http_action.value:
                slots = await ActionProcessor.__process_http_action(tracker, action_config)
                dispatcher.utter_message(slots.get(KAIRON_ACTION_RESPONSE_SLOT))
            elif action_type == ActionType.slot_set_action.value:
                slots = await ActionProcessor.__process_slot_set_action(tracker, action_config)
            elif action_type == ActionType.form_validation_action.value:
                slots = await ActionProcessor.__process_form_validation_action(dispatcher, tracker, action_config)
            elif action_type == ActionType.email_action.value:
                slots = await ActionProcessor.__process_email_action(dispatcher, tracker, action_config)
            elif action_type == ActionType.google_search_action.value:
                slots = await ActionProcessor.__process_google_search_action(dispatcher, tracker, action_config)
            elif action_type == ActionType.jira_action.value:
                slots = await ActionProcessor.__process_jira_action(dispatcher, tracker, action_config)
            elif action_type == ActionType.zendesk_action.value:
                slots = await ActionProcessor.__process_zendesk_action(dispatcher, tracker, action_config)
            elif action_type == ActionType.pipedrive_leads_action.value:
                slots = await ActionProcessor.__process_pipedrive_leads_action(dispatcher, tracker, action_config)
            return [SlotSet(slot, value) for slot, value in slots.items()]
        except Exception as e:
            logger.exception(e)
            ActionServerLogs(
                type=action_type,
                intent=tracker.get_intent_of_latest_message(),
                action=action,
                sender=tracker.sender_id,
                exception=str(e),
                bot=tracker.get_slot("bot"),
                status="FAILURE"
            ).save()
Exemplo n.º 2
0
    async def __process_http_action(dispatcher: CollectingDispatcher,
                                    tracker: Tracker, domain: Dict[Text, Any],
                                    action) -> List[Dict[Text, Any]]:
        bot_response = None
        http_response = None
        exception = None
        request_body = None
        status = "SUCCESS"
        http_url = None
        request_method = None
        try:
            logger.info(tracker.current_slot_values())
            intent = tracker.get_intent_of_latest_message()
            logger.info("intent: " + str(intent))
            logger.info("tracker.latest_message: " +
                        str(tracker.latest_message))
            bot_id = tracker.get_slot("bot")
            if ActionUtility.is_empty(bot_id) or ActionUtility.is_empty(
                    action):
                raise HttpActionFailure(
                    "Bot id and HTTP action configuration name not found in slot"
                )

            http_action_config: HttpActionConfig = ActionUtility.get_http_action_config(
                bot=bot_id, action_name=action)
            request_body = ActionUtility.prepare_request(
                tracker, http_action_config['params_list'])
            logger.info("request_body: " + str(request_body))
            request_method = http_action_config['request_method']
            http_response, http_url = ActionUtility.execute_http_request(
                auth_token=http_action_config['auth_token'],
                http_url=http_action_config['http_url'],
                request_method=request_method,
                request_body=request_body)
            logger.info("http response: " + str(http_response))

            bot_response = ActionUtility.prepare_response(
                http_action_config['response'], http_response)
            logger.info("response: " + str(bot_response))
        #  deepcode ignore W0703: General exceptions are captured to raise application specific exceptions
        except HttpActionFailure as e:
            exception = str(e)
            logger.exception(e)
            status = "FAILURE"
            bot_response = "I have failed to process your request"
        except Exception as e:
            exception = str(e)
            logger.exception(e)
            status = "FAILURE"
            bot_response = "I have failed to process your request"
        finally:
            dispatcher.utter_message(bot_response)
            HttpActionLog(
                intent=tracker.get_intent_of_latest_message(),
                action=action,
                sender=tracker.sender_id,
                url=http_url,
                request_params=None if request_method
                and request_method.lower() == "get" else request_body,
                api_response=str(http_response),
                bot_response=str(bot_response),
                exception=exception,
                bot=tracker.get_slot("bot"),
                status=status).save()

        return [SlotSet(KAIRON_ACTION_RESPONSE_SLOT, bot_response)]