Exemple #1
0
    def _flask_assitant_view_func(self, nlp_result=None, *args, **kwargs):
        if nlp_result:  # pass API query result directly
            self.request = nlp_result
        else:  # called as webhook
            self.request = self._dialogflow_request(verify=False)

        logger.debug(json.dumps(self.request, indent=2))

        try:
            self.intent = self.request["queryResult"]["intent"]["displayName"]
            self.context_in = self.request["queryResult"].get(
                "outputContexts", [])
            self.session_id = self._parse_session_id()
            assert self.session_id is not None
        except KeyError:
            raise DeprecationWarning(
                """It appears your agent is still using the Dialogflow V1 API,
                please update to V2 in the Dialogflow console.""")

        # update context_manager's assist reference
        # TODO: acces context_manager from assist, instead of own object
        self.context_manager._assist = self

        original_request = self.request.get("originalDetectIntentRequest")

        if original_request:
            payload = original_request.get("payload")
            if payload and payload.get("user"):
                self.user = original_request["payload"]["user"]
                self._set_user_profile()

        # Get access token from request
        if original_request and original_request.get("user"):
            self.access_token = original_request["user"].get("accessToken")

        self._update_contexts()
        self._dump_request()

        view_func = self._match_view_func()
        if view_func is None:
            logger.error("Failed to match an action function")
            return "", 400

        logger.info("Matched action function: {}".format(view_func.__name__))
        result = self._map_intent_to_view_func(view_func)()

        if result is not None:
            if isinstance(result, _Response):
                self._dump_result(view_func, result)
                resp = result.render_response()
                return resp
            return result
        logger.error("Action func returned empty response")
        return "", 400
def _errordump(obj, indent=2, default=None, cls=None):
    msg = json.dumps(obj, indent=indent, default=default, cls=cls)
    logger.error(msg)