M
M
MrChen2017-07-22 20:51:19
Python
MrChen, 2017-07-22 20:51:19

How to get command value in Telebot Python?

Hello! I am writing a Telegram bot and I use the Telebot library in Python to work with it. As you know, in Telegram, along with the command, you can transfer some value:
/command value
Actually, the question is: is there a method in Telebot that receives this very value?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Belousov, 2017-07-23
@MrChen

The method that xdgadd suggested is not very convenient, because for each command you need to count the length in characters, and if several commands of different lengths perform the same function, then this method stops working, therefore:

@bot.message_handler(commands=["command"]) # В commands может быть несколько разных команд
def answer(message):
    command = message.split(maxsplit=1)[1] # В переменной будет всё,что идёт после /command

X
xdgadd, 2017-07-22
@xdgadd

No. But you can just get the text of the message and manually extract the command from it:

GET_LEN = 5 # '/get '
...
@bot.message_handler(commands=["get"])
def get_handler(message):
    msg_text = message.text[GET_LEN:]
    print(msg_text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question