代码之家  ›  专栏  ›  技术社区  ›  Makah

电报机器人私人机器人[安全]

  •  1
  • Makah  · 技术社区  · 7 年前

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

    我试着用 leaveChat() 当bot收到 /start 但它只适用于群聊。

    未处理的拒绝错误:ETELEGRAM:400错误请求:无法在私人聊天中更改聊天成员状态

    我的代码:

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

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

    2 回复  |  直到 7 年前
        1
  •  3
  •   confetti    6 年前

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

    我所做的就是把 chat-id 我希望我的机器人在阵列中工作的每个用户或组。

    然后,bot会在收到的每条消息上检查 聊天室id 在该数组中,如果不是,则直接退出该函数。

    下面是一个示例 python-telegram-bot :

    whitelist = [-10012344586,-2457841,-100554879472]
    
    def on_message(bot, update):
       if not update.message.chat_id in whitelist:
          return
    
        2
  •  1
  •   Sean Wei    7 年前

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