D
D
Deduwka2021-08-31 14:16:10
Python
Deduwka, 2021-08-31 14:16:10

How to make a button in a telegram bot a command?

Hello everyone, I'm trying my hand at writing bots. I got to the creation of keyboards and then ran into a dead end. I figured out how to add a keyboard with a replica, but I can’t find an answer on how to make it so that when you click on the keyboard button, a specific command is sent to the bot, not text. Here is my code:

import config
import telebot
from telebot import types

bot = telebot.TeleBot(config.token)



@bot.message_handler(commands=["start"])
def start(message):
  kb = types.ReplyKeyboardMarkup()
  kb.row('Реєстрація')
  bot.send_message(message.chat.id, "Вітаю, я Ваш особистий помічник. Для подальшої роботи потрібно зареєструватися. Для реєстрації натисніть на вказівку з меню, або введіть команду /register", reply_markup=kb)


When you click on the button, the bot leaves only the text of the button, but you need to send the "/register" command, while the name of the button should remain.
Is it possible to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-08-31
@Deduwka

The Reply keyboard simply sends the text from the button, without any commands. You can simply do this:

@bot.message_handler(regexp='^Реєстрація$')
@bot.message_handler(commands=['register'])
def register(message):

S
Stefan, 2021-08-31
@MEDIOFF

ReplyKeyboard - sends what is written on it, and there is no other way, try to use InlineKeyboard, and process callback_data then no messages will be sent, and you can write anything on the button
PS: Or explicitly process 'Registration' instead of / register

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question