Node-telegram-bot-api: How to check if sender is admin?

Created on 18 Oct 2017  路  9Comments  路  Source: yagop/node-telegram-bot-api

I added bot to supergroup and created event that listen all messages.

bot.on('message', (msg) => {
  const chatId = msg.chat.id;

  bot.deleteMessage(chatId, msg.message_id);
});

How can I check if admin sent this message?

question

Most helpful comment

I use this workaround and seems to work.

bot.getChatMember(message.chat.id, message.from.id).then(function(data) {
    if ((data.status == "creator") || (data.status == "administrator")){
        // I'm admin or creator, so delete message
    }
});

All 9 comments

I use this workaround and seems to work.

bot.getChatMember(message.chat.id, message.from.id).then(function(data) {
    if ((data.status == "creator") || (data.status == "administrator")){
        // I'm admin or creator, so delete message
    }
});

Where do you add that paragraph? It does not work for me

@DestroyerIV

bot.onText(/^\/ban/, function(message, match) {
    bot.getChatMember(message.chat.id, message.from.id).then(function(data) {
        if ((data.status == "creator") || (data.status == "administrator")){
            bot.sendMessage(message.chat.id, "I'm admin!");
        }else{
            bot.sendMessage(message.chat.id, "I'm not admin");
        }
    });
});

Thanks bro, How can I join it to kickChatMember?

bot.onText(/^\/ban/, function(message) {
    // Easy way is use this command via reply, so:
    if (message.reply_to_message == undefined){
        // Not used via reply
        return;
    }
    var username = message.reply_to_message.from.username;
    var userid = message.reply_to_message.from.id;
    bot.getChatMember(message.chat.id, message.from.id).then(function(data) {
        if ((data.status == "creator") || (data.status == "administrator")){
            bot.kickChatMember(message.chat.id, userid).then(result => {
                bot.sendMessage(message.chat.id, username + " has been banned!");
            });
        }
    });
});

Thanks bro, good job. I am totally grateful

Thanks @sidelux !

Can this be closed then?

I think yes ;)

I have a question. Example I ban a person, then I use /unban to remove the ban. But if that person is inside the group and is not banned by using the command /unban the user receiver ban. I leave the code:

bot.onText(/^\/unban/, function(message) {
    if (message.reply_to_message == undefined){
        return;
    }
    var username = message.reply_to_message.from.username;
    var userid = message.reply_to_message.from.id;
    bot.getChatMember(message.chat.id, message.from.id).then(function(data) {
        if ((data.status == "creator") || (data.status == "administrator")){
            bot.unbanChatMember(message.chat.id, userid).then(result => {
                bot.sendMessage(message.chat.id, message.from.first_name + " has been unbaned!");
            });
        }
    });
});

Could you tell me how it can be solved? And if it is an insure?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rezania815 picture Rezania815  路  3Comments

Lemmmy picture Lemmmy  路  3Comments

Panthro picture Panthro  路  3Comments

alikhil picture alikhil  路  3Comments

kenanchristian picture kenanchristian  路  4Comments