【问题标题】:Telegram Bot Private bot [security]Telegram Bot 私人机器人 [安全]
【发布时间】:2018-08-11 05:04:00
【问题描述】:

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

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

未处理的拒绝错误: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.');                
    });
});

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

【问题讨论】:

    标签: node.js telegram-bot


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      我所做的是将我希望我的机器人工作的每个用户或组的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
      

      【讨论】:

        猜你喜欢
        • 2018-05-17
        • 2021-06-20
        • 1970-01-01
        • 2021-08-08
        • 2020-04-04
        • 1970-01-01
        • 2018-10-05
        • 2022-10-18
        • 2018-12-03
        相关资源
        最近更新 更多