2017-09-12 4 views
0

DirectLive 라이브러리를 기반으로 Microsoft bot 프레임 워크의 채팅에 메시지를 보내고 응답으로 응답하는 앱을 작성하려고합니다. 이 응용 프로그램은 메시지를 봇 클라이언트에 전달해야하는 POST 요청을 보내야하는 웹 응용 프로그램이며 Microsoft bot 프레임 워크에서 봇의 응답으로 응답합니다.nodejs 클라이언트에서 microsoft로 대화를 시작하는 방법

HTTP POST 요청의 예 :

http://host:port/api/message BODY : {메시지 : "안녕"}

그것은 전송해야 "안녕" 관련 채팅 봇에 텍스트 등, 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) 

}) 

답변

2

그것은 불분명는하지만 난 당신이 Node.js Direct Line sample을 확인하는 것이 좋습니다 발생하는 문제.

+0

나는 이미 그것을 확인하고 내가 원하는 것을 해결하지 못했습니다. 내가 설명하려고하자. http 클라이언트에서 json으로 본문에 텍스트가있는 게시물 요청을 보낼 수 있기를 원하므로 봇 클라이언트를 통해 메시지를 전달하고 봇 응답으로 응답해야합니다. – user4860092

+0

그러나 DirectLine을 사용하고 계십니까? 또는 REST API를 사용하여 봇과 대화 할 수 있습니까? 당신은 당신의 이슈를 설명하지 못합니다. –

+0

간단한 대화를 지원하기 위해 노출 된 자체 휴식 API 서비스를 원합니다. 따라서 메시지가 포함 된 간단한 표준 POST 요청을 보내면 봇 클라이언트를 통해 메시지가 전달됩니다. 봇 클라이언트는 직통 API를 통해 Microsoft 프레임 워크 봇과 이야기해야합니다. 그것은 대화의 또 다른 층입니다. 제가 만들려고하는 더 단순한 층입니다. – user4860092