Command with arguments
- Version of pyTelegramBotAPI: pyTelegramBotAPI==3.0.1
- OS: Win 10 x64
- Version of python: 2.7/3.6.1
I want to create a command with arguments
Example:
/activate 1
/run programm
how to do it?
SOLUTION - https://github.com/eternnoir/pyTelegramBotAPI/issues/358#issuecomment-309569184
def extract_arg(arg): return arg.split()[1] if len(texto.split()) > 1 else ""
@bot.message_handler(commands=['run']) def start(m): arg=extract_arg(m.text): subprocess.call('run', 'arg')
p.s: you must indent the code p.s.2: if you want more than one arg, you can split the text after /run
@alfilesnet, what is texto.split() in
return arg.split()[1] if len(texto.split()) > 1 else ""
texto - ?
Solution:
def extract_arg(arg):
return arg.split()[1:]
@bot.message_handler(commands=['yourCommand'])
def yourCommand(message):
status = extract_arg(message.text)
/yourCommand 1
status = ['1']
/yourCommand ff
status = ['ff']
Also I can add, that you can use more than one argument:
def extract_arg(arg):
return arg.split()[1]
def extract_arg2(arg2):
return arg2.split()[2]
@bot.message_handler(commands=['x'])
def yourCommand(message):
status = extract_arg(message.text)
status2 = extract_arg2(message.text)
x = int(status2)
bot.send_message(message.chat.id, status)
bot.send_message(message.chat.id, x)
Command x = int(status2) convert char to int