Answer the question
In order to leave comments, you need to log in
Processing a button click in a telegram bot?
I created a bot, and I'm stuck on the fact that I can't figure out how to send when I click on the button in the telegram, send a request to the server? There is a link when you go to which some action occurs, tell me how can I do so that I can send this request to the server when I click in the bot?
Answer the question
In order to leave comments, you need to log in
First, you need to decide how you display the button - the event handling methods differ from this.
There are two types of buttons - a custom keyboard that replaces the standard keyboard, or online buttons - displayed under the message. For both options, do not forget to import the library:
from telebot import types
1. Custom keyboard generation.
I made a separate function for generating the keyboard, which receives as arguments. Creating a keyboard looks like this:
keyboard = utilits.generate_keyboard('Сделать заказ', 'Хочу скидку на заказ', 'Изменить персональную информацию')
bot.send_message(message.chat.id, msg, reply_markup=keyboard)
def generate_keyboard (*answer):
keyboard = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
for item in answer:
button = types.KeyboardButton(item)
keyboard.add(button)
return keyboard
@bot.message_handler(content_types=["text"])
def check_text_message(message):
status = int()
try:
status = config.current_users[message.chat.id][0]
except Exception as er:
status = 0
if status == 0:
bot.send_message(message.chat.id, 'Извините, я запутался. Давайте начнем сначала')
first_step(message)
elif status == 10:
if message.text == 'Сделать заказ':
Dialogs.order(message) ...
keyboard = utilits.generate_inline_keyboard(['Имя', 'change_name'],
['Телефон', 'change_phone'],
['️Адрес', 'change_adress'],
['Вернуться обратно', 'back'])
def generate_inline_keyboard (*answer):
keyboard = types.InlineKeyboardMarkup()
temp_buttons = []
for i in answer:
temp_buttons.append(types.InlineKeyboardButton(text=i[0], callback_data=i[1]))
keyboard.add(*temp_buttons)
return keyboard
@bot.callback_query_handler(func=lambda call: True)
def ans(call):
try:
message = call.message
if call.data == 'change_name':
Dialogs.change_name(message)
elif call.data == 'change_phone': ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question