首页 > 解决方案 > 松弛如何知道机器人最近是否发布

问题描述

我正在使用 botkit,我有一个响应某个单词的机器人。但我不希望机器人在最近这样做时做出响应。

目前我正在使用channels.history方法来检索 4 条最近的消息,然后找到 bot id,如果它在那里它不会回复。这不漂亮,我一直在寻找有用的方法来使用,但我找不到任何方法。我只想了解该机器人是否最近发布并根据它执行操作。

   const targetBotID = 'GKALXJCM6'  
   bot.api.channels.history({
            channel: message.channel,
                latest: message.ts,
                count: 4,
                inclusive: 1,
            },  function(err, response) {
                  if(err) { bot.reply(message, 'Something is wrong with me, check log if there is??'); }

                  if(response){
                      const recentPostFound = response.messages.filter(function (member) {
                                                 return member.user === targetBotID;
                                              });

                      if(recentPostFound){
                        return bot.reply();
                      }
                        return bot.reply(answer) // Answer if no matching id found

                  }
              });

标签: node.jsslack-apibotkit

解决方案


我可以看到您的问题的两种解决方案:

  1. 在某种应用程序上下文(例如数据库)中记录您的机器人以前的操作。然后,您可以每次验证您的机器人是否已经回答。

  2. 考虑使用事件 API而不是每次都加载聊天记录。然后,您的机器人会针对频道中的每条新消息获得一个事件请求,并且您可以确定您的机器人只会做出一次反应。


推荐阅读