from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
import vk_api
import random
import data
import sqlite3
vk_session = vk_api.VkApi(token=data.token_vk)
session_api = vk_session.get_api()
longpoll = VkLongPoll(vk_session)
my_id = data.my_id
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
def create_keyboard(response):
keyboard = VkKeyboard(one_time=True)
if response == "меню":
keyboard.add_button('Домашнее задание', color=VkKeyboardColor.POSITIVE)
keyboard.add_line() # Переход на вторую строку
keyboard.add_button('Информация для дел', color=VkKeyboardColor.NEGATIVE)
# красная = NEGATIVE
# синяя = PRIMARY
# зеленая = POSITIVE
# белая = DEFAULT
elif response == 'Предзакрытие':
keyboard.add_button('Закрыть', color=VkKeyboardColor.NEGATIVE)
elif response == 'ДЗ':
keyboard.add_button('Домашнее задание', color=VkKeyboardColor.POSITIVE)
elif response == 'закрыть':
return keyboard.get_empty_keyboard()
keyboard = keyboard.get_keyboard()
return keyboard
def info_user(user_num):
global first_name, last_name
vk = vk_session.get_api()
user_get = vk.users.get(user_ids = (user_num))
user_get = user_get[0]
first_name = user_get['first_name']
last_name = user_get['last_name']
def send_message(vk_session, id_type, id, message=None, attachment=None, keyboard=None):
vk_session.method('messages.send',{id_type: id, 'message': message, 'random_id': random.randint(-2147483648, +2147483648), "attachment": attachment, 'keyboard': keyboard})
#НАЧАЛО ПРОГРАММЫ#
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
text_normal=str(event.text.lower)
response = event.text.lower()
create_keyboard(response)
user_num = event.user_id
info_user(user_num) # обращение к функции
if event.user_id == '205693186':
if response == '/дз':
send_message(vk_session, 'user_id', event.user_id, message='ТЕКСТ ТЕКСТ', keyboard=create_keyboard('Предзакрытие'))
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
if event.user_id == '205693186' or '147574833' and not event.from_me:
if event.text.lower() != 'закрыть':
id_predmet=str(event.text)[0]
home_dz=str(event.text)[2:255]
sql = "UPDATE home_work SET works=:home_dz WHERE id=:id;"
cursor.execute(sql,{"home_dz": home_dz, "id": id_predmet})
conn.commit()
send_message(vk_session, 'user_id', event.user_id, message='Заменил',keyboard=create_keyboard('меню'))
break
else:
send_message(vk_session, 'user_id', event.user_id, message='Закрываю..', keyboard=create_keyboard('меню'))
break
elif response == '/бд':
with conn:
cursor.execute("SELECT * FROM home_work")
row = cursor.fetchall()
for row in row:
send_message(vk_session, 'user_id', event.user_id, message='' + str(row)[1:-1] + '',keyboard=create_keyboard('меню'))