Answer the question
In order to leave comments, you need to log in
(aiogram) How to disable access to the inline button to other users than the specified one?
I apologize in advance for the stupid question. See what the problem is: I am writing a "Russian Roulette" bot, after the command you need to put the nickname of your opponent, after which a message with two buttons is displayed in the chat. One "Accept", the other "Do not accept". It is necessary to make it so that when the button is pressed, the bot checks the ID and compares it with the specified one. The required line is:
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from config import TOKEN
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands="ruletkaid")
async def inlinebtn(message: types.Message):
user1 = str(message.get_args())
user2 = str(message.from_user.username)
return user1
keyboard = types.InlineKeyboardMarkup()
keyboard.add(types.InlineKeyboardButton(text="Принять", callback_data="random_value"))
keyboard.add(types.InlineKeyboardButton(text="Не принимать", callback_data="da", ))
await message.answer(user1 + ', вас вызывает на дуэль ' + user2, reply_markup=keyboard)
@dp.callback_query_handler(text="random_value")
async def send_random_value(call: types.CallbackQuery):
global user1
userid = call.message.from_user.username
if userid != user1:
await call.answer(text="Не тебе адресовано.", show_alert=True)
return False
await call.answer(text="Код сработал.", show_alert=True)
@dp.callback_query_handler(text="da")
async def send_random_value(call: types.CallbackQuery):
await call.reply.edit(text="Вы не приняли предложение противника.")
Answer the question
In order to leave comments, you need to log in
return user1
here it at all did not understand
Place username of users in callback_data. In the handler, get their username from data and compare it with the one who clicked the button.
@dp.message_handler(commands="ruletkaid")
async def inlinebtn(message: types.Message):
user1 = message.get_args()
user2 = message.from_user.username
keyboard = types.InlineKeyboardMarkup()
keyboard.add(types.InlineKeyboardButton(text="Принять", callback_data=f"accept_{user1}:{user2}"))
keyboard.add(types.InlineKeyboardButton(text="Не принимать", callback_data=f"decline_{user1}:{user2}"))
await message.answer(user1 + ', вас вызывает на дуэль ' + user2, reply_markup=keyboard)
@dp.callback_query_handler(lambda call: call.data.startswith('accept_'))
async def accept_duel(call: types.CallbackQuery):
user1, user2 = call.data.replace('accept_', '', 1).split(':')
if call.from_user.username != user1:
await call.answer(text="Не тебе адресовано.", show_alert=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question