R
R
RIFaTqq2021-12-15 14:56:15
Python
RIFaTqq, 2021-12-15 14:56:15

Why does the telegram bot not accept private messages if you start chatting with it?

I start communicating with the bot in a telegram conversation, then it goes into private messages and writes to the user in PM, but when the user answers the bot in PM, the bot does not understand what the user is writing and fulfills the else condition, if the user writes in the conversation, then he will go further according to the script, how to make the communication completely in PM?
the code:

elif re.findall(r'.*роутер.*', message.text):
        bot.reply_to(message, "Давайте перейдём в личные сообщения и продолжим обсуждение там")
        bot.send_message(userID, 'Давайте заполним заявку, ответьте на следующие вопросы:')
        bot.send_message(userID, 'Ваш номер договора: ')
        bot.register_next_step_handler(message, dogovor, userID)
     else:
        bot.send_message(message.from_user.id, "я не понял, напиши '/help', чтобы посмотреть список команд")
def dogovor(message, userID):  # получаем фамилию
    print(message.text)
    global num_dog
    num_dog = message.from_user.id.text
    bot.send_message(userID, 'Какая у тебя фамилия?')
    bot.register_next_step_handler(message, get_surname, userID)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-15
@RIFaTqq

1. The bot cannot be the first to write to the user, first the user needs to start a dialogue with the bot. This is for the future
2. You bind the next step of the user in the same chat where the correspondence is conducted, i.e. in conversation. The bot is waiting for a message there, and the user writes to the PM. Use bot.register_next_step_handler_by_chat_id(). The first argument it takes is the ID of the chat where the bot will wait for a message. Accordingly, there you need to put the user ID. Outcome:
Replace

bot.register_next_step_handler(message, dogovor, userID)
on the
bot.register_next_step_handler_by_chat_id(userID, dogovor)

And you don't need to make global variables. If you want to pass user information further, also pass it through arguments to register_next_step_handler. I recommend creating at least a dictionary of the form . The result will be something like this:{'number': 123, 'surname': 'surname'}
def dogovor(message):  # получаем фамилию
    num_dog = message.text
    user_info = {'num_dog': num_dog}
    bot.send_message(message.chat.id, 'Какая у тебя фамилия?')
    bot.register_next_step_handler(message, get_surname, user_info)

def get_surname(message, user_info):  # получаем фамилию
    user_info['surname'] = message.text
    print('Фамилия:', user_info['surname'], '. Номер договора:', user_info['dog_num'])

ps: for the future - wrap your code with the appropriate < / > button

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question