G
G
gelugu2018-06-30 18:11:01
Python
gelugu, 2018-06-30 18:11:01

Bot on python vk api. How to send messages only once?

There is a bot code, in general, he is satisfied with the work. But I can’t finish, how can I prevent similar messages from being sent to the user?
Here is a file describing methods related to sending messages (damerau_levenshtein_distance is for typos, it's not important in this question):

import vkapi
import os
import importlib
from command_system import command_list


def damerau_levenshtein_distance(s1, s2):
    d = {}
    lenstr1 = len(s1)
    lenstr2 = len(s2)
    for i in range(-1, lenstr1 + 1):
        d[(i, -1)] = i + 1
    for j in range(-1, lenstr2 + 1):
        d[(-1, j)] = j + 1
    for i in range(lenstr1):
        for j in range(lenstr2):
            if s1[i] == s2[j]:
                cost = 0
            else:
                cost = 1
            d[(i, j)] = min(
                d[(i - 1, j)] + 1,  # deletion
                d[(i, j - 1)] + 1,  # insertion
                d[(i - 1, j - 1)] + cost,  # substitution
            )
            if i and j and s1[i] == s2[j - 1] and s1[i - 1] == s2[j]:
                d[(i, j)] = min(d[(i, j)], d[i - 2, j - 2] + cost)  # transposition
    return d[lenstr1 - 1, lenstr2 - 1]


def load_modules():
    files = os.listdir("mysite/commands")
    modules = filter(lambda x: x.endswith('.py'), files)
    for m in modules:
        importlib.import_module("commands." + m[0:-3])


def get_answer(body):
    attachment = ''
    distance = len(body)
    command = None
    for c in command_list:
        for k in c.keys:
            d = damerau_levenshtein_distance(body, k)
            if d < distance:
                distance = d
                command = c
                if distance == 0:
                    attachment = c.process()
                return attachment
    if distance < len(body)*0.4:
        attachment = command.process()
    return attachment


def create_answer(data, token):
    load_modules()
    user_id = data['user_id']
    message, attachment = get_answer(data['body'].lower())
    vkapi.send_message(user_id, token, message, attachment)

The task is as follows:
there is a list of commands (2), the user can only send requests for these commands. And the bot responds with prepared answers. Everything is good and working here.
It should be supplemented in such a way that the bot responds to each command only once, and then simply ignores the user.
I know that you can use the messages.get and messages.search methods, but I don't understand how to use them in this task.
Tell me where to dig...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly, 2018-07-01
@gelugu

Record the user id and check all messages for the presence of the user in the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question