javascript - Microsoft Bot Framework 无需用户与机器人对话即可发送消息

标签 javascript botframework bots

我需要创建一个机器人,使用 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 documentthe sample it references更好地理解如何做到这一点。

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

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

enter image description here

如果您仍然希望机器人接收消息但只是不想回复它们,只需编辑机器人代码中响应条件的部分 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/

相关文章:

javascript - 在 JSON 数组中搜索字符串并检索包含该字符串作为值的对象

javascript - 元素目的地

java - 无法通过 Skype 机器人创建新对话

javascript - Discord JS//尝试通过对消息使用react来添加角色

javascript - 更改 HTML <h1> 文本不起作用?我正确链接了 app.js 吗?

javascript - 如何重新加载 chrome 扩展程序中的某些选项卡

javascript - 我尝试了通过 Node.js 连接到 LUIS 的机器人构建器的示例。我无法连接到 LUIS

c# - 使用 MS BotFramework 中的 PromptDialog 开始对话

python - 如何在 Python 上使用 Tweepy 创建 Twitter 线程

python - 我如何在 Python 中为 discord 机器人创建重新加载命令?