'Telegram Bot Private bot [security]

I have a Telegram Bot written in javascript (node-telegram-bot-api + SailsJs). I want to create a bot that answer private information, for example, my /todayTasks or /myAgenda. Can I block people from using my bot in private chats?

I tried to use leaveChat() when the bot receives /start but it only works in group chat.

Unhandled rejection Error: ETELEGRAM: 400 Bad Request: chat member status can't be changed in private chats

My Code:

bot.onText(/\/start/, function(msg) {
    const chatId = msg.chat.id;

    CheckAuthorized(chatId).then(function(user) {
        if (!user) {
            bot.sendMessage(chatId, 'Sorry, you are not allowed to ask me.');
            bot.leaveChat(chatId); //<---- ERROR HERE!
            return;
        }

        bot.sendMessage(chatId, 'Hello my friend.');                
    });
});

Of course, I can use an Authentication Policy to run before every request, if there is no other option.



Solution 1:[1]

Unfortunately, bots can only leave group chat at this time, so the only thing you can do is just ignore them in your code :(

Solution 2:[2]

At the moment it is not possible for bots to block or ignore specific users, or everyone except a whitelist.

What I do is put the chat-id of every user or group I want my bot to work in into an array.

On every received message the bot then checks if the chat-id is in that array, if it's not it simply exits the function.

Here is an example for python-telegram-bot:

whitelist = [-10012344586,-2457841,-100554879472]

def on_message(bot, update):
   if not update.message.chat_id in whitelist:
      return

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sean
Solution 2 confetti