Created
February 20, 2017 21:16
-
-
Save hartct/53299004a0c432f4f4f988065e120297 to your computer and use it in GitHub Desktop.
Bot server implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var restify = require('restify'); | |
var builder = require('botbuilder'); | |
var env = require('node-env-file'); | |
var client = require("socket.io-client"); | |
function findRooms(io) { | |
var availableRooms = []; | |
var rooms = io.sockets.adapter.rooms; | |
if (rooms) { | |
for (var room in rooms) { | |
if (!rooms[room].hasOwnProperty(room)) { | |
availableRooms.push(room); | |
} | |
} | |
} | |
return availableRooms; | |
} | |
//========================================================= | |
// Bot Setup | |
//========================================================= | |
module.exports = function(io, http) { | |
// Setup Restify Server | |
var server = restify.createServer(); | |
server.listen(process.env.port || process.env.PORT || 3978, function () { | |
console.log('%s listening to %s for bot requests', server.name, server.url); | |
}); | |
// Create chat bot | |
var connector = new builder.ChatConnector({ | |
appId: process.env.appId, | |
appPassword: process.env.appPassword | |
}); | |
var bot = new builder.UniversalBot(connector); | |
server.post('/api/messages', connector.listen()); | |
//========================================================= | |
// Bots Dialogs | |
//========================================================= | |
bot.dialog('/', new builder.SimpleDialog(function (session, results) { | |
if (findRooms(io).indexOf(session.message.address.conversation.id) > -1) { | |
console.log("found existing room for conversation"); | |
// create a socket connection for this dialog` | |
var socket = client.connect("http://localhost:3000/"); | |
// join a channel that scopes the discussion between the bot and the | |
// agent using the session conversation ID | |
socket.emit("join channel", session.message.address.conversation.id); | |
// once the agent joins, send the message we received from the | |
// customer via the bot | |
socket.emit("customer message", session.message.text); | |
// TODO: AI/ML processing goes here | |
} else { | |
var socket = client.connect("http://localhost:3000/"); | |
socket.emit("chat message", "A new customer/bot discussion has been initiated. " + | |
"<a href='/join_chat.html?id=" + session.message.address.conversation.id + | |
"'>Click here to join.</a>"); | |
// join a channel that scopes the discussion between the bot and the | |
// agent using the session conversation ID | |
socket.emit("join channel", session.message.address.conversation.id); | |
// setup a callback that will be invoked once an agent joins | |
// the discussion | |
var agentJoinedCallback = function() { | |
session.channelInitiated = true; | |
// once the agent joins, send the message we received from the | |
// customer via the bot | |
socket.emit("customer message", session.message.text); | |
// TODO: AI/ML processing goes here | |
// setup a callback to be invoked once the agent responds so that | |
// we can reply to the customer using the text | |
var respondCallback = function(msg) { | |
console.log("botChatStart dialog: call back invoked with msg:" + msg); | |
session.send(msg); | |
}; | |
// setup an event handler so we respond to the customer when | |
// the agent responds | |
socket.on("agent message", function(msg) { | |
console.log("botChatStart dialog: got channel message: " + msg); | |
// here we invoke the response callback | |
respondCallback(msg); | |
}); | |
}; | |
// setup an event handler so we know once the agent joins | |
socket.on("agent joined", function(msg) { | |
// invoke our callback from above | |
agentJoinedCallback(); | |
}); | |
} | |
})); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment