我需要创建一个机器人,使用 Javascript 每十分钟发送一条消息。我使用的是Microsoft Bot Framework,这是入口代码:
const restify = require('restify');
const botbuilder = require('botbuilder');
var adapter = new botbuilder.BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
let server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log(`\n${server.name} listening to ${server.url}`);
console.log(`\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator`);
});
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
if (turnContext.activity.type === 'message') {
const text = turnContext.activity.text;
await turnContext.sendActivity(`You just said: ${ text }`);
}
});
});
基本上,无论与机器人交谈的人说什么,都会以“您刚刚说:x”来响应。
我需要的是机器人在 Skype 中分组并每十分钟发送一条消息。
但是,在我的示例中,服务器等待到/api/messages 的 POST,然后使用适配器处理该请求并从来自 processActivity 方法的turnContext 激发“sendActivity”方法。
我如何才能以固定的时间间隔发送消息,并忽略所有消息/提及。
最佳答案
您想要做的事情称为主动消息传送。您可以看看this document和 the sample it references更好地理解如何做到这一点。
如果您希望计时器触发主动消息,那么您可以在机器人的线程上运行计时器,但通常建议在外部运行计时器。
要禁用机器人的消息传递,只需在 channel configuration 中选择该选项即可。 。不过,我不确定如果您禁用消息传递,您将如何检索对话 ID。
如果您仍然希望机器人接收消息但只是不想回复它们,只需编辑机器人代码中响应条件的部分 turnContext.activity.type === 'message'
.
请记住,Skype 机器人功能可能会变得越来越有限。您应该会在 Skype channel 配置中看到一条官方消息:
As of October 31, 2019 the Skype channel will no longer be accepting new Bot registrations. Current Skype bots will continue to run uninterrupted.
关于javascript - Microsoft Bot Framework 无需用户与机器人对话即可发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58186268/