A
A
Aibot922021-04-09 16:05:43
PHP
Aibot92, 2021-04-09 16:05:43

How to stop "listening" to bot.*_handler?

Good day everyone!
I'll tell you in advance I rummaged through the whole Google and could not find the answer. I am
writing my first telegram bot using
pyTelegrambotAPI

@bot.message_handler(commands=['rename'])
def rename (massage):
    bot.send_message(massage.from_user.id, f'Привет , твое имя в телеграмм: {massage.from_user.first_name} \n'
                                           f'На какое хотите заменить?')
    bot.register_next_step_handler(massage,save_name)
def save_name (massage):
    global user_name
    user_name = massage.text
    quest = f'хорош, тепеь я буду называть Вас {user_name}, сохраняем?'
    keybord = keybord_yes_or_no()
    bot.send_message(massage.from_user.id, text= quest, reply_markup=keybord)
@bot.callback_query_handler(func=lambda call: True )
def rename_seve(call):
    if call.data == 'yes':
        global users
        users = ({
            'id': call.from_user.id,
            'name': user_name
        })
        bot.send_message(call.message.chat.id, f"Теперь для Вашего id {users['id']} будет сохранено имя {users['name']}")
    elif call.data == 'no':
        bot.send_message(call.from_user.id, 'Ок ничего не сохраняю')


and let's say I call the keyboard elsewhere:
@bot.message_handler(func=lambda message: sp.spelled(message.text.lower()) == "привет")
def hi (massage):
    bot.send_message(massage.chat.id, f"Привет ")
    keybord_1 = keybord_yes_or_no()
    q= '>>>>>>'
    bot.send_message(massage.chat.id,text=q, reply_markup=keybord_1)
@bot.callback_query_handler(func=lambda call: True)
def requset(call):
    if call.data == 'yes':
        bot.send_message(call.from_user.id, '>>>>????')
    elif call.data == 'no':
        bot.send_message(call.chat.id, '>>>>>!!!')


And of course it "listens" to the first decorator

. Question:
How to stop listening to the first decorator or the function itself so that it goes to the second one and starts listening to input on it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Agelios, 2018-06-06
@Agelios

Well, the error is obviously here: R::store($user);
and to help solve it, you should throw off the code of this function.
And also read php.net/manual/ru/book.filter.php , it will make life easier

A
Alexa2007, 2021-04-09
@Aibot92

In this version, I think instead of decorators, you should use the familiar bot.register_next_step_handler(massage,save_name) method . In the example from the github , you can see how the question is what gender? and two buttons m and w are added , and in the message where they are sent, they are deleted, and the response is processed as text.

def process_age_step(message):
    try:
        chat_id = message.chat.id
        age = message.text
        if not age.isdigit():
            msg = bot.reply_to(message, 'Age should be a number. How old are you?')
            bot.register_next_step_handler(msg, process_age_step)
            return
        user = user_dict[chat_id]
        user.age = age
        markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
        markup.add('Male', 'Female')
        msg = bot.reply_to(message, 'What is your gender', reply_markup=markup)
        bot.register_next_step_handler(msg, process_sex_step)
    except Exception as e:
        bot.reply_to(message, 'oooops')


def process_sex_step(message):
    try:
        chat_id = message.chat.id
        sex = message.text

I would also like to suggest making changes to the code, in such cases:
keybord = keybord_yes_or_no()
    bot.send_message(massage.from_user.id, text= quest, reply_markup=keybord)

If you generate buttons in this way, there will be one line less:
def day_btns():
  days = types.InlineKeyboardMarkup(row_width=7)
  days.add(*[types.InlineKeyboardButton(text='Day '+str(i),callback_data='call'+str(i)) for i in range(1,8)])
  return days

#Пример использования
bot.send_message(message.chat.id,'Days of week',reply_markup=day_btns())
#Хватает и одной строки

Also, if you are interested , here is a complete sample of my test bot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question