-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
217 lines (199 loc) · 7.85 KB
/
app.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// ///////TODO Apply memeory to all parts of sending process
// //////TODO Add instruction at the begining
import { getUser } from './app/controllers/users';
import { uploadToAccount } from './app/imgur_handler/api_consumer';
import {
verifyRequestSignature, receivedDeliveryConfirmation, receivedPostback,
receivedAccountLink, receivedAuthentication, receivedMessageRead,
} from './app/messages/events';
import { getFirstName } from './app/helpers/facebook_apis';
import { checkMessageContent } from './app/messages/receiver';
import { handlePayload } from './app/messages/payload';
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'staging') {
require('dotenv').config();
}
const bodyParser = require('body-parser');
const express = require('express');
const tools = require('./app/helpers/send_functions');
const app = express();
app.set('port', process.env.PORT || 1245);
app.set('view engine', 'ejs');
app.use(bodyParser.json({ verify: verifyRequestSignature }));
app.use(express.static('public'));
// App Secret can be retrieved from the App Dashboard
const APP_SECRET = (process.env.MESSENGER_APP_SECRET)
? process.env.MESSENGER_APP_SECRET
: process.env.APP_SECRET;
// Arbitrary value used to validate a webhook
const VALIDATION_TOKEN = (process.env.MESSENGER_VALIDATION_TOKEN)
? (process.env.MESSENGER_VALIDATION_TOKEN)
: process.env.VALIDATION_TOKEN;
// Generate a page access token for your page from the App Dashboard
const PAGE_ACCESS_TOKEN = (process.env.MESSENGER_PAGE_ACCESS_TOKEN)
? (process.env.MESSENGER_PAGE_ACCESS_TOKEN)
: process.env.PAGE_ACCESS_TOKEN;
// tools.PAGE_ACCESS_TOKEN= PAGE_ACCESS_TOKEN; //to use it in CallSendAPI
// URL where the app is running (include protocol). Used to point to scripts and
// assets located at this address.
const SERVER_URL = (process.env.SERVER_URL)
? (process.env.SERVER_URL)
: process.env.SERVER_URL;
if (!(APP_SECRET && VALIDATION_TOKEN && PAGE_ACCESS_TOKEN && SERVER_URL)) {
console.error('Missing config values');
process.exit(1);
}
app.get('/webhook', (req, res) => {
if (req.query['hub.mode'] === 'subscribe'
&& req.query['hub.verify_token'] === VALIDATION_TOKEN) {
console.log('Validating webhook');
res.status(200).send(req.query['hub.challenge']);
} else {
console.error('Failed validation. Make sure the validation tokens match.');
res.sendStatus(403);
}
});
function receivedMessage(event) {
let senderID = event.sender.id;
senderID = senderID.toString();
const recipientID = event.recipient.id;
const timeOfMessage = event.timestamp;
const { message } = event;
console.log('Received message for user %d and page %d at %d with message:',
senderID, recipientID, timeOfMessage);
console.log(JSON.stringify(message));
const isEcho = message.is_echo;
const messageId = message.mid;
const appId = message.app_id;
const { metadata } = message;
// You may get a text or attachment but not both
const messageText = message.text;
const messageAttachments = message.attachments;
const quickReply = message.quick_reply;
if (isEcho) {
// Just logging message echoes to conole COMMENTED FOR BETTER VISIBILLITY
// console.log("Received echo for message %s and app %d with metadata %s",
// messageId, appId, metadata);
return;
}
getUser(senderID, () => {
tools.sendReadReceipt(senderID);
if (quickReply) {
const quickReplyPayload = quickReply.payload;
console.log('Quick reply for message %s with payload %s',
messageId, quickReplyPayload);
tools.sendTypingOn(senderID); // typing on till fetching
handlePayload(quickReplyPayload, senderID);
return;
}
if (messageText) {
checkMessageContent(messageText, senderID);
} else if (messageAttachments) {
for (let i = 0; i < messageAttachments.length; i += 1) {
if (messageAttachments[i].type === 'image') {
const imageURL = messageAttachments[i].payload.url;
uploadToAccount(senderID, imageURL);
}
}
tools.sendTextMessage(senderID, 'Uploaded your meme/s for later happiness');
setTimeout(() => {
tools.sendTextMessage(senderID, "You can access this meme and other selected memes by typing 'my memes'");
}, 2000); // added timeout to make sure it comes later
}
tools.sendTypingOff(senderID);
});
}
/*
* All callbacks for Messenger are POST-ed. They will be sent to the same
* webhook. Be sure to subscribe your app to your page to receive callbacks
* for your page.
* https://developers.facebook.com/docs/messenger-platform/product-overview/setup#subscribe_app
*
*/
app.post('/webhook', (req, res) => {
const data = req.body;
// Make sure this is a page subscription
if (data.object === 'page') {
// Iterate over each entry
// There may be multiple if batched
data.entry.forEach((pageEntry) => {
const pageID = pageEntry.id;
const timeOfEvent = pageEntry.time;
// Iterate over each messaging event
pageEntry.messaging.forEach((messagingEvent) => {
if (messagingEvent.optin) {
receivedAuthentication(messagingEvent);
} else if (messagingEvent.message) {
receivedMessage(messagingEvent);
} else if (messagingEvent.delivery) {
receivedDeliveryConfirmation(messagingEvent);
} else if (messagingEvent.postback) {
receivedPostback(messagingEvent);
} else if (messagingEvent.read) {
receivedMessageRead(messagingEvent);
} else if (messagingEvent.account_linking) {
receivedAccountLink(messagingEvent);
} else {
console.log('Webhook received unknown messagingEvent: ', messagingEvent);
}
});
});
// Assume all went well.
//
// You must send back a 200, within 20 seconds, to let us know you've
// successfully received the callback. Otherwise, the request will time out.
res.sendStatus(200);
}
});
/*
* This path is used for account linking. The account linking call-to-action
* (sendAccountLinking) is pointed to this URL.
*
*/
app.get('/authorize', (req, res) => {
const accountLinkingToken = req.query.account_linking_token;
const redirectURI = req.query.redirect_uri;
// Authorization Code should be generated per user by the developer. This will
// be passed to the Account Linking callback.
const authCode = '1234567890';
// Redirect users to this URI on successful login
const redirectURISuccess = `${redirectURI}&authorization_code=${authCode}`;
res.render('authorize', {
accountLinkingToken,
redirectURI,
redirectURISuccess,
});
console.log('accountLinkingToken', accountLinkingToken);
getFirstName(accountLinkingToken, (err, data) => {
if (err) return console.error(err);
const user_first_name = data;
console.log('First NAMMMME', user_first_name);
const message_first_time = [`Hi ${user_first_name},`, "Try me by sending 'Send meme' or 'memes' "].join('\n');
// present user with some greeting or call to action
// tools.sendTextMessage(senderID, message_first_time);
});
});
// ImgurImagesConsumer('kaka', 'gallery', 'memes');
// sendMemeToUser('Khalod1');
// helpers.sendImageToNewUser('fuckme', '33333324ds32423sdfafadas');
// console.log(getUser('khal22ooood'));
// PromisedToDB('Khalod1')
// .then((data) => {
// console.log('=============================================');
// console.log('returned fresh data', data);
// })
// .catch((err) => console.error(`[Error]: ${err}`));
// PromisedsendToDialogflow("kilme")
// .then(data => {
// checkMessageContent(data, senderID);
// console.log(100*"==");
// console.log("data is " + data);
// }
// )
// .catch(err => console.error(`[Error]: ${err}`));
// Start server
// Webhooks must be available via SSL with a certificate signed by a valid
// certificate authority.
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'));
});
module.exports = app;