Answer the question
In order to leave comments, you need to log in
Handling pressing an inline button on a Telegram bot?
Hello. I am new to this portal and to Python too, sorry in advance if I poorly described the problem or wrote crooked code.
I am writing a code for a telegram bot. The task of the bot is to read lines from a text file and insert an inline keyboard after certain lines. When one of the buttons is pressed, it should keep sending messages up to a certain line. The problem is that I don't understand how to check for a keystroke and continue sending lines from the file.
import telebot
import config
import scenario
from time import sleep as sl
from telebot import types
bot = telebot.TeleBot(config.token)
@bot.message_handler(commands = ['start'])
def vstuplenie(message):
f = list(open ('Name_file.txt', encoding = 'Windows-1251'))
bot.send_message(message.chat.id, scenario.nachalo )
sl(1)
for i, line in enumerate(f):
if i < 5:
bot.send_message(message.chat.id, line )
sl(3)
markup = types.InlineKeyboardMarkup(row_width = 1)
item1 = types.InlineKeyboardButton("Бла-бла-бла", callback_data= "answer1")
item2 = types.InlineKeyboardButton("Бла-бла-бла", callback_data= "answer2")
markup.add(item1, item2)
bot.send_message(message.chat.id, "Бла-бла-бла", reply_markup=markup)
sl(3)
'''На этом месте проблема. Без проверки нажатия кнопки он просто продолжает отправлять текст не дожидаясь
клика'''
if :
for i, line in enumerate(f):
if i > 4:
bot.send_message(message.chat.id, line )
sl(3)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
try:
if call.message:
if call.data == "answer1":
bot.send_message(call.message.chat.id, "Бла-бла-бла")
elif call.data == "answer2":
bot.send_message(call.message.chat.id, "Бла-бла-бла")
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Бла-бла-бла" ,
reply_markup=None)
except Exception as e:
print(repr(e))
if __name__ == '__main__':
bot.polling(none_stop = True)
Answer the question
In order to leave comments, you need to log in
There is a problem here. Without checking the button click, it just keeps sending the text without waiting for the click.
if call.data == "answer1":
elif call.data == "answer2":
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question