P
P
PythonGen2020-10-13 15:43:15
Python
PythonGen, 2020-10-13 15:43:15

How can I make it give the user a response when the Inline button is clicked?

There is a bot that collects information about users, then issues a button - send, when it is pressed, all the filled information is sent to the admin chat, and 2 Inline buttons are issued - Accept, Reject
The error is that the bot does not understand who specifically to issue a message.
When you click accept, it should show - thank you, your application has been processed.
When you click Reject, it should show - thank you, but your application was rejected.
Preferably with an example on the telebot library.
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
agent_2203, 2020-10-13
@agent_2203

To do this, you need to send call-back information, let's say we initially know to whom we will respond, that is, the user ID, and then we accept and, depending on the button pressed, we understand who to send the message to

user_id = 123456 # айди пользователя кому будем отправлять
chat_id = -123456 # айди чата куда будем писать

# отправляем сообщение в чат
message_markup = InlineKeyboardMarkup()
message_markup.row_width = 1
message_markup.add(
     InlineKeyboardButton(
          "Принять",
           callback_data="success_" + str(user_id)
     ),
     InlineKeyboardButton(
          "Отклонить",
           callback_data="unsuccess_" + str(user_id)
     ))
bot.send_message(
    chat_id=chat_id,
    text="Новая заявка",
    reply_markup=message_markup,
    parse_mode="Html"
)

# принимаем кал-бек информацию от кнопки 
def clb_handler(x):
    bot.answer_callback_query(x.id)
    if x.message.chat.id == x.from_user.id:
        cb_data = x.data.split("_")
        if len(cb_data) == 2:
               user_id = cb_data[1]
               msg = "спасибо, ваша заявка обработана" if cb_data[0] == "success" else "спасибо, но, ваша заявка была отклонена."
               bot.send_message(
                       chat_id=user_id,
                       text=msg,
                       parse_mode="Html"
                )

# запускаем бота
def start_polling():
    while True:
        try:
            # ставим что прослушивать ответы на кнопки будет функция clb_handler() 
            @bot.callback_query_handler(func=lambda call: True)
            def callback_query(x):
                clb_handler(x)

            bot.polling(
                    none_stop=True,
                    interval=0,
                    timeout=60
            )
        except Exception as e:
            print(e)
            time.sleep(5)

tb = threading.Thread(
    target = start_polling,
    args = ()
)
tb.start()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question