D
D
Doit2021-05-20 16:22:35
Python
Doit, 2021-05-20 16:22:35

Why doesn't if, else work like this?

If I write the word "text"

bot.register_next_step_handler(call.message, location)
does not work, throws anyway in
bot.register_next_step_handler(call.message, get_weather)
And how can I write that I want to transfer not the name of the city, but the location, so that when entering the location, it would be transferred to def location?
@bot.callback_query_handler(func = lambda call: True)
def answer(call):
    markup_reply = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item_geo = types.KeyboardButton(text="Отправить местоположение", request_location=True)
    item_moscow = types.KeyboardButton("Москва")
    if call.data == 'weather':
        markup_reply.add(item_geo,  item_moscow,)
        bot.send_message(call.message.chat.id, 'Напиши город или выбери из списка', reply_markup = markup_reply)
        if call.message.text == 'текст':
            bot.register_next_step_handler(call.message, location)
        else:  
            bot.register_next_step_handler(call.message, get_weather)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Krostelev, 2021-05-20
@twistfire92

You need to use InlineKeyboardMarkup and InlineButton respectively.
Jumps you to get_weather not when you write 'text', but even earlier when you press an inline button with callback_data == 'weather'. Or somewhere else that you didn't show in your question.

S
soremix, 2021-05-20
@SoreMix

Because you're using it wrong. register_next_step_handler
The first argument is the message object you get when you send it. See examples:
https://github.com/eternnoir/pyTelegramBotAPI/blob...

msg = bot.send_message(call.message.chat.id, 'Напиши город или выбери из списка', reply_markup = markup_reply)
bot.register_next_step_handler(msg, location)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question