Exemple #1
0
 def __execute_request(self, app, parameters_dict):
     if app.get_integration_type() == IntegrationType.Module:
         class_name = app.get_impl()
         module = self.__get_module_by_class_name(class_name)
         answer = module.run(self, parameters_dict)
     elif not self.__is_stub_mode:
         url = app.get_endpoint_url()
         try:
             response = requests.post(url, data=json.dumps(parameters_dict))
             if response.status_code == 200:
                 response_dict = response.json()
                 message_source: Dict[str,
                                      str] = response_dict.get("answer")
                 if not message_source:
                     message_source = response_dict.get("error")
                 answer = AssistantAnswer(
                     message_source["message_key"],
                     message_str=message_source["message"])
             else:
                 answer = AssistantAnswer(
                     mc.ERROR_RESPONSE_CODE,
                     parameters_dict={"code": response.status_code})
         except Exception:
             answer = AssistantAnswer(mc.SERVICE_DOES_NOT_WORK)
     else:
         answer = "AppName: " + app.get_name()
         for key, value in parameters_dict.items():
             answer += "| " + key + "=" + value
         answer = AssistantAnswer(None, message_str=answer)
     return answer
Exemple #2
0
    def turn(self, assistant, parameters_dict):
        answer = None
        if self.is_started:
            amount_to_remove = int(parameters_dict["Amount"])
            if 1 <= amount_to_remove <= 3:
                self.amount_of_matches -= amount_to_remove
                bot_choice = self.bot.move(self.amount_of_matches)
                if self.amount_of_matches < 2:
                    answer = AssistantAnswer("matches_game.win")
                    self.is_started = False
                else:
                    self.amount_of_matches -= bot_choice
                    if self.amount_of_matches < 2:
                        answer = AssistantAnswer("matches_game.lose")
                        self.is_started = False
                    else:
                        params = {
                            "matches": self.amount_of_matches,
                            "amount": bot_choice
                        }
                        answer = AssistantAnswer(
                            "matches_game.amount_of_matches", params)
            else:
                answer = AssistantAnswer("matches_game.wrong_input_amount")

        return answer
Exemple #3
0
 def mark(self, dialog_step: int, mark: int) -> AssistantAnswer:
     step_desc: Dict[str, Any] = self.__history.get(dialog_step)
     answer = None
     if step_desc and step_desc[Assistant.__MARK] == -1:
         step_desc[Assistant.__MARK] = mark
         answer = AssistantAnswer(mc.FEEDBACK)
         formatted_answer = self.format_answer(answer)
         answer.message = formatted_answer
     return answer
Exemple #4
0
 def start(self, assistant, parameters_dict):
     self.amount_of_matches = int(parameters_dict.get(
         "AmountOfMatches", 30))
     self.is_started = True
     is_bot_first = random.randint(0, 1)
     if is_bot_first:
         bot_choice = self.bot.move(self.amount_of_matches)
         was_matches = self.amount_of_matches
         self.amount_of_matches -= bot_choice
         param = {"matches": was_matches, "amount": bot_choice}
         answer = AssistantAnswer("matches_game.start_game.bot_first",
                                  param)
     else:
         answer = AssistantAnswer("matches_game.start_game.user_first",
                                  {"matches": self.amount_of_matches})
     return answer
 def start(self, assistant, parameters_dict):
     self.is_started = True
     self.__game = XOLogics(font_path=self.__config[TicTacToeFontPath])
     board = self.__game.get_image_board()
     if self.__game.is_bot_first:
         message_key = "XO.start_game_key.bot_turn"
     else:
         message_key = "XO.start_game_key.user_turn"
     return AssistantAnswer(message_key, picture=board)
Exemple #6
0
 def run(self, assistant, parameters_dict):
     intent = parameters_dict[FORM_ACTION_NAME]
     answer = None
     if intent == "Ability demonstration":
         answer = self.ability_demonstration(assistant, parameters_dict)
     elif intent == "Say hi":
         answer = AssistantAnswer(mc.HI_MESSAGE)
     elif intent == "Say goodbye":
         answer = AssistantAnswer(mc.GOODBYE_MESSAGE)
     elif intent == "My affairs":
         answer = AssistantAnswer(mc.MY_AFFAIRS)
     elif intent == "Current time":
         time = datetime.datetime.now()
         params = {
             "hours": time.hour,
             "minutes": time.minute,
             "seconds": time.second
         }
         answer = AssistantAnswer(mc.CURRENT_TIME, parameters_dict=params)
     return answer
Exemple #7
0
    def process_request(self, user_request_str):
        dest_lang = self.language_model.language_code
        if self.__user_defined_lang != dest_lang:
            user_request_str = self._translate_module.translate(
                user_request_str, self.__user_defined_lang, dest_lang)

        request_information = self.language_model.parse(user_request_str)

        app, intent_description = self.__extract_app(request_information)

        if app is None or intent_description is None:
            if len(self.__stack) > 0:
                form = self.__stack.pop(0)
                app = form.get_app()
                answer = self.__process_intent(app, request_information, form)
            elif self.__game_app is not None:
                class_name = self.__game_app.get_impl()
                module = self.__get_module_by_class_name(class_name)
                if module.is_active:
                    app = self.__game_app
                    intent_description = self.__game_app.get_intent_by_name(
                        GAME_TURN_INTENT_NAME)
                    form = Form(app, intent_description)
                    answer = self.__process_intent(app, request_information,
                                                   form)
                else:
                    answer = AssistantAnswer(mc.DID_NOT_UNDERSTAND)
            else:
                answer = AssistantAnswer(mc.DID_NOT_UNDERSTAND)
        else:
            if app.get_intent_by_name(GAME_TURN_INTENT_NAME) is not None:
                self.__game_app = app
            form = Form(app, intent_description)
            answer = self.__process_intent(app, request_information, form)

        if answer is None:
            answer = AssistantAnswer(mc.DID_NOT_UNDERSTAND)
        formatted_answer = self.format_answer(answer)
        answer.message = formatted_answer

        step_desc: Dict[str, Any] = {
            Assistant.__REQUEST: user_request_str,
            Assistant.__FORMATTED_ANSWER: formatted_answer,
            Assistant.__ANSWER_KEY: answer.message_key,
            Assistant.__MARK: Assistant.__NO_MARK,
            Assistant.__DIALOG_STEP: self.__dialog_step
        }
        self.__history[self.__dialog_step] = step_desc
        answer.dialog_step = self.__dialog_step
        self.__dialog_step += 1
        return answer
Exemple #8
0
 def start(self, assistant, parameters_dict):
     self.is_started = True
     self.__game = TicTacToeLogic(
         font_path=self.__config[TicTacToeFontPath])
     board = self.__game.get_image_board()
     param = dict()
     param["label"] = self.__game.plr_fig
     if self.__game.is_bot_first:
         message_key = "tictactoe.start_game_key.bot_turn"
     else:
         message_key = "tictactoe.start_game_key.user_turn"
     return AssistantAnswer(message_key, param, picture=board)
Exemple #9
0
 def ability_demonstration(self, assistant, parameters_dict):
     apps = assistant.application_dict
     lines = []
     for app_name, app_desc in apps.items():
         lines.append(
             SelfIntroductionModule.HEAD_PATTERN.format(
                 app_desc.get_name(), app_desc.get_description()))
         for intent in app_desc.get_intents_list():
             desc = intent.description
             if desc is None:
                 line = SelfIntroductionModule.INTENT_PATTERN.format(
                     intent.get_name())
             else:
                 line = SelfIntroductionModule.INTENT_WITH_DESC_PATTERN.format(
                     intent.get_name(), desc)
             lines.append(line)
     lines = "\n".join(lines)
     answer = AssistantAnswer(mc.INTRODUCTION_MESSAGE,
                              parameters_dict={"desc": lines})
     return answer
    def turn(self, assistant, parameters_dict):
        pos = parameters_dict["Position"].lower()
        self.__game.move(pos)
        game_status, xo_message_key = self.__game.get_status()
        board = self.__game.get_image_board()
        par = {}
        if game_status == GameStatus.PLAYING:
            message_key = "XO_bot_turn"
        elif game_status == GameStatus.WIN:
            message_key = "XO_win"
            self.is_started = False
        elif game_status == GameStatus.LOSE:
            message_key = "XO_lose"
            self.is_started = False
        elif game_status == GameStatus.ERROR:
            message_key = xo_message_key
        else:
            message_key = "XO_draw_error"

        return AssistantAnswer(message_key, par, picture=board)
Exemple #11
0
    def turn(self, assistant, parameters_dict):
        pos = parameters_dict["Position"].lower()
        x_pos = (ord(pos[0]) - ord("a"))
        y_pos = int(pos[1]) - 1
        self.__game.move((x_pos, y_pos))
        game_status = self.__game.get_status()[0]
        board = self.__game.get_image_board()
        par = {}
        if game_status == GameStatus.PLAYING:
            message_key = "tictactoe_bot_turn"
        elif game_status == GameStatus.WIN:
            message_key = "tictactoe_win"
            self.is_started = False
        elif game_status == GameStatus.LOSE:
            message_key = "tictactoe_lose"
            self.is_started = False
        elif game_status == GameStatus.ERROR:
            message_key = "tictactoe_error"
            error_message = self.__game.get_status()[1]
            par["message"] = error_message
        else:
            message_key = "tictactoe_draw_error"

        return AssistantAnswer(message_key, par, picture=board)
 def run(self, assistant, parameters_dict):
     language = parameters_dict["language"].lower()
     assistant.user_defined_language = LanguageSelector.lang_map[language]
     param = {"language": language}
     return AssistantAnswer("Language_module.set_up_language",
                            parameters_dict=param)