Node-telegram-bot-api: create whitelist for my bot

Created on 9 Oct 2018  路  26Comments  路  Source: yagop/node-telegram-bot-api

Question

I only need certain people to have access to my bot, do you have any idea how i would be able to get my bot to respond only to the people that are in my list?

All 26 comments

Use a condition.
If (msg.chat.id === ?){

}

Or message.from.id :)

@saeedhei i already tried to use an if codition and it does allow me to keep out people by saying access denied but when the part of the if that should allow me to enter gives me a polling error.

@sidelux could you help me with an example, i read a little bit about that but i cant fine the exact way to use it

Uhm, easy example:

function checkPermission(userid){
    var permissionArray = [123,456,789];
    if (permissionArray.indexOf(userid) > -1)
        return 1;
    return 0;
}

bot.onText('\/command', function (message) {
    if (!checkPermission(message.from.id)){
        bot.sendMessage(message.chat.id, "Permission denied for this function");
        return;
    }
    // function...
});

@sidelux thanks i will try that, so basically i should put if (!checkPermission(message.from.id)) in every command of my bot and i would assume that the "else" sentence of this line would be my real response in case de the user is in my permission array

Yep, alternatively use "return" so user exit from that function instead of else, but is your choice.

@sidelux it gives me a polling error, i made a little bot with /start command this is the example i made could you help me to see if i did something wrong

const TelegramBot = require('node-telegram-bot-api');

const utf8 = require('utf8');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'TOKEN_HERE';

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});

function checkPermission(userid){
var permissionArray = [123,456,789];
if (permissionArray.indexOf(userid) > -1)
return 1;
return 0;
}

bot.onText(/\/start/, (msg) => {

if (!checkPermission(message.from.id)){
bot.sendMessage(message.chat.id, "Permission denied for this function");
}
else
{
bot.sendMessage(message.chat.id, "Hello world");
}
});

bot.onText(/\/restart/, (msg) => {

if (!checkPermission(message.from.id)){
bot.sendMessage(message.chat.id, "Permission denied for this function");
}
else
{
bot.sendMessage(message.chat.id, "Hello world");
}
});

(next time use 'insert code' button ;))
In bot.onText(//start/, (msg) => { the regex /start is not valid, you should use \/start and i suggest to use ^\/start to say "at start of string".
So:
bot.onText(/^\/start/, (msg) => {

@sidelux actually i do use \/ start in the way you say i tried yo use de insert code button bo i dont know why it dosent work would it be ok if i attach mi code file ?

Select all of you code, then click button and it add some quotes...

const TelegramBot = require('node-telegram-bot-api');
const mysql = require('mysql');
const utf8 = require('utf8');


// replace the value below with the Telegram token you receive from @BotFather
const token = 'TOKEN';

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});



function checkPermission(userid){
  var permissionArray = [123,456,789];
  if (permissionArray.indexOf(userid) > -1)
      return 1;
  return 0;
}


bot.onText(/^\/start/, (msg) => {

  if (!checkPermission(message.from.id)){
        bot.sendMessage(message.chat.id, "Permission denied for this function");
  }
  else
  {
    bot.sendMessage(message.chat.id, "Hello world");
      }
});


bot.onText(/^\/restart/, (msg) => {

  if (!checkPermission(message.from.id)){
        bot.sendMessage(message.chat.id, "Permission denied for this function");
  }
  else
  {
    bot.sendMessage(message.chat.id, "Hello world");
      }
});

@sidelux sorry this is my first time in github im still trying to learn everything

Don't worry, try to write in a single post or edit (three point on top right of message box) it to avoid spam :)
First:
you missed function and used => from onText so, replace
bot.onText(/^\/restart/, (msg) => {
with
bot.onText(/^\/restart/, function (msg) {

Second:
You have used msg on onText and message inside, you should use same variable.
So replace all message with msg or reverse.

Ok i made the change but it stills keeps giving me a polling error

Try only with this code:

const TelegramBot = require('node-telegram-bot-api');
const mysql = require('mysql');
const utf8 = require('utf8');

const token = 'TOKEN';
const bot = new TelegramBot(token, {polling: true});

bot.on('message', function (message) {
    console.log(message);
});

yep it works prints all the info in the console

Try only with this code:

const TelegramBot = require('node-telegram-bot-api');
const mysql = require('mysql');
const utf8 = require('utf8');

const token = 'TOKEN';
const bot = new TelegramBot(token, {polling: true});

function checkPermission(userid){
    var permissionArray = [123,456,789];
    if (permissionArray.indexOf(userid) > -1)
        return 1;
    return 0;
}

bot.on('^\/test', function (message) {
    console.log(checkPermission(message.from.id));
});

now it dosent print anything, it dosent give me an error either

If you write /test in your bot you see nothing in console?

nop, i tried with and without "/" and it dosent show anything

Uh ops my fault, replace with that:

bot.onText(/^\/test/i, function (message) {
    console.log(checkPermission(message.from.id));
});

No problem, now the console shows a "0" when i press /test

Now try with /start:

const TelegramBot = require('node-telegram-bot-api');
const mysql = require('mysql');
const utf8 = require('utf8');

const token = 'TOKEN';
const bot = new TelegramBot(token, {polling: true});

function checkPermission(userid){
    var permissionArray = [123,456,789];
    if (permissionArray.indexOf(userid) > -1)
        return 1;
    return 0;
}

bot.onText(/^\/start/i, function (message) {
    if (checkPermission(message.from.id) == 0){
        bot.sendMessage(message.chat.id, "Permission denied");
        return;
    }
    bot.sendMessage(message.chat.id, "Hello world");
});

Dosent show anything i tried adding the change yo did before /^\/start/i but still anything

I updated it 4 times, try again

It worked! thank you so much for your time, i added my id to the array and it does give me access

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rezania815 picture Rezania815  路  3Comments

sidelux picture sidelux  路  3Comments

antonrifco picture antonrifco  路  3Comments

saeedhei picture saeedhei  路  4Comments

Hostile picture Hostile  路  3Comments