-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (166 loc) · 5.3 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const express = require('express');
const bodyParser = require('body-parser');
const request = require('requestretry');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const server = app.listen(process.env.PORT || 3000, () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
app.get('/', (req, res) => {
res.redirect('https://m.me/joyjoythecatbot');
});
app.get('/webhook', (req, res) => {
if (req.query['hub.mode'] && req.query['hub.verify_token'] === process.env.VERIFY_TOKEN) {
res.status(200).send(req.query['hub.challenge']);
} else {
res.status(403).end();
}
});
/* Handling all messages */
app.post('/webhook', (req, res) => {
if (req.body.object === 'page') {
req.body.entry.forEach(entry => {
entry.messaging.forEach(event => {
if (event.message && (event.message.text || event.message.attachments)) {
acknowlegeMessageReceived(event);
setTimeout(() => {
prepareMessage(event);
}, 1000);
setTimeout(() => {
sendMessage(event);
cleanMessageState(event);
setTimeout(() => {
prepareMessage(event);
retrieveCat(event);
}, 1000);
}, 3000);
// TODO: Debounce requests in case app is hung up
}
});
});
res.status(200).end();
}
});
function acknowlegeMessageReceived(event) {
let sender = event.sender.id;
const options = {
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
"recipient": { id: sender },
"sender_action": "mark_seen"
}
}
request(options).then(response => {
if (response.body.error) {
console.log('Error: ', response.body.error);
}
}).catch(error => {
console.log('Error preparing message: ', error);
});
}
function prepareMessage(event) {
let sender = event.sender.id;
const options = {
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
"recipient": { id: sender },
"sender_action": "typing_on"
}
}
request(options).then(response => {
if (response.body.error) {
console.log('Error: ', response.body.error);
}
}).catch(error => {
console.log('Error preparing message: ', error);
});
}
function sendMessage(event) {
let sender = event.sender.id;
let text = "Joy Joy went off to fetch you one of her friends, please wait as she scurries off for a bit...";
const options = {
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
"recipient": { id: sender },
"message": { text: text }
}
};
request(options).then(response => {
if (response.body.error) {
console.log('Error: ', response.body.error);
}
}).catch(error => {
console.log('Error sending message: ', error);
});
}
function cleanMessageState(event) {
let sender = event.sender.id;
const options = {
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
"recipient": { id: sender },
"sender_action": "typing_off"
}
}
request(options).then(response => {
if (response.body.error) {
console.log('Error: ', response.body.error);
}
}).catch(error => {
console.log('Error preparing message: ', error);
});
}
function sendCat(event, gif) {
let sender = event.sender.id;
const options = {
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: {
"recipient": { "id": sender },
"message": {
"attachment": {
"type": "image",
"payload": {
"url": gif.data.image_url
}
}
}
}
};
request(options).then(response => {
if (response.body.error) {
console.log('Error: ', response.body.error);
}
}).catch(error => {
console.log('Error sending message: ', error);
});
}
function retrieveCat(event) {
const options = {
uri: 'http://api.giphy.com/v1/gifs/random',
qs: {
api_key: 'dc6zaTOxFJmzC',
tag: 'cat'
},
json: true
};
request(options).then(data => {
sendCat(event, data.body);
}).catch((error) => {
console.log('Error retrieving cat GIF from Giphy: ', error);
});
}
process.on('uncaughtException', error => {
console.log('An application error occurred: ', error);
process.exit(1);
});