Answer the question
In order to leave comments, you need to log in
How can I implement the issuance of data from excel to the VK bot?
Here is the code itself, do not kick hard, my first project:
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
import vk_api
from datetime import datetime
import random
token ='token'
vk_session = vk_api.VkApi(token=token)
session_api = vk_session.get_api()
longpoll = VkLongPoll(vk_session)
def create_keyboard(response):
keyboard = VkKeyboard(one_time=False)
if response == 'начать':
keyboard.add_button('Список групп', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Поиск по преподавателю', color=VkKeyboardColor.POSITIVE)
elif response == 'назад':
keyboard.add_button('Список групп', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Поиск по преподавателю', color=VkKeyboardColor.POSITIVE)
elif response == 'поиск по преподавателю':
keyboard.add_button('Назад', color=VkKeyboardColor.NEGATIVE)
elif response == 'список групп':
keyboard.add_button('1 Курс', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('2 Курс', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('3 Курс', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('4 Курс', color=VkKeyboardColor.DEFAULT)
elif response == '1 курс':
keyboard.add_button('ТМ1', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('ВС1', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('ИС1', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('ЭЛ7', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Ю45', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('М116', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('ПР23', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('В23', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Х182', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Х183', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Ю40', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Назад', color=VkKeyboardColor.NEGATIVE)
elif response == '2 курс':
keyboard.add_button('Ю38', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('ЭЛ6', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Ю43', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('П36', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('М115', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('ПР22', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('В22', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Х179', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Х180', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Х181', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Назад', color=VkKeyboardColor.NEGATIVE)
elif response == '3 курс':
keyboard.add_button('ЭЛ5', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Ю41', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('М114', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('ПР21', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('В21', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Х176', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Х177', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('В23', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Назад', color=VkKeyboardColor.NEGATIVE)
elif response == '4 курс':
keyboard.add_button('Х173', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Х174', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Х178', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('М113', color=VkKeyboardColor.DEFAULT)
keyboard.add_button('Ю39', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Назад', color=VkKeyboardColor.NEGATIVE)
keyboard = keyboard.get_keyboard()
return keyboard
def send_message(vk_session, id_type, id, message=None, keyboard=None):
vk_session.method('messages.send',{id_type: id, 'message': message, 'random_id': random.randint(-2147483648, +2147483648), 'keyboard': keyboard})
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
print('Сообщение пришло в: ' + str(datetime.strftime(datetime.now(), "%H:%M:%S")))
print('Текст сообщения: ' + str(event.text))
print(event.user_id)
response = event.text.lower()
keyboard = create_keyboard(response)
if event.from_user and not event.from_me:
if response == 'начать':
send_message(vk_session, 'user_id', event.user_id, message='Привет!', keyboard=keyboard)
elif response == 'список групп':
send_message(vk_session, 'user_id', event.user_id, message='Список групп', keyboard=keyboard)
elif response == '1 курс':
send_message(vk_session, 'user_id', event.user_id, message='Список групп 1 курса', keyboard=keyboard)
elif response == '2 курс':
send_message(vk_session, 'user_id', event.user_id, message='Список групп 2 курса', keyboard=keyboard)
elif response == '3 курс':
send_message(vk_session, 'user_id', event.user_id, message='Список групп 3 курса', keyboard=keyboard)
elif response == '4 курс':
send_message(vk_session, 'user_id', event.user_id, message='Список групп 4 курса', keyboard=keyboard)
elif response == 'поиск по преподавателю':
send_message(vk_session, 'user_id', event.user_id, message='Введите фамилию преподавателя', keyboard=keyboard)
elif response == 'назад':
send_message(vk_session, 'user_id', event.user_id, message='Привет!', keyboard=keyboard)
else: print("Неопознанная команда")
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question