pyTelegramBotAPI icon indicating copy to clipboard operation
pyTelegramBotAPI copied to clipboard

Arguments for register_next_step_handler

Open Mattze96 opened this issue 11 years ago • 10 comments

Hello,

I want to be able to pass arguments to my callback function.

register_next_step_handler(message, callback, args*) ???

This would be very usefull, because a global dictionary with data is unnecessary then.

Thanks, Matthias

Mattze96 avatar Sep 12 '15 21:09 Mattze96

I think that support pass arguments would let next_step_handler to become too complicated. Now I don't have plan to support this feature.

eternnoir avatar Sep 20 '15 03:09 eternnoir

@Mattze96 you can use lambdas as workaround: bot.register_next_step_handler(msg, lambda m: callback(m, args*))

stmotw avatar Sep 28 '15 11:09 stmotw

Ahh, thanks!

I will try that...

Am 28.09.2015 um 13:40 schrieb Timofey Urbanovich:

@Mattze96 you can use lambdas as workaround: bot.register_next_step_handler(msg, lambda m: callback(m, args*))


Reply to this email directly or view it on GitHub: https://github.com/eternnoir/pyTelegramBotAPI/issues/72#issuecomment-143719642

Mattze96 avatar Sep 28 '15 19:09 Mattze96

Nice solution!

pevdh avatar Sep 30 '15 16:09 pevdh

Is this solution works fine in your projects? I have a problem with it: https://github.com/eternnoir/pyTelegramBotAPI/issues/395 Unfortunately, I can't refuse to use lamda :((( I really need to pass arguments to process_next_step, but sometimes message.text becomes None.

Gooman-rus avatar Sep 12 '17 14:09 Gooman-rus

@stmotw, thanks a lot in half an hour and I began to cry, hugging my knees

Championloo avatar Oct 02 '17 11:10 Championloo

Русским помогу тоже, правильно будет code = bot.send_message(message.chat.id,'Введите потоки') bot.register_next_step_handler(code, lambda m: threadd(code, text,)) в функции же def threadd(message,text):

olegoleg1111121 avatar May 19 '20 08:05 olegoleg1111121

Русским помогу тоже, правильно будет code = bot.send_message(message.chat.id,'Введите потоки') bot.register_next_step_handler(code, lambda m: threadd(code, text,)) в функции же def threadd(message,text):

Cпасибо, ахах как раз искал и нашел, можно ли без bot.reply_to делать)

ddarkwing avatar May 23 '20 03:05 ddarkwing

Русским помогу тоже, правильно будет code = bot.send_message(message.chat.id,'Введите потоки') bot.register_next_step_handler(code, lambda m: threadd(code, text,)) в функции же def threadd(message,text):

Так не получилось, в функцию не передается сообщение введенное пользователем, вместо него в message передается сообщение из отправляющего метода.

Зато заработало так, оно и проще выглядит:

def first_poll(cu)
    msg = bot.send_message(cu.cid, get_lang_phrase('how_much', cu.language))
    bot.register_next_step_handler(msg, process_get_money, cu)

def process_get_money(message, cu)
    m = message.text
    if m.isdigit():
        amount = float(m)
        msg = bot.send_message(cu.cid, get_lang_phrase('how_long', cu.language))
        bot.register_next_step_handler(msg, process_get_term, cu, amount)

def process_get_term(message, cu, amount)
    m = message.text
    if m.isdigit():
        term = int(float(m))
        ra = calculate_return_amount(amount, term)
        send_calculated_results(cu, ra)

sumarokov-vp avatar Sep 11 '20 03:09 sumarokov-vp

Русским помогу тоже, правильно будет code = bot.send_message(message.chat.id,'Введите потоки') bot.register_next_step_handler(code, lambda m: threadd(code, text,)) в функции же def threadd(message,text):

Так не получилось, в функцию не передается сообщение введенное пользователем, вместо него в message передается сообщение из отправляющего метода.

Зато заработало так, оно и проще выглядит:

def first_poll(cu)
    msg = bot.send_message(cu.cid, get_lang_phrase('how_much', cu.language))
    bot.register_next_step_handler(msg, process_get_money, cu)

def process_get_money(message, cu)
    m = message.text
    if m.isdigit():
        amount = float(m)
        msg = bot.send_message(cu.cid, get_lang_phrase('how_long', cu.language))
        bot.register_next_step_handler(msg, process_get_term, cu, amount)

def process_get_term(message, cu, amount)
    m = message.text
    if m.isdigit():
        term = int(float(m))
        ra = calculate_return_amount(amount, term)
        send_calculated_results(cu, ra)

Really. You are a genius!

Nappy-Says avatar Apr 27 '22 20:04 Nappy-Says