如何从nodejs客户端启动对话到microsoft bot

时间:2017-09-12 18:33:32

标签: node.js azure bots botframework chatbot

我正在尝试构建一个应用程序,该应用程序将消息发送到基于Directline lib的Microsoft bot框架上的聊天,并回复答案。该应用程序是一个Web应用程序,应该发送一个POST请求,该请求应该将消息转发给bot客户端,并回复来自Microsoft bot框架的bot的响应。

HTTP POST请求的示例:

http://host:port/api/message BODY: {message:“嗨”}

它应该将“Hi”作为文本发送到Microsoft Bot框架中的相关聊天机器人,并回复框架回复的内容。

我已经把秘密完成了,我认为我应该做的就是为了工作但是,我在生成一个应该与聊天机器人交谈的对话时遇到问题。

我就这样做了:

"use strict";

require('dotenv').config();

var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require("body-parser");
var http = require('http');
var postLib = require("./lib");

var messageFilePath="message.out";

var cors = require('cors');
var uuid = require('uuid');

//new botclient
var client = require('directline-api');

// config items

var pollInterval = 1000;
var directLineSecret = 'secret';
var directLineClientName = 'DirectLineClient';
var directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';

///bot client end


var sendmail = require('sendmail')({silent: true})

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.options('*', cors()); // include before other routes
var corsOptions = {
  origin: '*',
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
};

    app.post('/interact/message', cors(corsOptions), function(req, res) {
                var uuid1 = uuid.v1();
                var bodyMessage = JSON.stringify(req.body);
                var log = uuid1 + ', ' + new Date().getTime() + ", " + bodyMessage;
                if (req.query.botId == 1) {
                        emailMessage(log);
                        res.send(postLib.reply.reply);
                }
                if (req.query.botId == 2) {
                        botMessage(bodyMessage.message);
                        res.send(postLib.reply.reply);
                }

});




function emailMessage(log){

                sendmail({
                        from: postLib.mail.from,
                        to: postLib.mail.to,
                        subject: postLib.mail.subject,
                        html: 'this is the log: [' + log + ']',
                        }, function(err, reply) {
                        console.log(err && err.stack);
                        console.dir(reply);
                });


                fs.appendFile(messageFilePath, "\n" + log, function(error){
                        if (error) throw error;
                });
}


function botMessage(message){

var token = client.getToken(directLineSecret);

// create a conversation
var conversationId = client.createConversation(token);

// post a message in a conversation
client.postMessage(token, conversationId, {
                text: message
            });
return client.getMessage(token, conversationId, 0);
}

var server = app.listen(8082, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("interact post server listening at http://%s:%s", host, port)

})

1 个答案:

答案 0 :(得分:2)

目前还不清楚您的问题,但我建议您查看Node.js Direct Line sample