Skip to content
Snippets Groups Projects
Commit f4bbb865 authored by ryadav88's avatar ryadav88
Browse files

index

parent 8699f0e8
No related branches found
No related tags found
No related merge requests found
...@@ -4,32 +4,36 @@ var url = require('url'); ...@@ -4,32 +4,36 @@ var url = require('url');
var request = require('request'); var request = require('request');
var format = ".json"; var format = ".json";
var apikey = process.env.WU_ACCESS var apikey = process.env.WU_ACCESS //WU API key; will be set in Heroku
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
//use port is set in the environment variable, or 9001 if it isn’t set.
app.set('port', (process.env.PORT || 9001)); app.set('port', (process.env.PORT || 9001));
//for testing that the app is running
app.get('/', function(req, res){ app.get('/', function(req, res){
res.send('It works!'); res.send('Running!!');
}); });
//app.post is triggered when a POST request is sent to the URL ‘/post’
app.post('/post', function(req, res){ app.post('/post', function(req, res){
//take a message from Slack slash command
var query = req.body.text var query = req.body.text
var parsed_url = url.format({ var parsed_url = url.format({
pathname: 'http://api.wunderground.com/api/' + apikey + '/conditions/q/' + req.body.text + format, pathname: 'http://api.wunderground.com/api/' + apikey + '/conditions/q/' + req.body.text + format,
}); });
console.log(parsed_url); console.log(parsed_url);
request(parsed_url, function (error, response, body) { request(parsed_url, function (error, response, body) {
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
var data = JSON.parse(body); var data = JSON.parse(body);
var first_url = data.current_observation.temperature_string; var temperature = data.current_observation.temperature_string;
var weatherC = data.current_observation.weather var weatherCondition = data.current_observation.weather
var icon_url = data.current_observation.icon_url var icon_url = data.current_observation.icon_url
var location = data.current_observation.display_location.full var location = data.current_observation.display_location.full
...@@ -37,17 +41,19 @@ app.post('/post', function(req, res){ ...@@ -37,17 +41,19 @@ app.post('/post', function(req, res){
response_type: "in_channel", response_type: "in_channel",
"attachments": [ "attachments": [
{ {
"text": "Location: " + location + "\n" + "Temperature: " + first_url + "\n" + "Condition: " + weatherC, "text": "Location: " + location + "\n"
"image_url": icon_url, + "Temperature: " + temperature + "\n"
+ "Condition: " + weatherCondition,
"image_url": icon_url,
} }
] ]
}; };
res.send(body); res.send(body);
} }
}); });
}); });
//tells Node which port to listen on
app.listen(app.get('port'), function() { app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port')); console.log('Node app is running on port', app.get('port'));
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment