Вот код, если сместить if response=='сегодня' к остальным, то оно прекрасно работает, но нужно чтобы после нажатия на группу и кнопку расписания происходила выдача
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
import vk_api
import random
import xlrd
from datetime import date
from datetime import datetime
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)
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 == 'тм1':
keyboard.add_button('Сегодня', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('Завтра', color=VkKeyboardColor.DEFAULT)
keyboard.add_line()
keyboard.add_button('На неделю', 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 == 'поиск по преподавателю':
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':
if response == 'сегодня':
today = datetime.now()
day = date.weekday(today)
if day == 0 :
workbook = xlrd.open_workbook(r"ras19-20_2s.xls")
sheet = workbook.sheet_by_index(0)
def get_cell_range(start_col, start_row, end_col, end_row):
return [sheet.row_slice(row, start_colx=start_col, end_colx=end_col + 1) for row in range(start_row, end_row + 1)]
data = get_cell_range(3, 7, 6, 26) # D8 to G27
print (data)
send_message(vk_session, 'user_id', event.user_id, message=str(data), keyboard=None)
elif day == 3 :
workbook = xlrd.open_workbook(r"ras19-20_2s.xls")
sheet = workbook.sheet_by_index(0)
def get_cell_range(start_col, start_row, end_col, end_row):
return [sheet.row_slice(row, start_colx=start_col, end_colx=end_col + 1) for row in range(start_row, end_row + 1)]
data = get_cell_range(3, 7, 6, 26) # D8 to G27
print (data)
send_message(vk_session, 'user_id', event.user_id, message=str(data), keyboard=None)
send_message(vk_session, 'user_id', event.user_id, message='123', keyboard=keyboard)