-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (42 loc) · 1.41 KB
/
Copy pathindex.js
File metadata and controls
45 lines (42 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const
bodyParser = require('body-parser'),
config = require('config'),
express = require('express'),
request = require('request');
var app = express();
var port = process.env.PORT || process.env.port || 5000;
app.set('port',port);
//use body parsing middleware
app.use(bodyParser.json());
app.listen(app.get('port'),function(){
console.log('[app.listen]Node app is running on port',app.get('port'));
});
module.exports = app;
const WEATHER_APP_ID = config.get('weather_app_id');
//主程式運作邏輯
app.post('/webhook', function(req, res){
let data = req.body;
let queryDate = data.queryResult.parameters.date;
let queryCity = data.queryResult.parameters["geo-city"];
//Go to OpenWeatherMap to get weather data
let propertiesObject = {
q:queryCity,
APPID:WEATHER_APP_ID,
units:"metric"
};
request({
uri:"https://api.openweathermap.org/data/2.5/weather",
json:true,
qs:propertiesObject
},function(error, response, body){
if(!error && response.statusCode ==200){
//Send back the weather result
res.json({fulfillmentText:"The weather in "+ queryCity +
" is " + body.weather[0].description +
". And the temperature is around " + body.main.temp});
}else{
console.log("[OpenWeatherMap] failed");
}
});
});