-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (59 loc) · 1.98 KB
/
index.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var request = require('request');
var http = require('http');
var bodyparser = require('body-parser');
const {parse} = require('querystring');
//create a server object:
const app = http.createServer(function (req, res) {
if (req.method == 'POST') {
var body = ''
req.on('data', function(data) {
body += data;
console.log("body :" , body);
var parsedData = JSON.parse(body);
// req.body.queryResult.parameters['location'];//agent.parameters.location.city;
var loc = parsedData.queryResult.parameters.location.city;
var apiUrl = "http://api.openweathermap.org/data/2.5/weather?q="+loc+"&appid=07f145d8a2550bdf4d2bb0de56a95506&units=metric";
request(apiUrl, function(error,request,body){
if(error){
let responobj = {
"fulfillmentText": "Try with some another place",
"fulfillmentmessages": "",
"source" : " ",
}
res.end(JSON.stringify(responobj));
return;
}
var response = JSON.parse(body);
var responseText = `In ${loc}, It's ${response.weather[0].description} and temperature is ${response.main.temp} °C`
let responobj = {
"fulfillmentText": responseText,
"fulfillmentmessages": "",
"source" : " ",
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "this is a simple response"
}
}
]
}
}
}
}
// return res(responobj);
// res.send(responobj);
console.log("Response send")
res.end(JSON.stringify(responobj));
});
});
}
});
// app.use(bodyparser);
app.listen(80);//the server object listens on port 8080
// function ('/webhook'){
//
// }