So i tried to make keyboard for quite some time but it don't work main.py
from flask import Flask, request, json
from settings import *
import messageHandler
app = Flask(__name__)
#Проверка перезагрузки сайта
@app.route('/')
def hello_world():
return 'Hi there'
@app.route('/', methods=['POST'])
def processing():
#Распаковываем json из пришедшего POST-запроса
data = json.loads(request.data)
#Узнаём какой запрос мы получили от ВК
if 'type' not in data.keys():
return 'not vk'
#Если подтверждение сервера
if data['type'] == 'confirmation':
return confirmation_token
#Если новое сообщение
if data['type'] == 'message_new':
messageHandler.create_answer(data['object']['message'], token)
return 'ok'
if __name__ == '__main__':
app.run()`
api.py
import vk
session = vk.Session()
api = vk.API(session, v=5.103)
def send_message(peer_id, token, message, random_id, keyboard):
api.messages.send(peer_id=int(peer_id), access_token=token, message=message, random_id=random_id)
messageHandler.py
import api
from vk_api.utils import get_random_id
import os
import importlib
from command_handler import command_list
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(text):
message = "Прости, я тебя не поняла, попоробуй написать 'помощь'."
for c in command_list:
if text in c.keys:
message = c.process()
return message
def create_answer(data, token):
load_modules()
peer_id = data['from_id']
message = get_answer(data['text'].lower())
random_id = get_random_id() #int(round(time() * 100000))
api.send_message(peer_id, token, message, random_id)
command_handler.py
command_list = []
class Command:
def __init__(self):
self.__keys = []
self.description = ''
command_list.append(self)
@property
def keys(self):
return self.__keys
@keys.setter
def keys(self, mas):
for k in mas:
self.__keys.append(k.lower())
def process(self):
pass
command with keyboard
import command_handler
def info():
message = "Приветствую вас в военно политической игре"
return message
def keyboard():
keyboard = {"one_time": True, "buttons": [[{"action": {"type": "text", "payload": "{\"button\": \"1\"}", "label": "Кнопка"}, "color": "positive"}, {"action": {"type": "text", "payload": "{\"button\": \"2\"}", "label": "Кнопка" }, "color": "positive"}]]}
return keyboard
wpg_command = command_handler.Command()
wpg_command.keys = ['впи', 'цивка', 'wpg']
wpg_command.description = 'Позволяет поиграть в военно политическую игру'
wpg_command.process = info
i tried using many things like adding
self.keyboard = {}...
...
def keyboard(self):
return self.keyboard
into command_handler but i am really bads with classes and made many changes in all these files but nothing happened with the exception of bot stopping working... pls help me