Telegram Bot 私人机器人 [安全]

Telegram Bot Private bot [security]

我有一个用 javascript (node-telegram-bot-api + SailsJs) 编写的 Telegram 机器人。我想创建一个机器人来回答私人信息,例如我的 /todayTasks 或 /myAgenda。我可以阻止人们在私人聊天中使用我的机器人吗?

我尝试在机器人收到 /start 时使用 leaveChat(),但它只适用于群聊。

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

我的代码:

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.');                
    });
});

当然,如果没有其他选择,我可以在每个请求之前使用身份验证策略运行。

不幸的是,机器人此时只能离开 group 聊天,所以你唯一能做的就是在你的代码中忽略它们:(

目前,机器人无法阻止或忽略特定用户,或除了白名单之外的所有人。

我所做的是将我希望我的机器人工作的每个用户或组的 chat-id 放入一个数组中。

在收到的每条消息中,机器人都会检查 chat-id 是否在该数组中,如果不在,它会简单地退出该函数。

这是 python-telegram-bot 的示例:

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

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