G
G
gargamel12021-04-18 00:54:18
PHP
gargamel1, 2021-04-18 00:54:18

Working with multiple telebot keyboards?

I work with a scheduling bot. It should, in theory, check the callback from the keyboard of quantums, then from the keyboard of groups and parse the document in search of a line with the desired group, and then write the contents of the line with the required group to the list. Here is the code:

import telebot
import config
import random
import openpyxl

from telebot import types

bot = telebot.TeleBot(config.TOKEN)
wb = openpyxl.reader.excel.load_workbook(filename="timetable.xlsx")

#Генератор кнопок из списка финкцией, sheet_num - для выбора страницы сканирования

def day_btns(groups):
  days = types.InlineKeyboardMarkup(row_width=2)
  days.add(*[types.InlineKeyboardButton(text=groups[i],callback_data=groups[i]) for i in range(len(groups))])
  return days

def kvants(kvantyms):
  InlineKvantsButtom = types.InlineKeyboardMarkup(row_width=2)
  InlineKvantsButtom.add(*[types.InlineKeyboardButton(text=kvantyms[i],callback_data=kvantyms[i]) for i in range(len(kvantyms))])
  return InlineKvantsButtom


@bot.message_handler(commands=['start'])
def welcome(message):
  sti = open('welcome.tgs', 'rb')
  bot.send_sticker(message.chat.id, sti)

  # keyboard
  markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
  item1 = types.KeyboardButton("Расписание")

  markup.add(item1)

  bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\n <b>Мы - команда Ярославского Кванториума.</b> \n Этот бот позволит:\nСвязаться с нами\nУзнать расписание занятий".format(message.from_user, bot.get_me()),
    parse_mode='html', reply_markup=markup)


#основной перебор всех команд
@bot.message_handler(content_types=['text'])
def lalala(message):
  if message.chat.type == 'private':
    if message.text == 'Расписание':
    
      kvantyms = []

      for i in range(len(wb.sheetnames)):
        kvantyms.append(wb.sheetnames[i])
      bot.send_message(message.chat.id, 'Выберите Квантум', reply_markup=kvants(kvantyms))

  else:


      bot.send_message(message.chat.id, 'Я не знаю что ответить ')


#этот блок выполнится если юзер отправит боту сообщение
@bot.message_handler(func=lambda c:True, content_types=['text'])
def info_message(message):
    bot.edit_message_reply_markup(message.chat.id, message_id = message.message_id-1, reply_markup = '')# удаляем кнопки у последнего сообщения


#обработка callback с больших кнопок расписания
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
  try:
    if call.message:
      #обработка callback с больших кнопок расписания
      i = 0
      while call.data != wb.sheetnames[i]:
        i+=1
      wb.active = i
      stop = i
      groups = [ ]
      smthe = wb.active.cell(row=4, column=2).value
      smth = smthe[0] + smthe[1]
      for i in range(1, 40):
        if wb.active.cell(row=i, column=2).value != None and smth in wb.active.cell(row=i, column=2).value:
          groups.append(wb.active.cell(row=i, column=2).value)

      bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Выберите группу",
      reply_markup=day_btns(groups))

      #обработка микрокнопок групп
      wb.active = stop
      t=1
      while wb.active.cell(row=t, column=2).value != call.data:
        t+=1
      lessons = [ ]
      for i in range(3, 10):
        lessons.append(wb.active.cell(row=t, column=i).value)

      

  except Exception as e:
    print(repr(e))

# RUN
bot.polling(none_stop=True)

First, the 1st error appears:
IndexError('list index out of range')
After a while, the second one appears:
ValueError('Row numbers must be between 1 and 1048576')

Please help me, why is this happening, what and where to change? By the way, the design of the callback check using the if-diaper had to be abandoned in favor of the adaptability of the bot to a changing schedule

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arthur, 2018-07-21
@phpguru

<?php
     $sql = "SELECT * FROM users";
     $result = mysql_query($sql)  or die(mysql_error());
   
     while ($row = mysql_fetch_assoc($result))
     {
     
         var_dump($row);  
      
     }

?>

See what data it returns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question