0

Есть код, телеграм бот. при нажатии на кнопку выдает фразы из списка. Когда список доходит до конца - выдает ошибку:

A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message text is empty"

это из-за того что список дошел до конца и там пусто?

как мне сделать что бы список начинался заново?

мой код:

import telebot
import random
from telebot import types
from collections import deque

def get_first_element_in_the_queue(array: list) -> str:
    first_element = array[0]
    array.rotate(-1)
    return first_element

f = open('/Users/marina/ukrwrite/ch.txt', 'r', encoding='UTF-8')
ch = f.read().split('\n')
f.close()

f = open('/Users/marina/ukrwrite/useful.txt', 'r', encoding='UTF-8')
useful = f.read().split('\n')
f.close()

f = open('/Users/marina/ukrwrite/ra.txt', 'r', encoding='UTF-8')
ra = f.read().split('\n')
f.close()

ch_q = deque(ch)
useful_q = deque(useful)
ra_q = deque(ra)

bot = telebot.TeleBot('')

@bot.message_handler(commands=["start"])
def start(m, res=False):
        markup=types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1=types.KeyboardButton("челендж")
        item2=types.KeyboardButton("корисне")
        item3 = types.KeyboardButton("рекомендовані авторки")
        markup.add(item1)
        markup.add(item2)
        markup.add(item3)
        bot.send_message(m.chat.id, 'Привіт. ',  reply_markup=markup)
        handle_text(message=None, useful_q=useful_q)

@bot.message_handler(content_types=["text"])
def handle_text(message):
    if message.text.strip() == 'челендж':
        ch_q == get_first_element_in_the_queue(ch_q)
        bot.send_message(message.chat.id, ch_q)
    if message.text.strip() == 'корисне':
        useful_q == get_first_element_in_the_queue(useful_q)
        bot.send_message(message.chat.id, useful_q)
    if message.text.strip() == 'рекомендовані авторки':
        ra_q == get_first_element_in_the_queue(ra_q)
        bot.send_message(message.chat.id, ra_q)
bot.infinity_polling()

1 ответ 1

0

Можно попробовать так

def get_first_element_in_the_queue(queue: deque):
    first_element = queue.popleft()
    queue.append(first_element)
    return first_element

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.