Esempio n. 1
0
    def next_dialog(self, answer):
        """
        :param answer: user answer
        :return: message for user
        """
        if self.schema is None:
            return msg_code.get_error(1, self.__lang)
        all_nodes = self.schema.nodes
        all_edges = self.schema.edges
        # Получаем список ребер из этой вершины
        edges = self.schema.get_edges(self.schema.current_node_id)
        # Must be one or more edges else error in dot file
        if len(edges) == 1:
            # Node have one edges, go to next node
            self.schema.current_node_id = edges[0][1]
        elif len(edges) > 1:
            # Node have two or more edges, need answer analise
            # //TODO answer переделать в цифру и именьшить на 1
            if not answer.isdigit():
                return msg_code.get_error(22, self.__lang)
            answer = int(answer)
            self.schema.current_node_id = edges[answer - 1][1]
        else:
            if self.schema.current_node_id != 'End':
                msg = msg_code.get_error(20, self.__lang) \
                      % (self.schema.current_node_id, all_nodes[self.schema.current_node_id])
                return msg

        msg = all_nodes[self.schema.current_node_id].replace(r'\n', ' ')
        # List edges for this node
        edges = self.schema.get_edges(self.schema.current_node_id)
        if len(edges) == 1:
            # Only one edge, don't need choice
            msg += "\n" + msg_code.get_error(19, self.__lang)
        else:
            # User must make choice
            for i in range(1, len(edges) + 1):
                msg += '\n%s) %s' % (i, edges[i - 1][2])
        # For end return special message and clear current dot-schema
        if self.schema.current_node_id == 'End':
            msg = msg_code.get_error(21, lang=self.__lang)
            self.schema = None
        return msg
Esempio n. 2
0
    def command(self, user, text):
        c_graph = classes.GraphObject()
        result = errors_code.get_error(25, user.get_lang())
        # User ask end, history was forget
        if text.find('/end') >= 0:
            user.clear()
            result = errors_code.get_error(1, user.get_lang())
        # User ask start
        elif text.find('/start') >= 0:
            # Must find the number of the solution. Split the line by space
            # If length 2 then process. Example / start 1
            try:
                numer = int(text.split()[1])
            except:
                result = errors_code.get_error(4, user.get_lang())
            else:
                # Let us verify that the solution exists
                if 1 <= numer <= c_graph.len():
                    numer -= 1
                    user.start(schema=c_graph.get_by_id(numer))
                    result = user.next_dialog("any")
                else:
                    result = errors_code.get_error(2, user.get_lang())

        # if the user is not in the dictionary add
        elif text.find('/help') >= 0:
            result = errors_code.get_error(5, user.get_lang()) % user.get_name()

        # Issue an existing list of algorithms with a description
        elif text.find('/list') >= 0:
            result = errors_code.get_error(6, user.get_lang())
            for i in range(0, len(graphs)):
                result += '\n%s) %s %s' % (i + 1, graphs[i]['file_name'], graphs[i]['description'])
        return result
Esempio n. 3
0
async def test_spec_start_2(http_server_client):
    """
    Неправильный формат команды /start 1000 - команды со слишком большим номером
    """
    req = {
        'update_id': 588992740,
        'message': {
            'message_id': 2011,
            'from': {
                'id': 322608291,
                'is_bot': False,
                'first_name': 'Алексей',
                'last_name': 'Простаков',
                'username': '******',
                'language_code': 'ru'
            },
            'chat': {
                'id': 322608291,
                'first_name': 'Алексей',
                'last_name': 'Простаков',
                'username': '******',
                'type': 'private'
            },
            'date': 1566312765,
            'text': '/start 1000',
            'entities': [{
                'offset': 0,
                'length': 6,
                'type': 'bot_command'
            }]
        }
    }
    patch_response = await http_server_client.fetch('/bot',
                                                    method='POST',
                                                    body=json.dumps(req))
    assert patch_response.code == HTTPStatus.OK
    resp = json.loads(patch_response.body, encoding='utf-8')
    assert resp['text'] == errors_code.get_error(2, 'ru')
Esempio n. 4
0
    def dot_algorithm(self, user_id, user_name, text, lang):
        """
        This is main function. It make answer for user
        :param user_id: unic user id
        :param user_name:
        :param text:
        :param lang:
        :return:
        """

        # We have not talked yet
        if user_id not in users_dict:
            result = errors_code.get_error(7, lang) % user_name
            users_dict[user_id] = classes.User(user_id, user_name, lang)
            return result
        user = users_dict[user_id]
        if text[0] == '/':
            # command
            result = self.command(user, text)
        else:
            # answer
            result = user.next_dialog(text)
        return result
Esempio n. 5
0
async def test_spec_start_1(http_server_client):
    """
    Ввели команду старт и не поздаровались.
    """
    req = {
        'update_id': 588992740,
        'message': {
            'message_id': 2011,
            'from': {
                'id': 322608291,
                'is_bot': False,
                'first_name': 'Алексей',
                'last_name': 'Простаков',
                'username': '******',
                'language_code': 'ru'
            },
            'chat': {
                'id': 322608291,
                'first_name': 'Алексей',
                'last_name': 'Простаков',
                'username': '******',
                'type': 'private'
            },
            'date': 1566312765,
            'text': '/help',
            'entities': [{
                'offset': 0,
                'length': 6,
                'type': 'bot_command'
            }]
        }
    }
    patch_response = await http_server_client.fetch('/bot',
                                                    method='POST',
                                                    body=json.dumps(req))
    if patch_response.code == HTTPStatus.OK:
        req = {
            'update_id': 588992740,
            'message': {
                'message_id': 2011,
                'from': {
                    'id': 322608291,
                    'is_bot': False,
                    'first_name': 'Алексей',
                    'last_name': 'Простаков',
                    'username': '******',
                    'language_code': 'ru'
                },
                'chat': {
                    'id': 322608291,
                    'first_name': 'Алексей',
                    'last_name': 'Простаков',
                    'username': '******',
                    'type': 'private'
                },
                'date': 1566312765,
                'text': '/start',
                'entities': [{
                    'offset': 0,
                    'length': 6,
                    'type': 'bot_command'
                }]
            }
        }
        patch_response = await http_server_client.fetch('/bot',
                                                        method='POST',
                                                        body=json.dumps(req))
        assert patch_response.code == HTTPStatus.OK
        resp = json.loads(patch_response.body, encoding='utf-8')
        assert resp['text'] == errors_code.get_error(4, 'ru')