如何让MS bot与Hapi合作

时间:2016-12-24 10:40:51

标签: node.js botframework hapijs

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

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

1 个答案:

答案 0 :(得分:0)

Maxpert从Microsoft BotBuilder框架的github repo给我解决方案。

<!-- 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}`);
});