I have a bot for VK on callback api (python), he answers in the LAN, but there is no conversation!

import vk import random session = vk.Session() api = vk.API(session, v=5.0) def get_random_wall_picture(group_id, token): max_num = api.photos.get(owner_id=group_id, album_id='260175787', count=0, access_token=token)['count'] num = random.randint(1, max_num) photo = api.photos.get(owner_id=str(group_id), album_id='260175787ll', count=1, offset=num, access_token=token)['items'][0]['id'] attachment = 'photo' + str(group_id) + '_' + str(photo) return attachment def send_message(user_id, token, message, attachment=""): api.messages.send(access_token=token, user_id=str(user_id), message=message, attachment=attachment) 
  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): message = "Прости, не понимаю тебя. ПЛАКИЧ! Напиши '?команды', чтобы узнать как не странно мои команды НЯ" attachment = '' distance = len(body) command = None key = '' for c in command_list: for k in c.keys: d = damerau_levenshtein_distance(body, k) if d < distance: distance = d command = c key = k if distance == 0: message, attachment = c.process() return message, attachment if distance < len(body)*0.4: message, attachment = command.process() message = 'Я поняла твой запрос как "%s"\n\n' % key + message return message, 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) 
  • Why apologize if you can copy the code into the question? - cpp questions
  • one
    Here corrected, instead of screenshots code. - Shiro Durachek 7:07 pm

1 answer 1

Sending messages to conversations is almost the same as in HP, only in api.messages.send, instead of the user_id argument, user_id need to use chat_id

https://vk.com/dev/messages.send

  • And at the end where the create_answer function is needed? - Shiro Durachek 6:22 pm
  • In the given piece of code, it is not very clear how you want to use create_answer, and where the data parameter comes from. But most likely - yes, it is necessary. - Xander