Telegraf: How to check if sender in chat is admin?

Created on 2 Nov 2018  路  3Comments  路  Source: telegraf/telegraf

If I add bot to supergroup and get a command from it. For example:
bot.command('list', (ctx) => ctx.reply('Command answer'));

How can I check if admin sent this message?

Most helpful comment

Here is my concept
But there is also needed to cache admin list results somehow. May be telegraf/session help here. But I dont remember how it will work in groups & channels
And I saw, that in simple groups, where all members are admins - that query receive only creator info

const Telegraf = require('telegraf');
//const session = require('telegraf/session');
const bot = new Telegraf(process.env.BOTTOKEN);

bot.use(function(ctx, next){
    /// or other chat types...
    // if( ctx.chat.type !== 'channel' ) return next();
    if( ctx.chat.id > 0 ) return next();

    /// need to cache this result ( variable or session or ....)
    /// because u don't need to call this method
    /// every message
    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.hears('I\'m admin', function(ctx){
    console.log(ctx.from);
    if( ctx.from._is_in_admin_list ){
        return ctx.reply('Yep!');
    } else {
        return ctx.reply('No, you don\'t');
    }
});

////....
bot.startPolling();

All 3 comments

Hello, @ookldev

May be your should write your own middleware.
Which run telegram.getChatAdministrators(chatId) => Promise query and cache that result in session before each chat message, for example

@tva10 can you give me an example please?

Here is my concept
But there is also needed to cache admin list results somehow. May be telegraf/session help here. But I dont remember how it will work in groups & channels
And I saw, that in simple groups, where all members are admins - that query receive only creator info

const Telegraf = require('telegraf');
//const session = require('telegraf/session');
const bot = new Telegraf(process.env.BOTTOKEN);

bot.use(function(ctx, next){
    /// or other chat types...
    // if( ctx.chat.type !== 'channel' ) return next();
    if( ctx.chat.id > 0 ) return next();

    /// need to cache this result ( variable or session or ....)
    /// because u don't need to call this method
    /// every message
    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.hears('I\'m admin', function(ctx){
    console.log(ctx.from);
    if( ctx.from._is_in_admin_list ){
        return ctx.reply('Yep!');
    } else {
        return ctx.reply('No, you don\'t');
    }
});

////....
bot.startPolling();
Was this page helpful?
0 / 5 - 0 ratings