Reputation: 1070
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. 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.
An example of HTTP POST request:
http://host:port/api/message BODY: {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.
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)
})
Upvotes: 0
Views: 627
Reputation: 14787
It's unclear the issue that you are having but I recommend you to check the Node.js Direct Line sample.
Upvotes: 2