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):
Related
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)
I am making a chatterbot and I wonder how can I put inlinekeyboardbutton, how can I make that?
I need a code of keyboardbutton that handles a big conversation, please and thank you.
import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
TELEGRAM_TOKEN = '<TOKEN>'
bot = telebot.TeleBot(TELEGRAM_TOKEN)
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
#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.answer_callback_query(call.id, "Answer is No")
#bot.message_handler(func=lambda message: True)
def message_handler(message):
bot.send_message(message.chat.id, "Yes/no?", reply_markup=gen_markup())
bot.infinity_polling()
You can create buttons with the code below:
from telebot import types
markup = InlinKeyboardMarkup(row_width=1) # row_width: number of buttons
some_item = types.InlineKeyboardButton("text of your button",
callback_data="main info")
markup.add(some_item)
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")
I am buidling a simple game with pytelegrambotapi.
According to the rules, the user is sent a definition and four buttons with words as text, of which one is correct.
The user then should press the right button. Another routine then compares the user's answer to the right answer.
How do I retrieve the value of the button that the user pressed?
import telebot
from telebot import types
TOKEN = 'XXXXXXXXXXXXXXXXX'
bot = telebot.TeleBot(TOKEN)
kb = types.InlineKeyboardMarkup(row_width=2)
words = {
'Meager' : 'Lacking in quantity',
'Asylum' : 'Shelter',
'Irk' : 'To annoy',
'Kudos' : 'Great glory'
}
def compare(message, word, definition):
if definition == words[word]:
bot.send_message(message.from_user.id, 'Correct!')
else:
bot.send_message(message.from_user.id, 'Wrong!')
for item in words.items():
kb.add(types.InlineKeyboardButton(item[0], callback_data = item[0]))
#bot.message_handler(commands=['start', 's'])
def post_random_article(message):
word = 'Meager'
bot.send_message(message.from_user.id, word, reply_markup = kb)
bot.polling()
Nevermind, I found an answer.
#bot.callback_query_handler(func=lambda call: True)
def handle_query(call):
if call.data.split('#')[0] == call.data.split('#')[1]:
bot.send_message(call.message.chat.id, '✅ Correct!')
else:
bot.send_message(call.message.chat.id, '❌ Wrong!\n♻️ The answer is: %s' % call.data.split('#')[1])
#bot.message_handler(commands=['game', 'g'])
def game(message):
list_of_words = load_my_dictionary(message.from_user.id)
random.shuffle(list_of_words)
while not len(list_of_words) < 4:
current = list_of_words.pop()
word = current[0]
definition = current[1]
all_answers = generate_answers(word, list_of_words)
keyboard = generate_keyboard(all_answers, word)
bot.send_message(message.from_user.id, definition, reply_markup = keyboard)
time.sleep(7)
bot.send_message(message.from_user.id, '📭 List is empty!')