D
D
Denis2020-05-28 22:48:58
Python
Denis, 2020-05-28 22:48:58

How to make an issue for a group?

Here is the code, if you shift if response=='today' to the rest, then it works fine, but you need to issue after clicking on the group and the schedule button

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)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Murad Murtuzaliev, 2020-05-28
@l513f

longpoll.listen() receives one event per iteration and does not move on to the next until it has finished with the current one. If an event has been received before the previous one has completed, it will not be executed.
When "tm1" was received from the user, the condition became true and the corresponding block of code began to execute. Since the entire block was not executed and the next event was not received, the condition is false, so the block of code following it will not be executed. To avoid this, you need to place a condition outside elif response == 'tm1'. In this case, so that the execution of the block of code occurs only when first "tm1" and then "today" come in succession, you can add a bool type variable, which is false by default,
elif response == 'тм1':
if response == 'сегодня':

group = False
elif response == 'тм1':
    group = True

elif response == 'сегодня' and group:
    ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question