0
我正在嘗試構建一個應用程序,該應用程序可以將消息發送到基於Directline lib的Microsoft bot框架上的聊天並回復答案。該應用程序是一個Web應用程序,應該發送一個POST請求,該請求應該將消息轉發給機器人客戶端,並回復來自Microsoft bot框架的機器人響應。如何啓動從nodejs客戶端到微軟機器人的對話
HTTP POST請求的示例:
http://host:port/api/message BODY: {消息: 「您好」}
應該發送「嗨」作爲文本到相關聊天機器人在微軟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)
})
我已經檢查過,沒有設法解決我想要的。讓我試着重新解釋一下。我希望能夠從http客戶端發送發送請求,並在主體中將文本作爲json發送,因此它將通過bot客戶端傳遞消息,並且應該使用bot回覆進行響應。 – user4860092
但使用DirectLine?或REST API與機器人交談?你沒有解釋你的問題 –
我想讓我自己的其他API服務支持一個簡單的對話,所以當你發送一個簡單的標準POST請求時,其中的消息將通過bot客戶端傳遞消息。殭屍客戶端應該使用直接api與Microsoft框架殭屍工具進行交流。這是另一種對話層,我試圖建立一個更簡單的層。 – user4860092