P
P
PosikGG2021-08-17 12:08:44
Python
PosikGG, 2021-08-17 12:08:44

How to make it so that the string is passed to another function (telebot)?

import telebot
import config

bot = telebot.TeleBot(config.TOKEN)

@bot.message_handler(content_types = ["text"])

def messages_1(message):

    bot.send_message(message.from_user.id, "Не матерись!")

def messages_2(message):

    if message.text.upper() in config.lst_7:

        bot.send_message(message.from_user.id, f"{config.lst_8[random.randint(0, len(config.lst_8) - 1)]}")
    
    elif message.text.upper() in config.lst_9:

        bot.send_message(message.from_user.id, f"{config.lst_10[random.randint(0, len(config.lst_10) - 1)]}")

    else:

        bot.register_next_step_handler(message, messages_1)

bot.polling(none_stop = True, interval = 0)


Line
bot.register_next_step_handler(message, messages_1)


Asks for a message, but does not respond to it. How can I get this string to be passed to another function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-08-17
@twistfire92

See examples of how register_next_step_handler
is used The function called in register_next_step_handler does not need to be wrapped with a decorator.
If you want to pass any parameters to the called function, then you need to add them to the parameters when calling register_next_step_handler .

@bot.message_handler(content_types = ["text"])
def messages_1(message):
    param1 = 4
    param2 = 5
    mes = bot.send_message(message.chat.id, 'Введите сообщение')
    bot.register_next_step_handler(mes, foo, param1, param2)

def foo(message, param1, param2):
    print(message.text, param1, param2)

# теперь после того как бот пришлет 'Введите сообщение' если отправить "текст сообщения", в консоли выдаст
# "текст сообщения 4 5"

While I was describing all this, I noticed that you send messages to users in a personal. When the bot is running in a group, this option may not work if the user has not started a private conversation with the bot. Try to specify message.chat.id
instead of message.from_user.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question