[英]How to start a conversation from nodejs client to microsoft bot
I'm trying to build an app that would send messages to a chat on Microsoft bot framework based on Directline lib, and reply with answer. 我正在尝试构建一个应用程序,该应用程序可以将消息发送到基于Directline lib的Microsoft bot框架上的聊天室,然后进行答复。 The app is a web application where should send a POST request that should forward the message to the bot client, and reply with response of the bot from Microsoft bot framework.
该应用程序是一个Web应用程序,应在其中发送POST请求,该请求应将消息转发到bot客户端,并以来自Microsoft bot框架的bot响应进行回复。
An example of HTTP POST request: HTTP POST请求的示例:
http://host:port/api/message BODY: {message:"Hi"} http:// host:port / api / message正文 : {message:“ Hi”}
It should send the "Hi" as a text to the relevant chat bot in the Microsoft Bot framework and reply with what ever the framework replys with. 它应将“ Hi”作为文本发送到Microsoft Bot框架中的相关聊天机器人,并以该框架答复的内容进行答复。
I have put the secret and done all i think i should have done in order it to be working but, i'm having problem generating a conversation that should talk with the chat bot. 我已经提出了秘密,并做了我认为应该做的所有事情,以便使其正常工作,但是,在生成应与聊天机器人进行对话的对话时遇到了问题。
This is how i did it: 这是我的方法:
"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)
})
目前尚不清楚您遇到的问题,但我建议您检查一下Node.js Direct Line示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.