F
F
FIRSTNAME LASTNAME2019-11-03 16:37:02
Python
FIRSTNAME LASTNAME, 2019-11-03 16:37:02

Why does the bot on VkBotLongPoll duplicate responses to messages?

There is some bot using VkBotLongPoll. The logic, in principle, is simple: authorization, creation of a list of names and responses to unrecognized commands, command dictionaries in the format "command text: response", and a message handler function:

def message_type(self, message: str) -> str:
        """
        Функция определения типа сообщения

        :param message: текст сообщения
        """
        for name in self.NAMES:
            if message.startswith(name):
                message_words = message.split()
                other_text_list = message_words
                other_text_list.pop(0)
                command = ' '.join(other_text_list)

                if command in self.PHRASES:
                    return random.choice(self.PHRASES[command])
                elif command not in self.PHRASES and command not in self.COMMANDS:
                    return random.choice(self.COMMAND_NOT_STATED)
            elif message == 'да':
                return self.MEMES[message]
            elif message in self.MEMES:
                return self.MEMES[message]

And the main loop:
def listen(self):
        """
        Основной цикл, реагирующий на события
        """

        for event in self.longpoll.listen():
            try:
                if event.type == VkBotEventType.MESSAGE_NEW:
                    self.text = event.object['text'].lower()
                    self.result = self.message_type(message=self.text)
                    if self.result:
                        if event.from_user:
                                self.send_message(id_=event.object['from_id'], message=self.result)
                        elif event.from_chat:
                                self.send_message(id_=event.object['peer_id'], message=self.result)
            except requests.exceptions.ReadTimeout:
                print('Reconnection...')
                self.listen()

The bot works quite correctly in LAN, as well as with one conversation. But as the number of conversations increases, he starts to duplicate answers from time to time. What could be the reason, and will sorting conversations in the database save?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FIRSTNAME LASTNAME, 2019-11-05
@true_pelmeshek

Changed the access key, everything works correctly.

A
arynyklas, 2019-11-04
@arynyklas

I think because you pointed out

elif message == 'да':
    return self.MEMES[message]
elif message in self.MEMES:
    return self.MEMES[message]

Explanation:
You write that if a person sends 'yes' then the message variable is sent.
But the message variable in this situation will be equal = 'yes'
Do you understand?
You must not specify 'message' in the send method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question