gpt4 book ai didi

javascript - 通过具有对话 ID 的 Restify API 访问特定聊天

转载 作者:行者123 更新时间:2023-12-01 00:36:14 25 4
gpt4 key购买 nike

我在寻找一种方法来访问机器人的特定聊天以从外部 API 调用进行处理,以便与内部消息系统集成以便运算符(operator)启动对话时遇到问题。

一般来说,我的想法是:如果用户想与人交谈 - 他会触发流程(例如在 CustomBot.js 中)并启动通信。但是,为了从不同的系统发送消息 - 我需要通过外部 API 调用访问这个非常特定的用户并聊天,以将消息路由到正确的用户。

因此,我从机器人的上下文中获取对话 ID,但我需要句柄来找到一种通过 Restift API 获取完全相同的上下文的方法。

我想这样写:

server.post('/api/route_messages', (req, res) => {
context = adapter.getContextById(req.conversationId)
context.sendActivity(req.message)
})

不幸的是,我找不到像“adapter.getContextById”这样的正确方法。

您能建议一种方法吗?

谢谢

最佳答案

如果您想公开一个 API,用于从外部服务向特定 session 发送消息,您可以使用类似通知/主动消息的方式来实现。这是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();
});

运行演示并通过 postman 或restclient发布消息,如下所示: enter image description here

如您所见,我打开了两个对话,但只有我指定的对话收到了消息: enter image description here

这只是一个示例演示,您可以根据您的要求修改请求和逻辑,例如将 url 更改为 /api/route_messages

当然,如果有帮助请标记我:)

关于javascript - 通过具有对话 ID 的 Restify API 访问特定聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58112550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com