2

我需要使用 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”方法。

我怎样才能以固定的时间间隔发送一条消息,而忽略所有消息/提及。

4

1 回答 1

2

您要做的就是所谓的主动消息传递。您可以查看此文档及其引用的示例,以更好地了解如何执行此操作。

如果您希望您的主动消息由计时器触发,那么您可以在机器人的线程上运行计时器,但通常建议让计时器在外部运行。

要为您的机器人禁用消息传递,只需在您的频道配置中选择该选项。不过,如果您禁用消息传递,我不确定您将如何检索对话 ID。

在此处输入图像描述

如果您仍然希望您的机器人接收消息但不想回复它们,只需编辑您的机器人代码中响应条件的部分turnContext.activity.type === 'message'

请记住,Skype 机器人功能可能会变得越来越有限。你应该会在 Skype 频道配置中看到一条官方消息,上面写着:

自 2019 年 10 月 31 日起,Skype 频道将不再接受新的 Bot 注册。当前的 Skype 机器人将继续不间断地运行。

于 2019-10-01T17:43:58.733 回答