How do I make it so ReplyKeyboardMarkup() button executes a command for me for telegram bot?

Question:

So I am making a trading bot the user first have to type "/BUY" for the first command.
And every user have some settings saved up in files so I want them to choose to buy with old settings or new ones.
I tried to put a command instead of a text but that was impractical because anyone can use it without having to go the first one
This is my code for now:

from telebot import types
import telebot
from decouple import config
BOT_TOKEN = config('BOT_TOKEN')
bot = telebot.TeleBot(BOT_TOKEN)


@bot.message_handler(commands=['Start'])
def msg_hndl(message):
    print(message)
    markup =types.ReplyKeyboardMarkup(one_time_keyboard=True,selective=True)
    markup.row_width = 2
    button1 =types.InlineKeyboardButton(text="/BUY",callback_data="test",selective=True)
    markup.add(button1)
    bot.send_message(message.chat.id,"Choose @"+message.from_user.username, reply_markup=markup)


@bot.message_handler(commands=['BUY'])
def msg_hndl(message):
    markup2 = types.ReplyKeyboardMarkup(one_time_keyboard=True,selective=True)
    markup2.row_width = 2
    button1 = types.InlineKeyboardButton(text="/New_settings", callback_data="test")
    button2 = types.InlineKeyboardButton(text="/Old_settings", callback_data="test")
    markup2.add(button1,button2)
    bot.send_message(message.chat.id,"Choose @"+message.from_user.username, reply_markup=markup2)

@bot.message_handler(commands=['New_settings'])
def msg_hndl(message):
    #do something
    
bot.polling()
Asked By: Axel Crk

||

Answers:

So I did the research myself and saw the only solution is to save messages and have a message handler that saves the user id and compare it everytime.

Answered By: Axel Crk

You have created buttons using types.InlineKeyboardButton(). But this buttons are for types.InlineKeyboardMarkup() not types.ReplyKeyboadMarkup().
Rewrite your coding using types.KeyboardButton(text). See https://pytba.readthedocs.io/en/latest/types.html#telebot.types.KeyboardButton for details.

Answered By: Subham Saha
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.