示例#1
0
    def run(self, dispatcher, tracker, domain):
        global sessions

        current_detail = tracker.get_slot('detail')
        current_theme = find_theme(current_detail)
        sessions[tracker.sender_id] = current_theme

        reply_options = []
        for key in dict.kanban[current_theme]['details']:
            if key != current_detail:
                reply_options.append({
                    "text": key,
                    "reply": "Tell me more about " + key
                })

        dispatcher.utter_message(
            utils.prepare_action_response(
                action_type="explain",
                content=dict.kanban[current_theme]['details'][current_detail],
                action_name=self.name(),
                title=current_detail,
                reply_options=reply_options,
                slots=tracker.current_slot_values(),
                topic="kanban"))
        return []
示例#2
0
    def run(self, dispatcher, tracker, domain):
        global sessions

        intent = tracker.latest_message.parse_data['intent']['name']
        entities = tracker.latest_message.parse_data['entities']

        if intent == 'switch_kanban' and len(entities) == 0:
            current_theme = get_first_theme()
        elif (intent == 'switch_kanban'
              and len(entities) > 0) or intent == 'inform':
            current_theme = tracker.get_slot('theme')
        else:
            if tracker.sender_id in sessions:
                current_theme = sessions[tracker.sender_id]
            else:
                current_theme = get_first_theme()

        try:
            current_details = dict.kanban[current_theme]['details']
        except KeyError:
            current_details = None

        sessions[tracker.sender_id] = current_theme

        # declare reply options
        reply_options = []
        # check if there available options and add them to the reply options
        if current_details is not None:
            for key in current_details.keys():
                reply_options.append({
                    "text": key,
                    'reply': "Tell me more about " + key
                })

        # explain the current key
        dispatcher.utter_message(
            utils.prepare_action_response(
                action_type="explain",
                content=dict.kanban[current_theme]['general'],
                action_name=self.name(),
                title=current_theme,
                reply_options=reply_options,
                slots=tracker.current_slot_values(),
                topic="kanban"))
        return []
示例#3
0
    def run(self, dispatcher, tracker, domain):
        global sessions

        next_theme = get_next_theme(sessions[tracker.sender_id])

        if not next_theme:
            content = 'That is it for the crash course in kanban. Would you like to restart?'
            sessions[tracker.sender_id] = get_first_theme()
        else:
            content = 'Would you like to know about ' + next_theme + '?'
            sessions[tracker.sender_id] = next_theme

        reply_options = [{"text": "yes", "reply": "yes"}]

        dispatcher.utter_message(
            utils.prepare_action_response(action_type="continue",
                                          content=content,
                                          action_name=self.name(),
                                          reply_options=reply_options,
                                          slots=tracker.current_slot_values(),
                                          topic="kanban"))
        return []
    def converse(self, message, session_id):

        message = message.lower()

        if message == "reset dialogue":
            # Clean local session
            self.sessions[session_id] = Session()

            # Clean persistence session for all topics
            if self.persistence:
                for dialogue_topic in self.dialogue_models:
                    domain = TemplateDomain.load(dialogue_topic +
                                                 self.domain_path)
                    redis_tracker_store = RedisTrackerStoreAgentized(
                        domain=domain, host="redis-container")
                    redis_tracker_store.clean(session_id)

            # Create the JSON that would be returned to the user
            dialogue = [
                utils.prepare_action_response(
                    action_type="info",
                    content="The conversation history is successfully deleted")
            ]

            nlu_json_response = None

            dialogue_message = message
        else:
            # Parse user input
            nlu_json_response = self.interpreter.parse(message)
            intent = nlu_json_response['intent']
            entities = self.prepare_entities(nlu_json_response)

            # Select session
            if session_id not in self.sessions:
                self.sessions[session_id] = Session()

            current_dialogue_topic = self.sessions[
                session_id].get_current_dialogue_topic()

            if intent['confidence'] <= 0.77:
                dialogue_message = message
                dialogue = [
                    utils.prepare_action_response(
                        action_type="warning",
                        content="Sorry, I did not understand you!")
                ]
                return self.prepare_response(session_id, message,
                                             dialogue_message,
                                             nlu_json_response, dialogue)

            # Select current dialogue topic
            for dialogue_topic in self.dialogue_models:
                switching_intent = self.switching_intent_prefix + dialogue_topic

                if switching_intent == intent['name']:
                    if current_dialogue_topic != dialogue_topic:
                        current_dialogue_topic = dialogue_topic
                        # Reset dialogue model
                        self.dialogue_models[
                            current_dialogue_topic] = self.load_dialogue_model(
                                current_dialogue_topic)
                        if current_dialogue_topic in self.sessions[
                                session_id].get_active_topics():
                            # Ask user about restart or continue
                            dialogue_message = '_' + intent[
                                'name'] + '[' + ','.join(map(str,
                                                             entities)) + ']'
                            dialogue = [
                                utils.prepare_action_response(
                                    action_type="info",
                                    content=
                                    "Would you like to restart or continue?",
                                    reply_options=[{
                                        "text": "restart",
                                        "reply": "restart"
                                    }, {
                                        "text": "continue",
                                        "reply": "continue"
                                    }])
                            ]
                            # Save changes in session
                            self.sessions[
                                session_id].set_current_dialogue_topic(
                                    current_dialogue_topic)
                            self.sessions[session_id].set_reset_flag(True)
                            return self.prepare_response(
                                session_id, message, dialogue_message,
                                nlu_json_response, dialogue)
                        else:
                            self.sessions[session_id].add_active_topic(
                                current_dialogue_topic)

            # Handle user input
            if current_dialogue_topic is not None:
                if self.sessions[session_id].reset_flag:
                    if message == 'restart':
                        self.sessions[session_id].set_reset_flag(False)
                        dialogue_message = '_switch_' + current_dialogue_topic + '[]'
                        dialogue = self.dialogue_models[
                            current_dialogue_topic].handle_message(
                                text_message=dialogue_message,
                                sender_id=session_id)
                    elif message == 'continue':
                        self.sessions[session_id].set_reset_flag(False)
                        dialogue_message = '_continue[]'
                        dialogue = self.dialogue_models[
                            current_dialogue_topic].handle_message(
                                text_message=dialogue_message,
                                sender_id=session_id)
                    else:
                        dialogue_message = message
                        dialogue = [
                            utils.prepare_action_response(
                                action_type="warning",
                                content="Sorry, I did not understand you!")
                        ]
                else:
                    dialogue_message = '_' + intent['name'] + '[' + ','.join(
                        map(str, entities)) + ']'
                    dialogue = self.dialogue_models[
                        current_dialogue_topic].handle_message(
                            text_message=dialogue_message,
                            sender_id=session_id)
                # Save changes in session
                self.sessions[session_id].set_current_dialogue_topic(
                    current_dialogue_topic)
            else:
                dialogue_message = message
                dialogue = [
                    utils.prepare_action_response(
                        action_type="warning",
                        content="Sorry, I did not understand you!")
                ]

        return self.prepare_response(session_id, message, dialogue_message,
                                     nlu_json_response, dialogue)