Im triyng to make filter inside inlinekeyboard, in short:
markup = types.InlineKeyboardMarkup()
button_4 = types.InlineKeyboardButton('Да, все верно', callback_data = 'yes')
button_5 = types.InlineKeyboardButton('Нет, нужно изменить', callback_data = 'no')
markup.add(button_4, button_5)
bot.send_message(message.chat.id, 'Введеные данные верны?', reply_markup = markup)
#bot.callback_query_handler(func = lambda call: True)
def answer(call):
if call.data == 'yes':
print('GOT IT')
but nothing happend, i don't see "GOT IT" in terminal (btw i used it in same code at the begin and it works...)
Try to Print callBack data:
#bot.message_handler(commands=["start"])
key = types.InlineKeyboardMarkup()
key.add(types.InlineKeyboardButton('Да, все верно', callback_data='yes'))
key.add(types.InlineKeyboardButton('Нет, нужно изменить', callback_data='no'))
bot.send_message(message.chat.id, 'Введеные данные верны?', reply_markup=markup)
#bot.callback_query_handler(func=lambda call: call.data)
def answer(call):
print(call.data)
Related
I need to make working "back" button in aiogram that returns to /start menu.
I know what's my mistake is, but I have no idea how to fix it. Can someone help?
back_button = InlineKeyboardButton(text = "back", callback_data="bk")
keyboard_back = InlineKeyboardMarkup().add(back_button)
#dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
return await bot.send_message(message.from_user.id, text="Hello!\nI'm WriteEssayBot!\nI will write any essay for you!", reply_markup = keyboard_inline)
#dp.callback_query_handler()
async def generate_text(call: types.CallbackQuery):
await bot.answer_callback_query(call.id)
if call.data == "bk":
await send_welcome() ```
recently i created a telegram bot using python and i added keyboard button features to the bot. However, i am having difficulties in getting replies from bot to the buttons users choose.
button7 = KeyboardButton('About Us',request_contact= False)
keyboard2 = ReplyKeyboardMarkup(resize_keyboard = True, one_time_keyboard = True).row(button7)
#dp.message_handler(commands=['info'])
async def mood(message: types.Message):
await message.reply('Do you wish to know about us??', reply_markup=keyboard2)
In this case, i created a button named "About Us" and i want the bot to open a url using webbrowser.open if the user click on that button. Can anyone help me solving this problem?
Try it:
markup = types.InlineKeyboardMarkup()
button1 = types.InlineKeyboardButton(text='Ukraine', url="https://en.wikipedia.org/wiki/Ukraine", callback_data='1')
markup.add(button1)
bot.send_message(message.from_user.id, 'Do you know?, parse_mode='Markdown', reply_markup=markup)
Text (inline button)
from aiogram import Bot, Dispatcher, executor, types
bot = Bot(token='token')
dp = Dispatcher(bot)
#dp.message_handler(commands=['start'])
async def welcome(message: types.Message):
markup = types.InlineKeyboardMarkup()
button1 = types.InlineKeyboardButton(text='Ukraine', url="https://en.wikipedia.org/wiki/Ukraine", callback_data='1')
button2 = types.InlineKeyboardButton(text='Hi bot!', callback_data="1")
markup.add(button1, button2)
await bot.send_message(message.from_user.id, "Do you know?", parse_mode="Markdown", reply_markup=markup)
#dp.callback_query_handler(lambda call: True)
async def sendText(call: types.CallbackQuery):
msg = ""
if call.data == "1":
msg = "Hi, programmer!"
await call.message.edit_text(msg)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
I'm implementing a simple function in Python, but it doesn't work
Function:
# Рандомное число с кнопкой
#bot.message_handler(content_types=['text'], commands=['get_number'])
def get_number(message):
number = random.randint(0, 1000)
markup = types.InlineKeyboardMarkup()
item_yes = types.InlineKeyboardButton('Да', callback_data='yes')
item_no = types.InlineKeyboardButton('Нет', callback_data='no')
markup.row(item_yes, item_no)
bot.send_message(message.chat.id, f"<i><b>Ваше число:</b></i>\n{number}\n\nЗагадать еще раз?", reply_markup=markup,
parse_mode='html')
# Реакция на нажатие кнопки
#bot.callback_query_handler(func=lambda call: True)
def answer(call, message):
if call.data == 'yes':
pass
elif call.data == 'no':
pass
Tell me how to make it so that when you click the "Yes" button, the function is executed when you click it again, when you click "No", it stops accordingly.
I believe you can change your method:
def answer(call, message):
for
def handle_query(call):
I'm using TelegramBotAPI library to create a telegram bot in python.
I have this code to show an Inline Keyboard
def gen_markup():
markup = InlineKeyboardMarkup()
markup.row_width = 2
markup.add(InlineKeyboardButton("Yes", callback_data="cb_yes"),
InlineKeyboardButton("No", callback_data="cb_no"))
return markup
And after a function to see this keyboard when you send a command.
I have also this code to do an action when press on a button.
#bot.callback_query_handler(func=lambda call: True)
def send_welcome(call):
if call.data == "cb_yes":
bot.answer_callback_query(call.id, "YES")
elif call.data == "cb_no":
bot.send_message(call.id, "NO")
This only show the text on the screen and after some seconds it disappear. How can I send instead a text message?
Thanks
CallbackQuery contains the message object which holds chat id that can be used with send_message()
#bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
if call.data == "cb_yes":
bot.answer_callback_query(call.id, "Answer is Yes")
elif call.data == "cb_no":
bot.send_message(call.message.chat.id, "This is a message")
I made telegram bot which works fine except the second InLineKeyboard (keyboard2 in the relevant code below). Can you provide any hints why? Thanks!
#bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text == "start":
keyboard = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton(text="1", callback_data="1")
...
btn10 = types.InlineKeyboardButton(text="10", callback_data="10")
keyboard.row(btn1, btn2, btn3, btn4, btn5)
keyboard.row(btn6, btn7, btn8, btn9, btn10)
bot.send_message(
message.chat.id, "some text", reply_markup=keyboard)
else:
keyboard2 = types.InlineKeyboardMarkup()
btn11 = types.InlineKeyboardButton(text="Yes", callback_data="yes")
btn12 = types.InlineKeyboardButton(text="No", callback_data="no")
keyboard2.add(btn11, btn12)
bot.send_message(message.from_user.id, "some text", reply_markup=keyboard2)
#bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.message:
if call.data == "1":
img = open('1.jpg', 'rb')
bot.send_photo(chat_id=call.message.chat.id, photo=img, reply_markup=keyboard)
img.close()
#Nine more buttons here
elif call.data == "yes":
bot.send_message(message.chat.id, "some text", reply_markup=keyboard)
elif call.data == "no":
bot.send_message(message.from_user.id, "some text")