Skip to content

Instantly share code, notes, and snippets.

@gbaeke
Last active October 5, 2016 14:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gbaeke/8afbe51be41f029390e690d823b4c4b0 to your computer and use it in GitHub Desktop.
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