Javascript 通过具有conversationId的restify API访问特定聊天
我遇到了一个问题,那就是如何从外部API调用访问Bot要处理的特定聊天,以便与内部消息传递系统集成,以便运营商参与对话 一般来说,我的想法是:如果用户想与人交谈,他会触发流(例如在CustomBot.js中)并启动通信。然而,为了从不同的系统发送消息,我需要访问这个非常特定的用户&通过外部API调用聊天,将消息路由到正确的用户 所以我从bot的上下文中获取conversationId,但是我需要句柄来找到一种通过restiftapi获取相同上下文的方法 我想写这样的短信:Javascript 通过具有conversationId的restify API访问特定聊天,javascript,botframework,restify,Javascript,Botframework,Restify,我遇到了一个问题,那就是如何从外部API调用访问Bot要处理的特定聊天,以便与内部消息传递系统集成,以便运营商参与对话 一般来说,我的想法是:如果用户想与人交谈,他会触发流(例如在CustomBot.js中)并启动通信。然而,为了从不同的系统发送消息,我需要访问这个非常特定的用户&通过外部API调用聊天,将消息路由到正确的用户 所以我从bot的上下文中获取conversationId,但是我需要句柄来找到一种通过restiftapi获取相同上下文的方法 我想写这样的短信: server.post
server.post('/api/route_messages',(req,res)=>{
context=adapter.getContextById(req.conversationId)
context.sendActivity(请求消息)
})
不幸的是,我找不到像“adapter.getContextById”这样的合适方法
你能给我一个建议吗
谢谢你如果你想公开一个API,用于从外部服务向特定对话发送消息,你可以使用类似notify/proactive message的方法来实现。这就是。但是如果您想向特定对话发送消息,您应该做一些修改:用下面的代码替换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();
});
const path=require('path');
const restify=require('restify');
const restifybodparser=require('restify-plugins')。bodyParser;
//导入所需的bot服务。看见https://aka.ms/bot-services 了解更多有关机器人不同部分的信息。
const{BotFrameworkAdapter}=require('botbuilder');
//此bot的主对话框。
const{ProactiveBot}=require('./bots/ProactiveBot');
//注意:确保您有一个.env文件,并包含MicrosoftAppId和MicrosoftAppPassword。
const ENV_FILE=path.join(_dirname,'.ENV');
require('dotenv').config({path:ENV_FILE});
//创建适配器。
//看https://aka.ms/about-bot-adapter 了解有关适配器的更多信息。
const adapter=新的BotFrameworkAdapter({
appId:process.env.MicrosoftAppId,
appPassword:process.env.MicrosoftAppPassword
});
//抓住所有的错误。
adapter.onTurnError=async(上下文,错误)=>{
//此检查将错误写入控制台日志
/Note:在生产环境中,您应该考虑将此记录到Azure。
//应用见解。
console.error(`\n[onTurnError]:${error}`);
//向用户发送消息
wait context.sendActivity(`Oops.Something出错了!`);
};
//创建主对话框。
const conversationReferences={};
const bot=新的ProactiveBot(会话引用);
//创建HTTP服务器。
const server=restify.createServer();
使用(restfifybodyparser());
侦听(process.env.port | | process.env.port | | | 3978,函数(){
log(`\n${server.name}正在侦听${server.url}`);
console.log(`\nGet Bot Framework仿真器:https://aka.ms/botframework-emulator`);
});
//侦听传入的活动并将它们路由到您的bot主对话框。
server.post('/api/messages',(req,res)=>{
adapter.processActivity(req、res、异步(turnContext)=>{
//路由到主对话框。
等待bot.run(turnContext);
});
});
//侦听传入的通知并向用户发送主动消息。
server.post('/api/notify',async(req,res)=>{
const conversationId=req.body.conversationId;
const message=req.body.message;
for(对象值的常量conversationReference(conversationReferences)){
if(conversationReference.conversation.id==conversationId){
等待适配器。continueConversation(conversationReference,async turnContext=>{
等待turnContext.sendActivity(消息);
});
}
}
res.setHeader('Content-Type','text/html');
书面记录(200);
res.write('已发送主动消息');
res.end();
});
运行演示并通过邮递员或restclient发布消息,如下所示:
如您所见,我打开了两个对话,但只有我指定的对话收到了消息:
这只是一个示例演示,您可以根据自己的需求修改请求和逻辑,例如将url更改为/api/route\u messages
当然,如果有帮助,请标记我:)如果您想公开一个用于从外部服务向特定会话发送消息的API,您可以使用类似notify/proactive message的方法来完成。这就是。但是如果您想向特定对话发送消息,您应该做一些修改:用下面的代码替换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();
});
const path=require('path');
const restify=require('restify');
const restifybodparser=require('restify-plugins')。bodyParser;
//导入所需的bot服务。看见https://aka.ms/bot-services 了解更多有关机器人不同部分的信息。
const{BotFrameworkAdapter}=require('botbuilder');
//此bot的主对话框。
const{ProactiveBot}=require('./bots/ProactiveBot');
//注意:确保您有一个.env文件,并包含MicrosoftAppId和MicrosoftAppPassword。
const ENV_FILE=path.join(_dirname,'.ENV');
require('dotenv').config({path:ENV_FILE});
//创建适配器。
//看https://aka.ms/about-bot-adapter 了解有关适配器的更多信息。
const adapter=新的BotFrameworkAdapter({
appId:process.env.MicrosoftAppId,
appPassword:process.env.MicrosoftAppPassword
});
//抓住所有的错误。
adapter.onTurnError=async(上下文,错误)=>{
//此检查将错误写入控制台日志
/Note:在生产环境中,您应该考虑将此记录到Azure。
//应用见解。
console.error(`\n[onTurnError]:${error}`);
//向用户发送消息
wait context.sendActivity(`Oops.Something出错了!`);
};
//创建主对话框。
const conversationReferences={};
康斯特机器人