N
N
Nikita Chaynikovich2022-04-12 17:09:52
Python
Nikita Chaynikovich, 2022-04-12 17:09:52

Why do I need to click on the button under the tg bot message twice?

When I launch the bot and poke the /Photos command (the code is below, instead of a photo text), then after re-selecting a section to send a photo or a subsection of the section, you need to select it 2 times (on the 1st it deletes on the 2nd it sends although it should do it at the same time)

from telebot import *
import telebot

bot = telebot.TeleBot('')

dict = {"second_1": "sword_demon.jpg", "second_2": "anime-rain-cyber.jpg", "second_3": "wallpaperflare.com_wallpaper (1).jpg", "second_4": "sword_demon_purple.jpg", "second_5": "anime girl.jpg", "second_6": "space1.jpg",
    "second_7": "wallpaperflare.com_wallpaper (3).jpg", "second_8": "space2.jpg", "second_9": "planet_brake.jpg", "second_10": "little_princ.jpg",
    "second_11": "nature1.jpg", "second_12": "nature2.jpg", "second_13": "nature3.jpg", "second_14": "nature4.jpg", "second_15": "nature5.jpg", } 

photo_id = 0
photo_num_choice_id = 0

@bot.message_handler(commands=['Photos'])
def start_message(message):
    keyboard = types.InlineKeyboardMarkup()
    button1 = types.InlineKeyboardButton(text='Аниме', callback_data='first_1')
    button2 = types.InlineKeyboardButton(text='Космос', callback_data='first_2')
    button3 = types.InlineKeyboardButton(text='Природа', callback_data='first_3')
    keyboard.add(button1)
    keyboard.add(button2)
    keyboard.add(button3)
    bot.send_message(message.chat.id, text="Выбери тематику фото", reply_markup=keyboard)

@bot.callback_query_handler(func=lambda call: call.data.startswith('first'))
def photos1(call):
    global photo_num_choice_id
    bot.answer_callback_query(callback_query_id=call.id)
    try:
        bot.delete_message(call.message.chat.id, photo_num_choice_id)
        bot.delete_message(call.message.chat.id, photo_id)
        print(photo_id)
        if call.data == 'first_1':
            keyboard = types.InlineKeyboardMarkup()
            button1 = types.InlineKeyboardButton(text='Фото 1', callback_data='second_1')
            button2 = types.InlineKeyboardButton(text='Фото 2', callback_data='second_2')
            button3 = types.InlineKeyboardButton(text='Фото 3', callback_data='second_3')
            button4 = types.InlineKeyboardButton(text='Фото 4', callback_data='second_4')
            button5 = types.InlineKeyboardButton(text='Фото 5', callback_data='second_5')
            keyboard.add(button1)
            keyboard.add(button2)
            keyboard.add(button3)
            keyboard.add(button4)
            keyboard.add(button5)
            photo_num_choice_id = bot.send_message(call.message.chat.id, text="Выбери фото (Аниме)", reply_markup=keyboard).message_id
        elif call.data == 'first_2':
            keyboard = types.InlineKeyboardMarkup()
            button1 = types.InlineKeyboardButton(text='Фото 1', callback_data='second_6')
            button2 = types.InlineKeyboardButton(text='Фото 2', callback_data='second_7')
            button3 = types.InlineKeyboardButton(text='Фото 3', callback_data='second_8')
            button4 = types.InlineKeyboardButton(text='Фото 4', callback_data='second_9')
            button5 = types.InlineKeyboardButton(text='Фото 5', callback_data='second_10')
            keyboard.add(button1)
            keyboard.add(button2)
            keyboard.add(button3)
            keyboard.add(button4)
            keyboard.add(button5)
            photo_num_choice_id = bot.send_message(call.message.chat.id, text="Выбери фото (Космос)", reply_markup=keyboard).message_id
        elif call.data == 'first_3':
            keyboard = types.InlineKeyboardMarkup()
            button1 = types.InlineKeyboardButton(text='Фото 1', callback_data='second_11')
            button2 = types.InlineKeyboardButton(text='Фото 2', callback_data='second_12')
            button3 = types.InlineKeyboardButton(text='Фото 3', callback_data='second_13')
            button4 = types.InlineKeyboardButton(text='Фото 4', callback_data='second_14')
            button5 = types.InlineKeyboardButton(text='Фото 5', callback_data='second_15')
            keyboard.add(button1)
            keyboard.add(button2)
            keyboard.add(button3)
            keyboard.add(button4)
            keyboard.add(button5)
            photo_num_choice_id = bot.send_message(call.message.chat.id, text="Выбери фото (Природа)", reply_markup=keyboard).message_id
    except:
        if call.data == 'first_1':
            keyboard = types.InlineKeyboardMarkup()
            button1 = types.InlineKeyboardButton(text='Фото 1', callback_data='second_1')
            button2 = types.InlineKeyboardButton(text='Фото 2', callback_data='second_2')
            button3 = types.InlineKeyboardButton(text='Фото 3', callback_data='second_3')
            button4 = types.InlineKeyboardButton(text='Фото 4', callback_data='second_4')
            button5 = types.InlineKeyboardButton(text='Фото 5', callback_data='second_5')
            keyboard.add(button1)
            keyboard.add(button2)
            keyboard.add(button3)
            keyboard.add(button4)
            keyboard.add(button5)
            photo_num_choice_id = bot.send_message(call.message.chat.id, text="Выбери фото (Аниме)", reply_markup=keyboard).message_id
        elif call.data == 'first_2':
            keyboard = types.InlineKeyboardMarkup()
            button1 = types.InlineKeyboardButton(text='Фото 1', callback_data='second_6')
            button2 = types.InlineKeyboardButton(text='Фото 2', callback_data='second_7')
            button3 = types.InlineKeyboardButton(text='Фото 3', callback_data='second_8')
            button4 = types.InlineKeyboardButton(text='Фото 4', callback_data='second_9')
            button5 = types.InlineKeyboardButton(text='Фото 5', callback_data='second_10')
            keyboard.add(button1)
            keyboard.add(button2)
            keyboard.add(button3)
            keyboard.add(button4)
            keyboard.add(button5)
            photo_num_choice_id = bot.send_message(call.message.chat.id, text="Выбери фото (Космос)", reply_markup=keyboard).message_id
        elif call.data == 'first_3':
            keyboard = types.InlineKeyboardMarkup()
            button1 = types.InlineKeyboardButton(text='Фото 1', callback_data='second_11')
            button2 = types.InlineKeyboardButton(text='Фото 2', callback_data='second_12')
            button3 = types.InlineKeyboardButton(text='Фото 3', callback_data='second_13')
            button4 = types.InlineKeyboardButton(text='Фото 4', callback_data='second_14')
            button5 = types.InlineKeyboardButton(text='Фото 5', callback_data='second_15')
            keyboard.add(button1)
            keyboard.add(button2)
            keyboard.add(button3)
            keyboard.add(button4)
            keyboard.add(button5)
            photo_num_choice_id = bot.send_message(call.message.chat.id, text="Выбери фото (Природа)", reply_markup=keyboard).message_id

@bot.callback_query_handler(func=lambda call: call.data.startswith('second'))
def photos2(call):
    global photo_id
    bot.answer_callback_query(callback_query_id=call.id)
    try:
        bot.delete_message(call.message.chat.id, photo_id)
        img = dict[call.data]
        photo_id = bot.send_message(call.message.chat.id, img).message_id
    except:
        img = dict[call.data]
        photo_id = bot.send_message(call.message.chat.id, img).message_id
bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question