通过具有 conversationId 的 restify API 访问特定聊天
Access specific chat via restify API having conversationId
我在寻找一种方法来访问 Bot 的特定聊天以从外部处理 API 呼叫时遇到问题,以便与内部消息传递系统集成以便操作员启动对话。
一般来说,我的想法是:如果用户想与人交谈 - 他触发流程(例如 CustomBot.js)并启动通信。但是,为了从不同的系统发送消息 - 我需要通过外部 API 调用访问这个非常特定的用户和聊天,以将消息路由到正确的用户。
所以我从机器人的上下文中获取 conversationId,但我需要句柄来找到一种方法来通过 restift 获取完全相同的上下文 API。
我想这样写:
server.post('/api/route_messages', (req, res) => {
context = adapter.getContextById(req.conversationId)
context.sendActivity(req.message)
})
不幸的是我找不到像"adapter.getContextById"这样的合适方法。
你能建议一种方法吗?
谢谢
如果您想公开一个 API 以从外部服务向特定对话发送消息,您可以使用像 notify/proactive 消息这样的方式来实现。这是official demo for it。但是如果你想发送消息到特定的对话,你应该做一些修改:用下面的代码替换index.js中的内容:
const path = require('path');
const restify = require('restify');
const restifyBodyParser = require('restify-plugins').bodyParser;
// Import required bot services. See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');
// This bot's main dialog.
const { ProactiveBot } = require('./bots/proactiveBot');
// Note: Ensure you have a .env file and include the MicrosoftAppId and MicrosoftAppPassword.
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log
// NOTE: In production environment, you should consider logging this to Azure
// application insights.
console.error(`\n [onTurnError]: ${ error }`);
// Send a message to the user
await context.sendActivity(`Oops. Something went wrong!`);
};
// Create the main dialog.
const conversationReferences = {};
const bot = new ProactiveBot(conversationReferences);
// Create HTTP server.
const server = restify.createServer();
server.use(restifyBodyParser());
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`);
});
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
// route to main dialog.
await bot.run(turnContext);
});
});
// Listen for incoming notifications and send proactive messages to users.
server.post('/api/notify', async (req, res) => {
const conversationId = req.body.conversationId;
const message = req.body.message;
for (const conversationReference of Object.values(conversationReferences)) {
if (conversationReference.conversation.id === conversationId) {
await adapter.continueConversation(conversationReference, async turnContext => {
await turnContext.sendActivity(message);
});
}
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
res.end();
});
运行 演示和 post postman 或 restclient 的消息如下:
如您所见,我打开了两个对话,但只有我指定的对话收到了消息:
这只是一个示例演示,您可以根据您的获胜要求修改请求和逻辑,例如将 url 更改为 /api/route_messages
。
当然,如果有帮助,请标记我:)
我在寻找一种方法来访问 Bot 的特定聊天以从外部处理 API 呼叫时遇到问题,以便与内部消息传递系统集成以便操作员启动对话。
一般来说,我的想法是:如果用户想与人交谈 - 他触发流程(例如 CustomBot.js)并启动通信。但是,为了从不同的系统发送消息 - 我需要通过外部 API 调用访问这个非常特定的用户和聊天,以将消息路由到正确的用户。
所以我从机器人的上下文中获取 conversationId,但我需要句柄来找到一种方法来通过 restift 获取完全相同的上下文 API。
我想这样写:
server.post('/api/route_messages', (req, res) => {
context = adapter.getContextById(req.conversationId)
context.sendActivity(req.message)
})
不幸的是我找不到像"adapter.getContextById"这样的合适方法。
你能建议一种方法吗?
谢谢
如果您想公开一个 API 以从外部服务向特定对话发送消息,您可以使用像 notify/proactive 消息这样的方式来实现。这是official demo for it。但是如果你想发送消息到特定的对话,你应该做一些修改:用下面的代码替换index.js中的内容:
const path = require('path');
const restify = require('restify');
const restifyBodyParser = require('restify-plugins').bodyParser;
// Import required bot services. See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');
// This bot's main dialog.
const { ProactiveBot } = require('./bots/proactiveBot');
// Note: Ensure you have a .env file and include the MicrosoftAppId and MicrosoftAppPassword.
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log
// NOTE: In production environment, you should consider logging this to Azure
// application insights.
console.error(`\n [onTurnError]: ${ error }`);
// Send a message to the user
await context.sendActivity(`Oops. Something went wrong!`);
};
// Create the main dialog.
const conversationReferences = {};
const bot = new ProactiveBot(conversationReferences);
// Create HTTP server.
const server = restify.createServer();
server.use(restifyBodyParser());
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`);
});
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
// route to main dialog.
await bot.run(turnContext);
});
});
// Listen for incoming notifications and send proactive messages to users.
server.post('/api/notify', async (req, res) => {
const conversationId = req.body.conversationId;
const message = req.body.message;
for (const conversationReference of Object.values(conversationReferences)) {
if (conversationReference.conversation.id === conversationId) {
await adapter.continueConversation(conversationReference, async turnContext => {
await turnContext.sendActivity(message);
});
}
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
res.end();
});
运行 演示和 post postman 或 restclient 的消息如下:
如您所见,我打开了两个对话,但只有我指定的对话收到了消息:
这只是一个示例演示,您可以根据您的获胜要求修改请求和逻辑,例如将 url 更改为 /api/route_messages
。
当然,如果有帮助,请标记我:)