Z
Z
Zero None2020-09-09 09:54:36
Python
Zero None, 2020-09-09 09:54:36

How to accept input arguments in user message in Telegram API?

I need the bot to accept arguments (text after the command in the bot) entered by the user immediately after the command, like in other Telegram bots. I am using pyTelegramBotApi library.
Here is a piece of code:

code = ''
@bot.message_handler(content_types=['text'])
def echo(message):
    if message.text == "тест":
        global code
        bot.register_next_step_handler(message, code_edit)

def code_edit(message):
    global code
    code = message.text


The problem is that if you use this method, then in groups, when you entered the command, you can be interrupted, and a different text will be recorded, not your message. Is there a way to immediately write an argument after the command?
For example: test (text to edit here)
And it immediately wrote the text after the command?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lesha, 2020-09-09
@ZERRITO

Maybe not the best option, but I use something like this in my php:

>>> msg = '/cmd ping pong argumenty'
>>> args = msg.split(' ') ## переводим в массив
>>> args
['/cmd', 'ping', 'pong', 'argumenty']
>>> del args[0] ## убираем первый элемент
>>> args = ' '.join(args) ## соединяем обратно
>>> args
'ping pong argumenty'

R
robprane, 2020-09-09
@robprane

As Steven Konrov wrote, it is possible to split the message into a command and arguments, but this is inconvenient for users and fraught with errors.
If you have a database, then you can remember the current command of each user and, depending on this, process the following messages. So the messages in the groups will not be confused.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question