-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (88 loc) · 2.61 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const apiaiApp = require('apiai')('*****');
const app = express()
let token = "****"
let WEATHER_API_KEY = '*****'
app.set('port', (process.env.PORT || 5000))
// Allows us to process the data
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
// ROUTES
app.get('/', function(req, res) {
res.send("Hi I am a weather chatbot")
})
// Facebook
app.get('/webhook/', function(req, res) {
if (req.query['hub.verify_token'] === "helloWorld") {
res.send(req.query['hub.challenge'])
}
res.send("Wrong token")
})
app.post('/webhook', (req, res) => {
console.log(req.body);
if (req.body.object === 'page') {
req.body.entry.forEach((entry) => {
entry.messaging.forEach((event) => {
if (event.message && event.message.text) {
sendMessage(event);
}
});
});
res.status(200).end();
}
});
app.listen(app.get('port'), function() {
console.log("‘Webhook server is listening, port: 5000")
})
app.post('/ai', (req, res) => {
if (req.body.result.action === 'weather') {
let city = req.body.result.parameters['geo-city']
let restUrl = 'http://api.openweathermap.org/data/2.5/weather?APPID='+WEATHER_API_KEY+'&q='+city
request.get(restUrl, (err, response, body) => {
if (!err && response.statusCode == 200) {
let json = JSON.parse(body);
let msg = json.weather[0].description + ' and the temperature is ' + json.main.temp + ' ℉'
return res.json({
speech: msg,
displayText: msg,
source: 'weather'})
} else {
return res.status(400).json({
status: {
code: 400,
errorType: 'I failed to look up the city name.'}});
}})
}
})
function sendMessage(event) {
let sender = event.sender.id;
let text = event.message.text;
let apiai = apiaiApp.textRequest(text, {
sessionId: 'tabby_cat' // use any arbitrary id
});
apiai.on('response', (response) => {
let aiText = response.result.fulfillment.speech;
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: token},
method: 'POST',
json: {
recipient: {id: sender},
message: {text: aiText}
}
}, (error, response) => {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
});
apiai.on('error', (error) => {
console.log(error);
});
apiai.end();
}