Created
May 18, 2016 09:58
-
-
Save eyal-he/a97e0bc70687dd0c579fab63b5727e91 to your computer and use it in GitHub Desktop.
Building a Bot for Facebook Messenger using IBM Bluemix: Weather JS code
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 express = require('express'); | |
var request = require('request'); | |
var bodyParser = require('body-parser'); | |
var geocoderProvider = 'google'; | |
var httpAdapter = 'http'; | |
var geocoder = require('node-geocoder')(geocoderProvider, httpAdapter); | |
var watson = require('watson-developer-cloud'); | |
var cfenv = require('cfenv'); | |
var app = express(); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
// var appEnv = cfenv.getAppEnv(); | |
// var envVars = process.env; | |
var classifier = <CLASSIFIER_ID> //pre-trained classifier that distinguishes between a weather conditions question and temperature question | |
var vcap = JSON.parse(process.env.VCAP_SERVICES); | |
var nlClassifier = watson.natural_language_classifier({ | |
url : vcap.natural_language_classifier[0].credentials.url, | |
username : vcap.natural_language_classifier[0].credentials.username, | |
password : vcap.natural_language_classifier[0].credentials.password, | |
version : 'v1' | |
}); | |
var weatherUserName = vcap.weatherinsights[0].credentials.username; | |
var weatherPassword = vcap.weatherinsights[0].credentials.password; | |
var alchemyApiKey = vcap.alchemy_api[0].credentials.apikey; | |
app.get('/getWeather', function (req,responseToRequest) { | |
var extractedLocation; | |
text = req.query.text; | |
extractedLocation = text; | |
// Extracting the location from the input text using Watson AlChemyAPI service // | |
request("https://gateway-a.watsonplatform.net/calls/text/TextGetRankedNamedEntities?apikey=" + alchemyApiKey + "&text=" + text + "&outputMode=json", function(error, response, body) { | |
var entities = JSON.parse(body).entities; | |
for (i = 0; i < entities.length; i++) { | |
if(entities[i].type === "Country" || entities[i].type === "City" || entities[i].type === "County" || entities[i].type === "State" || entities[i].type === "StateOrCounty"){ | |
extractedLocation = entities[i].text; | |
} | |
} | |
// As a backup, you can also extract the location using a specialized engine such as node-geocoder // | |
geocoder.geocode(extractedLocation).then(function(res) { | |
if(res[0]){ | |
// Requesting weather data from Bluemix Insights for Weather Service // | |
request("https://" + weatherUserName + ":" + weatherPassword + "@twcservice.mybluemix.net/api/weather/v2/observations/current?units=m&geocode=" + res[0].latitude + "%2C" + res[0].longitude + "&language=en-US", function(error, response, body) { | |
var json = JSON.parse(response.body); | |
var params = { | |
classifier: classifier, // identifying type of question using the pre-trained classifier | |
text: text | |
}; | |
nlClassifier.classify(params, function(error,results){ | |
var message = "Could not identify if question is about weather conditions or temperature"; | |
var location = ""; | |
if (res[0].city) { | |
location += res[0].city + ", "; | |
} | |
if (res[0].country) { | |
location += res[0].country; | |
} | |
for (i = 0; i < results.classes.length; i++) { | |
if (results.classes[i].confidence > 0.8) { | |
if (results.classes[i].class_name === "temperature") { | |
message = "Temperature in " + location + " is " + json.observation.metric.temp_min_24hour + "--" + json.observation.metric.temp_max_24hour; | |
} else if (results.classes[i].class_name === "conditions") { | |
message = "Weather in " + location + " is " + json.observation.sky_cover; // Returned message determined according to the classifier's result | |
} | |
} | |
} | |
responseToRequest.send(message); | |
}); | |
}); | |
} else { | |
responseToRequest.send("Location not identified"); | |
} | |
}).catch(function(err){responseToRequest.send("Location not identified");}); | |
}); | |
}); | |
var host = (process.env.VCAP_APP_HOST || 'localhost'); | |
var port = (process.env.VCAP_APP_PORT || 3000); | |
app.listen(port,host); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I push the weather app to bluemix, it said don't know 0 of " vcap.natural_language_classifier[0].credentials.url".so I hard code it.
The value of entities get from JSON.parse(body).entities is zero.I don't know why??