How to send a message when click in an InlineKeyboardButton (Telegram & Python) - python

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")

Related

telegram bot reply message for buttons

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)

get user from chat telegram, pyTelegramBotAPI

I am using the library pyTelegramBotAPI
How can I get the user ID from the chat?
Not using a message from the user.
I know the method message.from_user.id
I need to get the id on the line
#bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
toId = call.message.chat.id
if call.data == Buttons.next:
bot.send_message(toId, Buttons.next)
elif call.data == Buttons.problem:
bot.send_message(toId, Buttons.problem)
elif call.data == Buttons.end:
bot.send_message(toId, Buttons.end)
elif call.data == Buttons.yes:
//Here I need get user_id

Repeat action on button click in telebot module

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):

How can i make a inline keyboardbutton with telebot for a chatterbot?

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)

Python Telegram bot - second InLineKeyboard not working

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")

Categories