pyTelegramBotAPI icon indicating copy to clipboard operation
pyTelegramBotAPI copied to clipboard

Command with arguments

Open BoberMod opened this issue 9 years ago • 4 comments

  1. Version of pyTelegramBotAPI: pyTelegramBotAPI==3.0.1
  2. OS: Win 10 x64
  3. 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

BoberMod avatar Jun 18 '17 22:06 BoberMod

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 avatar Jun 19 '17 15:06 alfilesnet

@alfilesnet, what is texto.split() in return arg.split()[1] if len(texto.split()) > 1 else "" texto - ?

BoberMod avatar Jun 19 '17 17:06 BoberMod

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']

BoberMod avatar Jun 19 '17 20:06 BoberMod

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

ghost avatar Mar 01 '20 18:03 ghost