Skip to content

Instantly share code, notes, and snippets.

@gbaeke gbaeke/server.js
Last active Oct 5, 2016

Embed
What would you like to do?
var builder = require('botbuilder');
var airq = require('./airq');
var restify = require('restify');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
var intents = new builder.IntentDialog();
server.post('/api/messages', connector.listen());
// Serve a static web page
server.get(/.*/, restify.serveStatic({
'directory': '.',
'default': 'index.html'
}));
// idea is that you set the room and then ask questions about that room
bot.dialog('/', intents);
intents.matches(/^change room/i, [
function (session) {
session.send("Ok, let's change the room name...");
session.beginDialog('/roomName');
},
function(session, results) {
session.send('Changed room to %s', session.userData.roomName);
}
]);
intents.matches(/^air quality/i, [
function(session) {
airqData=airq.getRoom(session.userData.roomName);
if(airqData) {
CO2 = airqData.avgCO2;
Temp = airqData.avgTemp;
Hum = airqData.avgHum;
session.endDialog("In room " + session.userData.roomName + " it is " + Temp + " degrees with a humidity of " + Hum + " percent. Amount of CO2 is " + CO2 + ".");
} else session.endDialog("Room does not exist or no data!");
}
]);
intents.matches(/^commands/i, [
function(session) {
session.send('You can use the following commands:');
session.send('change room');
session.send('air quality');
}
]);
intents.onDefault([
function(session, args, next) {
if(!session.userData.roomName) {
session.beginDialog('/roomName');
} else {
next();
}
},
function(session, results) {
session.send('Room set to %s. Change it with command: change room', session.userData.roomName);
}
]);
bot.dialog('/roomName', [
function(session) {
// we are here bacause name of room not saved in userData
builder.Prompts.text(session, "What's the room name?");
},
function(session, results) {
session.userData.roomName = results.response;
session.endDialog();
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.