松弛如何知道机器人是否最近发布

slack how to know if bot recently posted

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

目前我正在使用 channels.history 方法检索 4 条最近的消息,然后找到机器人 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

                  }
              });

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

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

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