Skip to content

Instantly share code, notes, and snippets.

@alexandrenicol
Created February 2, 2017 12:12
Facebook Messenger Bot - Final
const Facebook = require('Facebook');
const Apiai = require('Apiai');
const WeatherStation = require('WeatherStation');
exports.handler = (event, context, callback) => {
/*...
get info from messenger https://gist.github.com/alexandrenicol/f01f867cb175cd89e3e100c508cc6f4a#file-index-js
const message, senderID
...*/
Apiai.nlu(message, senderID)
.then(function(apiaiData){
if (apiaiData.result.source === 'domains') {
Facebook.sendTextMessage(senderID, apiaiData.result.fulfillment.speech);
} else if (apiaiData.result.source === 'agent') {
const action = apiaiData.result.action;
const result = apiaiData.result;
switch(action){
case 'needwhere':
Facebook.sendQuickReply(senderID, result.fulfillment.speech, result.fulfillment.messages[1].replies);
break;
case 'humidity':
WeatherStation.getData()
.then(function(weatherStationData){
let value = null;
if (WeatherStation.getLocation(result) === 'indoor') {
value = weatherStationData.body.devices[0].dashboard_data.Humidity;
} else {
value = weatherStationData.body.devices[0].modules[0].dashboard_data.Humidity;
};
Facebook.sendTextMessage(senderID, `The Humidity is ${value}% `);
}).catch(function(err){
Facebook.sendTextMessage(senderID, `Hum, something is wrong with Netatmo :/ `);
});
break;
case 'temperature':
WeatherStation.getData()
.then(function(weatherStationData){
let value = null;
if (WeatherStation.getLocation(result) === 'indoor') {
value = weatherStationData.body.devices[0].dashboard_data.Temperature;
} else {
value = weatherStationData.body.devices[0].modules[0].dashboard_data.Temperature;
};
Facebook.sendTextMessage(senderID, `The temperature is ${value}ºC `);
}).catch(function(err){
Facebook.sendTextMessage(senderID, `Hum, something is wrong with Netatmo :/ `);
});
break;
default:
Facebook.sendTextMessage(senderID, `Oops, something happened...`);
break;
}
} else {
Facebook.sendTextMessage(senderID, `Oops, something happened...`);
}
})
.catch(function(err){
Facebook.sendTextMessage(senderID, `Oops, something happened...`);
});
/* ...
some code https://gist.github.com/alexandrenicol/f01f867cb175cd89e3e100c508cc6f4a#file-index-js
... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment