U
U
Ulukman Amangeldiev2020-06-09 18:46:26
Python
Ulukman Amangeldiev, 2020-06-09 18:46:26

How to interrupt listener in pyTelegramBotAPI?

Hello. I am using pyTelegramBotAPI. I searched all over Google and found one single question that remained unanswered on some IT forum where a person had the same problem as me. And so, to the essence of the question: my bot should ask the user to send a message using the /add_my_question command, the text of which will be the question to be added. I did it via bot.set_update_listener() but it handles all messages after /add_my_question. And I need that only the first message is processed and the listener is interrupted. How can i do this? Perhaps there are some other ways to implement this?

def listener(messages):
    for m in messages:
        chat_id = m.chat.id 
        text = m.text
        """Дальше бот берет текст первого сообщения и добавляет его в БД, 
        а второе сообщение, которое он получит, уже должен будет игнорировать.
        Но мой бот сейчас отвечает на все сообщения, а не только на первое"""

@bot.message_handler(commands=["add_my_question"])
def add_question(message):
    chat_id = message.chat.id
    bot.send_message(chat_id, "Напишите вопрос который хотите добавить: ") # --- Вот после этого сообщения пользователь должен отправить свой вопрос в одном сообщении
    bot.set_update_listener(listener)


if __name__ == "__main__":
    bot.infinity_polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-09
@Atageldiev

Better use register_next_step_handler()
https://github.com/eternnoir/pyTelegramBotAPI/blob...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question