Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit access to admins only #32

Closed
ghost opened this issue Nov 17, 2021 · 3 comments
Closed

Limit access to admins only #32

ghost opened this issue Nov 17, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Nov 17, 2021

How to limit access so the buttons don't show up for all users.

Either only the user currently pressing the buttons or all admins?

@RealPeha
Copy link
Owner

you need to send different buttons for admin and for users
for example:

if (admin) {
   ctx.reply('you are admin', Keyboard.reply(['Admin button']))
} else {
   ctx.reply('you are user', Keyboard.reply(['User button']))
}

@ghost
Copy link
Author

ghost commented Nov 18, 2021

Thank you, I tried it and i still the following issue:

  • The bot will show the buttons to all users regardless of if they are admin or not
  • The non admin users can click the buttons and the reply is given in the chat but no bot action is taken, it's ignored
  • If the admin selects any buttons the non admins can see the button menu change in realtime too

How do I stop non admins from seeing the menu at all? or limiting it to the current user interacting with it only?

Here's my code:

const main = (ctx) => {
    if( ctx.from._is_in_admin_list ){
    return ctx.reply('Select your audio list', Keyboard.reply(['Controls'], { columns: 1 }))
}
else {
}
}
bot.use(function(ctx, next){
	if( ctx.chat.id > 0 ) return next();
		return bot.telegram.getChatAdministrators(ctx.chat.id)
		.then(function(data){
			if( !data || !data.length ) return;
			console.log('admin list:', data);
			ctx.chat._admins = data;
			ctx.from._is_in_admin_list = data.some( adm => adm.user.id === ctx.from.id );
		})
		.catch(console.log)
		.then(_ => next(ctx));
});

bot.start(main)
bot.hears('Back', main)
bot.hears('Controls', (ctx) => {
    if( ctx.from._is_in_admin_list ){
    const keyboard = Keyboard.make(["/stop","/pause","/resume","/skip"], { columns: 2 })
    return ctx.reply('Controls menu selected', Keyboard.combine(keyboard, backKeyboard).reply())
}
else {
//do nothing
}
})

bot.launch()

@RealPeha
Copy link
Owner

I understand your bot works in groups. Well, in that case it seems to me that you cannot separate the buttons for the admin and for the users. But I'm not really very familiar with this, so I can't say for sure.

@ghost ghost closed this as completed Nov 23, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant