botanalytics-botframework-middleware
Version:
Microsoft BotBuilder Middleware for Botanalytics
43 lines (40 loc) • 1.42 kB
JavaScript
var request = require('request')
module.exports = function(credentials) {
if (!credentials || !credentials.token) {
throw new Error('No token specified');
}
BotanalyticsMiddleware = {};
BotanalyticsMiddleware.receive = function(session, next) {
console.log(session);
sendToBotanalytics(session,false,next);
}
BotanalyticsMiddleware.send = function(session, next) {
console.log(session);
sendToBotanalytics(session,true,next);
}
function sendToBotanalytics(session,is_sender_bot,next) {
request({
url:'http://127.0.0.1:8000/api/v1/messages/microsoft-bot-framework/', //'https://botanalytics.co/api/v1/messages/microsoft-bot-framework',
body: JSON.stringify({message: session,
timestamp:new Date().getTime(),
is_sender_bot:is_sender_bot}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization':'Token '+ credentials.token
}
}, function(error, response, body){
if(error) {
console.log(error);
next(error);
} else if (response.statusCode != 200 || response.statusCode != 201) {
console.log(response);
next(new Error("Unexpected Status Code from Botanalytics API"));
}else{
console.log(body);
next();
}
});
}
return BotanalyticsMiddleware;
}