Split codes to 4 Parts.
1. app.js - API Endpoint ( Receive requests from Facebook page and start to make request to Facebook Graph API ) - Open port ( app.listen(PORT_NUMBER) ), Perform verification ( app.get('/webhook') ) - Use/Call Handler function from controller.js 2. controller.js - Handle Message or Postback - Use/Call model.js / template.js to make response - Make request to Facebook's Graph API ( Messaging Platform ) using response, sender_psid 3. template.js - Make JSON format using data given from controller.js - Return JSON format ( to be 'response' object in controller.js ) 4. models.js - Get data from database. - Give data required by controller.js.
Showing
5 changed files
with
150 additions
and
144 deletions
1 | -+2019/5/25 | 1 | +### mother project : LINEBOT |
2 | -+mother project : LINEBOT | 2 | +최근 수정일 : 2019/5/31<br> |
3 | -+new project : 페이스북 메신저 기반 컴공/소융 강의평가 봇 | 3 | +new project : 페이스북 메신저 기반 컴공/소융 강의평가 봇<br> |
4 | -+ | 4 | +Description : Everytime 강의 평가 및 과 홈페이지를 기반으로 함. |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -2,9 +2,8 @@ | ... | @@ -2,9 +2,8 @@ |
2 | let express = require("express"), | 2 | let express = require("express"), |
3 | bodyParser= require("body-parser"), | 3 | bodyParser= require("body-parser"), |
4 | app = express(), | 4 | app = express(), |
5 | - request = require('request'), | ||
6 | config = require('config'), | 5 | config = require('config'), |
7 | - images = require("./pics"); | 6 | + controller = require('./controller'); |
8 | 7 | ||
9 | app.use(bodyParser.urlencoded({ extended : false})); | 8 | app.use(bodyParser.urlencoded({ extended : false})); |
10 | app.use(bodyParser.json()); | 9 | app.use(bodyParser.json()); |
... | @@ -20,7 +19,7 @@ app.get('/', (req, res) => res.send('Hello World!')); | ... | @@ -20,7 +19,7 @@ app.get('/', (req, res) => res.send('Hello World!')); |
20 | app.get('/webhook', (req, res) => { | 19 | app.get('/webhook', (req, res) => { |
21 | 20 | ||
22 | // Your verify token, Should be a random string. | 21 | // Your verify token, Should be a random string. |
23 | - let VERIFY_TOKEN = "2016104171"; | 22 | + let VERIFY_TOKEN = config.get('facebook.page.verify_token'); |
24 | 23 | ||
25 | // Parse the query params | 24 | // Parse the query params |
26 | let mode = req.query['hub.mode']; | 25 | let mode = req.query['hub.mode']; |
... | @@ -67,151 +66,17 @@ app.post('/webhook', (req, res) => { | ... | @@ -67,151 +66,17 @@ app.post('/webhook', (req, res) => { |
67 | // Check if the event is a message or postback and | 66 | // Check if the event is a message or postback and |
68 | // pass the event to the appropriate handler function | 67 | // pass the event to the appropriate handler function |
69 | if (webhook_event.message) { | 68 | if (webhook_event.message) { |
70 | - handleMessage(sender_psid, webhook_event.message); | 69 | + controller.handleMessage(sender_psid, webhook_event.message); |
71 | } else if (webhook_event.postback) { | 70 | } else if (webhook_event.postback) { |
72 | - handlePostback(sender_psid, webhook_event.postback); | 71 | + controller.handlePostback(sender_psid, webhook_event.postback); |
73 | } | 72 | } |
74 | }); | 73 | }); |
75 | - | ||
76 | // Returns a '200 OK' response to all requests | 74 | // Returns a '200 OK' response to all requests |
77 | res.status(200).send('EVENT_RECEIVED'); | 75 | res.status(200).send('EVENT_RECEIVED'); |
78 | } else { | 76 | } else { |
79 | - // Returns a '404 Not Found' if event is not from a page subscription | 77 | + // Returns a '404 Not Found' if event is ninteliot from a page subscription |
80 | res.sendStatus(404); | 78 | res.sendStatus(404); |
81 | } | 79 | } |
82 | - | ||
83 | }); | 80 | }); |
84 | 81 | ||
85 | -// Views - handle Message, handle Postback | ||
86 | - | ||
87 | -// Handles message events | ||
88 | -const handleMessage = (sender_psid, received_message) => { | ||
89 | - let response; | ||
90 | - | ||
91 | - if(received_message.text){ | ||
92 | - | ||
93 | - // Create the payload for a basic text message | ||
94 | - response = askTemplate() | ||
95 | - } | ||
96 | - | ||
97 | - // Sends the reponse message | ||
98 | - callSendAPI(sender_psid, response; | ||
99 | -} | ||
100 | - | ||
101 | -const handlePostback = (sender_psid, received_postback) => { | ||
102 | - let response; | ||
103 | - | ||
104 | - // Get the payload for the postback | ||
105 | - let payload = received_postback.payload; | ||
106 | - | ||
107 | - // Set the response based on the postback payload | ||
108 | - if (payload === 'CAT_PICS') { | ||
109 | - response = imageTemplate('cats', sender_psid); | ||
110 | - callSendAPI(sender_psid, response, function(){ | ||
111 | - callSendAPI(sender_psid, askTemplate('Show me more')); | ||
112 | - }); | ||
113 | - } else if (payload === 'DOG_PICS') { | ||
114 | - response = imageTemplate('dogs', sender_psid); | ||
115 | - callSendAPI(sender_psid, response, function(){ | ||
116 | - callSendAPI(sender_psid, askTemplate('Show me more')); | ||
117 | - }); | ||
118 | - } else if(payload === 'GET_STARTED'){ | ||
119 | - response = askTemplate('Are you a Cat or Dog Person?'); | ||
120 | - callSendAPI(sender_psid, response); | ||
121 | - } | ||
122 | - // Send the message to acknowledge the postback | ||
123 | -} | ||
124 | - | ||
125 | -const askTemplate = (text) => { | ||
126 | - return { | ||
127 | - "attachment":{ | ||
128 | - "type":"template", | ||
129 | - "payload":{ | ||
130 | - "template_type":"button", | ||
131 | - "text": text, | ||
132 | - "buttons":[ | ||
133 | - { | ||
134 | - "type":"postback", | ||
135 | - "title":"Cats", | ||
136 | - "payload":"CAT_PICS" | ||
137 | - }, | ||
138 | - { | ||
139 | - "type":"postback", | ||
140 | - "title":"Dogs", | ||
141 | - "payload":"DOG_PICS" | ||
142 | - } | ||
143 | - ] | ||
144 | - } | ||
145 | - } | ||
146 | - } | ||
147 | -} | ||
148 | - | ||
149 | -// Sends response messages via the Send API | ||
150 | -const callSendAPI = (sender_psid, response, cb = null) => { | ||
151 | - // Construct the message body | ||
152 | - let request_body = { | ||
153 | - "recipient": { | ||
154 | - "id": sender_psid | ||
155 | - }, | ||
156 | - "message": response | ||
157 | - }; | ||
158 | - | ||
159 | - // Send the HTTP request to the Messenger Platform | ||
160 | - request({ | ||
161 | - "uri": "https://graph.facebook.com/v2.6/me/messages", | ||
162 | - "qs": { "access_token": config.get('facebook.page.access_token') }, | ||
163 | - "method": "POST", | ||
164 | - "json": request_body | ||
165 | - }, (err, res, body) => { | ||
166 | - if (!err) { | ||
167 | - if(cb){ | ||
168 | - cb(); | ||
169 | - } | ||
170 | - } else { | ||
171 | - console.error("Unable to send message:" + err); | ||
172 | - } | ||
173 | - }); | ||
174 | -} | ||
175 | - | ||
176 | -const imageTemplate= (type, sender_id) => { | ||
177 | - return { | ||
178 | - "attachment":{ | ||
179 | - "type":"image", | ||
180 | - "payload":{ | ||
181 | - "url": getImage(type, sender_id), | ||
182 | - "is_reusable":true | ||
183 | - } | ||
184 | - } | ||
185 | - } | ||
186 | -} | ||
187 | - | ||
188 | -let users = {}; | ||
189 | - | ||
190 | -const getImage= (type, sender_id) => { | ||
191 | - // create user if doesn't exist | ||
192 | - if(users[sender_id] === undefined){ | ||
193 | - users = Object.assign({ | ||
194 | - [sender_id] : { | ||
195 | - 'cats_count' : 0, | ||
196 | - 'dogs_count' : 0 | ||
197 | - } | ||
198 | - }, users); | ||
199 | - } | ||
200 | - | ||
201 | - let count = images[type].length, // total available images by type | ||
202 | - user = users[sender_id], // // user requesting image | ||
203 | - user_type_count = user[type+'_count']; | ||
204 | - | ||
205 | - | ||
206 | - // update user before returning image | ||
207 | - let updated_user = { | ||
208 | - [sender_id] : Object.assign(user, { | ||
209 | - [type+'_count'] : count === user_type_count + 1 ? 0 : user_type_count + 1 | ||
210 | - }) | ||
211 | - }; | ||
212 | - // update users | ||
213 | - users = Object.assign(users, updated_user); | ||
214 | 82 | ||
215 | - console.log(users); | ||
216 | - return images[type][user_type_count]; | ||
217 | -} | ... | ... |
controller.js
0 → 100644
1 | +let request = require('request'), | ||
2 | + template = require('./template'), | ||
3 | + config = require('config'); | ||
4 | + | ||
5 | +// Views - handle Message, handle Postback | ||
6 | + | ||
7 | +// Handles message events | ||
8 | +exports.handleMessage = (sender_psid, received_message) => { | ||
9 | + let response; | ||
10 | + | ||
11 | + if(received_message.text){ | ||
12 | + | ||
13 | + // Create the payload for a basic text message | ||
14 | + response = template.askTemplate() | ||
15 | + } | ||
16 | + | ||
17 | + // Sends the reponse message | ||
18 | + callSendAPI(sender_psid, response); | ||
19 | +} | ||
20 | + | ||
21 | +exports.handlePostback = (sender_psid, received_postback) => { | ||
22 | + let response; | ||
23 | + | ||
24 | + // Get the payload for the postback | ||
25 | + let payload = received_postback.payload; | ||
26 | + | ||
27 | + // Set the response based on the postback payload | ||
28 | + if (payload === 'CAT_PICS') { | ||
29 | + response = template.imageTemplate('cats', sender_psid); | ||
30 | + callSendAPI(sender_psid, response, function(){ | ||
31 | + callSendAPI(sender_psid, template.askTemplate('Show me more')); | ||
32 | + }); | ||
33 | + } else if (payload === 'DOG_PICS') { | ||
34 | + response = template.imageTemplate('dogs', sender_psid); | ||
35 | + callSendAPI(sender_psid, response, function(){ | ||
36 | + callSendAPI(sender_psid, template.askTemplate('Show me more')); | ||
37 | + }); | ||
38 | + } else if(payload === 'GET_STARTED'){ | ||
39 | + response = template.askTemplate('Are you a Cat or Dog Person?'); | ||
40 | + callSendAPI(sender_psid, response); | ||
41 | + } | ||
42 | + // Send the message to acknowledge the postback | ||
43 | +} | ||
44 | + | ||
45 | +// Sends response messages via the Send API | ||
46 | +const callSendAPI = (sender_psid, response, cb = null) => { | ||
47 | + // Construct the message body | ||
48 | + let request_body = { | ||
49 | + "recipient": { | ||
50 | + "id": sender_psid | ||
51 | + }, | ||
52 | + "message": response | ||
53 | + }; | ||
54 | + | ||
55 | + // Send the HTTP request to the Messenger Platform | ||
56 | + request({ | ||
57 | + "uri": "https://graph.facebook.com/v2.6/me/messages", | ||
58 | + "qs": { "access_token": config.get('facebook.page.access_token') }, | ||
59 | + "method": "POST", | ||
60 | + "json": request_body | ||
61 | + }, (err, res, body) => { | ||
62 | + if (!err) { | ||
63 | + if(cb){ | ||
64 | + cb(); | ||
65 | + } | ||
66 | + } else { | ||
67 | + console.error("Unable to send message:" + err); | ||
68 | + } | ||
69 | + }); | ||
70 | +} |
models.js
0 → 100644
File mode changed
template.js
0 → 100644
1 | +let images = require("./pics"); | ||
2 | + | ||
3 | + | ||
4 | +exports.askTemplate = (text) => { | ||
5 | + return { | ||
6 | + "attachment":{ | ||
7 | + "type":"template", | ||
8 | + "payload":{ | ||
9 | + "template_type":"button", | ||
10 | + "text": text, | ||
11 | + "buttons":[ | ||
12 | + { | ||
13 | + "type":"postback", | ||
14 | + "title":"Cats", | ||
15 | + "payload":"CAT_PICS" | ||
16 | + }, | ||
17 | + { | ||
18 | + "type":"postback", | ||
19 | + "title":"Dogs", | ||
20 | + "payload":"DOG_PICS" | ||
21 | + } | ||
22 | + ] | ||
23 | + } | ||
24 | + } | ||
25 | + } | ||
26 | +} | ||
27 | + | ||
28 | + | ||
29 | + | ||
30 | +exports.imageTemplate= (type, sender_id) => { | ||
31 | + return { | ||
32 | + "attachment":{ | ||
33 | + "type":"image", | ||
34 | + "payload":{ | ||
35 | + "url": getImage(type, sender_id), | ||
36 | + "is_reusable":true | ||
37 | + } | ||
38 | + } | ||
39 | + } | ||
40 | +} | ||
41 | + | ||
42 | +let users = {}; | ||
43 | + | ||
44 | +const getImage= (type, sender_id) => { | ||
45 | + // create user if doesn't exist | ||
46 | + if(users[sender_id] === undefined){ | ||
47 | + users = Object.assign({ | ||
48 | + [sender_id] : { | ||
49 | + 'cats_count' : 0, | ||
50 | + 'dogs_count' : 0 | ||
51 | + } | ||
52 | + }, users); | ||
53 | + } | ||
54 | + | ||
55 | + let count = images[type].length, // total available images by type | ||
56 | + user = users[sender_id], // // user requesting image | ||
57 | + user_type_count = user[type+'_count']; | ||
58 | + | ||
59 | + | ||
60 | + // update user before returning image | ||
61 | + let updated_user = { | ||
62 | + [sender_id] : Object.assign(user, { | ||
63 | + [type+'_count'] : count === user_type_count + 1 ? 0 : user_type_count + 1 | ||
64 | + }) | ||
65 | + }; | ||
66 | + // update users | ||
67 | + users = Object.assign(users, updated_user); | ||
68 | + | ||
69 | + console.log(users); | ||
70 | + return images[type][user_type_count]; | ||
71 | +} |
-
Please register or login to post a comment