node.js - 松弛如何知道机器人最近是否发布
问题描述
我正在使用 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
}
});
解决方案
我可以看到您的问题的两种解决方案:
在某种应用程序上下文(例如数据库)中记录您的机器人以前的操作。然后,您可以每次验证您的机器人是否已经回答。
考虑使用事件 API而不是每次都加载聊天记录。然后,您的机器人会针对频道中的每条新消息获得一个事件请求,并且您可以确定您的机器人只会做出一次反应。
推荐阅读
- javascript - 如何从 jquery 中的多个 li 中确定文本的长度?
- jquery - 切换 jQuery div 以打开和关闭无法正常工作
- javascript - 如何在反应 d3 树中自定义方向
- python - 什么是 yolo_best.weights 文件?它与 yolo_custom.weights 有何不同
- memory - 使用 webgl 在 chrome 上的 GPU 中的纹理成本是多少?
- java - bitnami 解析服务器升级问题
- php - 如何使用 PHP 抓取 HTML 表格数据
- java - 如何创建自定义 FX 组件以动态呈现其子级?
- javascript - 声明类属性时出现“lines-between-class-members”ESLint 错误
- mysql - 如何从数据库中选择所有表?使用 node.js