gpt4 book ai didi

node.js - 如何让 MS bot 与 Hapi 一起工作

转载 作者:太空宇宙 更新时间:2023-11-04 00:29:59 26 4
gpt4 key购买 nike

我当前的实现不会返回任何有用的内容,例如错误消息,所以我有点卡住了。

method: "POST",
path: '/api/messages',
handler: status

我的状态函数是一个简单的函数:

const status = function (request, reply){  
return connector.listen(request.raw.req, request.raw.res);
};

此外,使用模拟器时,消息似乎只是发送并超时,没有日志信息。

Full code can be seen here

最佳答案

Maxpert 从 Microsoft BotBuilder 框架的 github 存储库中为我提供了该解决方案。

<!-- language: lang-js -->
'use strict';

const Hapi = require('hapi');
const botbuilder = require('botbuilder');

const server = new Hapi.Server();
server.connection({ port: 3000 });

/**
* interface IWebResponse {
* end(): this;
* send(status: number, body?: any): this;
* send(body: any): this;
* status(code: number): this;
* }
*/
function responseWrapper(res) {
return {
end() {
res.end();
return this;
},

send(statusOrBody, maybeBody, callback) {
var responseCode = 200;
var responseBody = maybeBody;

if (typeof statusOrBody == 'number') {
responseCode = statusOrBody;
responseBody = maybeBody;
} else {
responseBody = statusOrBody;
}

if (typeof responseBody != 'string') {
responseBody = JSON.stringify(responseBody);
}

res.writeHead(responseCode);
res.setHeader("Content-Type", "application/json");
res.write(responseBody, callback);
res.end();
return this;
},

status(code) {
res.writeHead(code);
return this;
}
}
}

/**
*
* interface IWebRequest {
* body: any;
* headers: {
* [name: string]: string;
* };
* on(event: string, ...args: any[]): void;
* }
*
*/
function requestWrapper(request) {
return {
body: request.payload,
headers: request.headers,
on(event, listener) {
request.raw.req.on(event, listener)
return this;
}
}
}

// Initialize your connector
const connector = new botbuilder.ChatConnector({
appId: 'YOUR APP ID',
appPassword: 'YOUR APP PASSWORD'
});

const bot = new botbuilder.UniversalBot(connector);
const botHandler = connector.listen();
//=========================================================
// Bots Dialogs
//=========================================================

bot.dialog('/', function (session) {
session.send("Hello World");
});

server.route({
method: 'POST',
path: '/api/messages',
handler: function (request, reply) {
var wrappedResponse = responseWrapper(request.raw.res);
var wrappedRequest = requestWrapper(request);
botHandler(wrappedRequest, wrappedResponse); // Forwarded!!!
}
});

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
var req = request.raw.req,
res = request.raw.res;
reply('Hello, world!');
}
});

server.start((err) => {
if (err) {
throw err;
}

console.log(`Server running at: ${server.info.uri}`);
});

关于node.js - 如何让 MS bot 与 Hapi 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312528/

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