I
I
Igroman2282021-03-13 23:40:29
Python
Igroman228, 2021-03-13 23:40:29

How to compare the id of the person who clicked on the inline button?

How to get the ID of the person who pressed the button and compare it with the person who this button is intended for.

@bot.message_handler(content_types=["new_chat_members"])
def send_test(message):
   bot.delete_message(message.chat.id, message.message_id)
   bot.restrict_chat_member(message.chat.id, message.from_user.id)
   Knopka = types.InlineKeyboardMarkup()
   knop1 = types.InlineKeyboardButton(text = '', callback_data = 'Ok')
   knop2 = types.InlineKeyboardButton(text = '', callback_data = 'No')
   Knopka.add(knop1, knop2)
   name = message.from_user.first_name
   bot.send_message(message.chat.id, name + ', добро пожаловать в чат \nчтобы получить доступ к чату нажми на эмодзи \'\' ниже', reply_markup = Knopka)


@bot.callback_query_handler(func = lambda call: True)
def answer(call):
  if call.data == "Ok":
    name = call.from_user.first_name
    bot.restrict_chat_member(call.message.chat.id, call.from_user.id, can_send_messages = True, can_send_media_messages = True, can_send_polls = True, can_send_other_messages = True, can_add_web_page_previews = True)
    bot.restrict_chat_member(call.message.chat.id, call.from_user.id, until_date=time()+300)
    bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text= name +  ' доступ к чату откроется через 5 минут а пока вы можете ознакомится с правилами')
  elif call.data == "No":
    name = call.from_user.first_name
    bot.kick_chat_member(call.message.chat.id, call.from_user.id)
    bot.restrict_chat_member(call.message.chat.id, call.from_user.id, can_send_messages = True, can_send_media_messages = True, can_send_polls = True, can_send_other_messages = True, can_add_web_page_previews = True)
    bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text= name + ', не прошел капчу и был кикнут из чата')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igroman228, 2021-03-15
@Igroman228

In general, here is the solution:
It was necessary to create a global variable
AND place it in 1 function

S
shurshur, 2021-03-15
@shurshur

If all this is done in a chat with other people (which is somewhat strange, I think), then you can, for example, pass the user id to callback_data (for example, in the form of ok-12345 and no-12345, respectively), and then compare the id from the call. data with call.from_user.id value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question