├── command_system.py ├── commands ├── cat.py ├── hello.py └── info.py ├── flask_app.py ├── messageHandler.py ├── settings.py └── vkapi.py /command_system.py: -------------------------------------------------------------------------------- 1 | command_list = [] 2 | 3 | 4 | class Command: 5 | def __init__(self): 6 | self.__keys = [] 7 | self.description = '' 8 | command_list.append(self) 9 | 10 | @property 11 | def keys(self): 12 | return self.__keys 13 | 14 | @keys.setter 15 | def keys(self, mas): 16 | for k in mas: 17 | self.__keys.append(k.lower()) 18 | 19 | def process(self): 20 | pass 21 | -------------------------------------------------------------------------------- /commands/cat.py: -------------------------------------------------------------------------------- 1 | import command_system 2 | import vkapi 3 | import settings 4 | 5 | def cat(): 6 | # Получаем случайную картинку из паблика 7 | attachment = vkapi.get_random_wall_picture(-32015300, settings.access_token) 8 | message = 'Вот тебе котик :)\nВ следующий раз я пришлю другого котика.' 9 | return message, attachment 10 | 11 | cat_command = command_system.Command() 12 | 13 | cat_command.keys = ['котик', 'кошка', 'кот', 'котенок', 'котяра', 'cat'] 14 | cat_command.description = 'Пришлю картинку с котиком' 15 | cat_command.process = cat 16 | -------------------------------------------------------------------------------- /commands/hello.py: -------------------------------------------------------------------------------- 1 | import command_system 2 | 3 | 4 | def hello(): 5 | message = 'Привет, друг!\nЯ новый чат-бот.' 6 | return message, '' 7 | 8 | hello_command = command_system.Command() 9 | 10 | hello_command.keys = ['привет', 'hello', 'дратути', 'здравствуй', 'здравствуйте'] 11 | hello_command.description = 'Поприветствую тебя' 12 | hello_command.process = hello 13 | -------------------------------------------------------------------------------- /commands/info.py: -------------------------------------------------------------------------------- 1 | import command_system 2 | 3 | 4 | def info(): 5 | message = '' 6 | for c in command_system.command_list: 7 | message += c.keys[0] + ' - ' + c.description + '\n' 8 | return message, '' 9 | 10 | 11 | info_command = command_system.Command() 12 | 13 | info_command.keys = ['помощь', 'помоги', 'help'] 14 | info_command.description = 'Покажу список команд' 15 | info_command.process = info 16 | -------------------------------------------------------------------------------- /flask_app.py: -------------------------------------------------------------------------------- 1 | 2 | # A very simple Flask Hello World app for you to get started with... 3 | 4 | from flask import Flask, request, json 5 | from settings import * 6 | import messageHandler 7 | 8 | app = Flask(__name__) 9 | 10 | @app.route('/') 11 | def hello_world(): 12 | return 'Hello from Flask!' 13 | 14 | @app.route('/', methods=['POST']) 15 | def processing(): 16 | data = json.loads(request.data) 17 | if 'type' not in data.keys(): 18 | return 'not vk' 19 | if data['type'] == 'confirmation': 20 | return confirmation_token 21 | elif data['type'] == 'message_new': 22 | messageHandler.create_answer(data['object'], token) 23 | return 'ok' 24 | -------------------------------------------------------------------------------- /messageHandler.py: -------------------------------------------------------------------------------- 1 | import vkapi 2 | import os 3 | import importlib 4 | from command_system import command_list 5 | 6 | 7 | def damerau_levenshtein_distance(s1, s2): 8 | d = {} 9 | lenstr1 = len(s1) 10 | lenstr2 = len(s2) 11 | for i in range(-1, lenstr1 + 1): 12 | d[(i, -1)] = i + 1 13 | for j in range(-1, lenstr2 + 1): 14 | d[(-1, j)] = j + 1 15 | for i in range(lenstr1): 16 | for j in range(lenstr2): 17 | if s1[i] == s2[j]: 18 | cost = 0 19 | else: 20 | cost = 1 21 | d[(i, j)] = min( 22 | d[(i - 1, j)] + 1, # deletion 23 | d[(i, j - 1)] + 1, # insertion 24 | d[(i - 1, j - 1)] + cost, # substitution 25 | ) 26 | if i and j and s1[i] == s2[j - 1] and s1[i - 1] == s2[j]: 27 | d[(i, j)] = min(d[(i, j)], d[i - 2, j - 2] + cost) # transposition 28 | return d[lenstr1 - 1, lenstr2 - 1] 29 | 30 | 31 | def load_modules(): 32 | files = os.listdir("mysite/commands") 33 | modules = filter(lambda x: x.endswith('.py'), files) 34 | for m in modules: 35 | importlib.import_module("commands." + m[0:-3]) 36 | 37 | 38 | def get_answer(body): 39 | message = "Прости, не понимаю тебя. Напиши 'помощь', чтобы узнать мои команды" 40 | attachment = '' 41 | distance = len(body) 42 | command = None 43 | key = '' 44 | for c in command_list: 45 | for k in c.keys: 46 | d = damerau_levenshtein_distance(body, k) 47 | if d < distance: 48 | distance = d 49 | command = c 50 | key = k 51 | if distance == 0: 52 | message, attachment = c.process() 53 | return message, attachment 54 | if distance < len(body)*0.4: 55 | message, attachment = command.process() 56 | message = 'Я понял ваш запрос как "%s"\n\n' % key + message 57 | return message, attachment 58 | 59 | 60 | 61 | def create_answer(data, token): 62 | load_modules() 63 | user_id = data['user_id'] 64 | message, attachment = get_answer(data['body'].lower()) 65 | vkapi.send_message(user_id, token, message, attachment) 66 | 67 | -------------------------------------------------------------------------------- /settings.py: -------------------------------------------------------------------------------- 1 | token = '' 2 | confirmation_token = '' 3 | access_token = '' 4 | -------------------------------------------------------------------------------- /vkapi.py: -------------------------------------------------------------------------------- 1 | import vk 2 | import random 3 | 4 | session = vk.Session() 5 | api = vk.API(session, v=5.0) 6 | 7 | 8 | def get_random_wall_picture(group_id, token): 9 | max_num = api.photos.get(owner_id=group_id, album_id='wall', count=0, access_token=token)['count'] 10 | num = random.randint(1, max_num) 11 | photo = api.photos.get(owner_id=str(group_id), album_id='wall', count=1, offset=num, access_token=token)['items'][0]['id'] 12 | attachment = 'photo' + str(group_id) + '_' + str(photo) 13 | return attachment 14 | 15 | 16 | def send_message(user_id, token, message, attachment=""): 17 | api.messages.send(access_token=token, user_id=str(user_id), message=message, attachment=attachment) 18 | 19 | --------------------------------------------------------------------------------