Showing
4 changed files
with
102 additions
and
63 deletions
... | @@ -29,7 +29,7 @@ app.get("/webhook", function (req, res) { | ... | @@ -29,7 +29,7 @@ app.get("/webhook", function (req, res) { |
29 | // Creates the endpoint for our webhook | 29 | // Creates the endpoint for our webhook |
30 | app.post('/webhook', (req, res) => { | 30 | app.post('/webhook', (req, res) => { |
31 | 31 | ||
32 | - let body = req.body; | 32 | + var body = req.body; |
33 | 33 | ||
34 | if (body.object === 'page') { | 34 | if (body.object === 'page') { |
35 | 35 | ||
... | @@ -38,11 +38,11 @@ app.post('/webhook', (req, res) => { | ... | @@ -38,11 +38,11 @@ app.post('/webhook', (req, res) => { |
38 | 38 | ||
39 | // Gets the message. entry.messaging is an array, but | 39 | // Gets the message. entry.messaging is an array, but |
40 | // will only ever contain one message, so we get index 0 | 40 | // will only ever contain one message, so we get index 0 |
41 | - let webhook_event = entry.messaging[0]; | 41 | + var webhook_event = entry.messaging[0]; |
42 | console.log(webhook_event); | 42 | console.log(webhook_event); |
43 | 43 | ||
44 | // Get the sender PSID | 44 | // Get the sender PSID |
45 | - let sender_psid = webhook_event.sender.id; | 45 | + var sender_psid = webhook_event.sender.id; |
46 | console.log('Sender PSID: ' + sender_psid); | 46 | console.log('Sender PSID: ' + sender_psid); |
47 | 47 | ||
48 | 48 | ||
... | @@ -63,4 +63,3 @@ app.post('/webhook', (req, res) => { | ... | @@ -63,4 +63,3 @@ app.post('/webhook', (req, res) => { |
63 | } | 63 | } |
64 | }); | 64 | }); |
65 | 65 | ||
66 | - | ... | ... |
1 | -let request = require('request'), | 1 | +var request = require('request'); |
2 | - template = require('./template'); | 2 | +var template = require('./template.js'); |
3 | 3 | ||
4 | -// Handles message events | 4 | +exports.handleMessage=function(sender_psid, received_message) { |
5 | -exports.handleMessage = (sender_psid, received_message) => { | 5 | + var response; |
6 | - let response; | 6 | + var text = received_message.text; |
7 | - if(received_message.text === "처음으로"){ | 7 | + |
8 | - response = template.goToGreetTemplate(received_message); | 8 | + if(text === "처음으로"){ |
9 | + template.greetingTemplate(function(result, res){ | ||
10 | + if(result==true){ | ||
11 | + console.log(res); | ||
12 | + response=res; | ||
13 | + } | ||
14 | + }); | ||
15 | + callSendAPI(sender_psid, response); | ||
9 | }else{ | 16 | }else{ |
10 | - response = template.choiceLectProfTemplate(received_message); | 17 | + template.choiceLectProfTemplate(text, function(result, res){ |
18 | + if(result==true){ | ||
19 | + response=res; | ||
20 | + } | ||
21 | + }); | ||
11 | } | 22 | } |
12 | callSendAPI(sender_psid, response); | 23 | callSendAPI(sender_psid, response); |
13 | } | 24 | } |
14 | 25 | ||
15 | -exports.handlePostback = (sender_psid, received_postback) => { | 26 | +exports.handlePostback =function(sender_psid, received_postback) { |
16 | - let response; | 27 | + var response; |
17 | 28 | ||
18 | // Get the payload for the postback | 29 | // Get the payload for the postback |
19 | - let payload = received_postback.payload; | 30 | + var payload = received_postback.payload; |
31 | + var firstchoice = payload.substr(0,14); | ||
20 | 32 | ||
21 | // Set the response based on the postback payload | 33 | // Set the response based on the postback payload |
22 | if(payload === 'Greeting'){ | 34 | if(payload === 'Greeting'){ |
23 | - response = template.greetingTemplate(); | 35 | + console.log('Greeting'); |
36 | + template.greetingTemplate(function(result, res){ | ||
37 | + if(result==true){ | ||
38 | + console.log(res); | ||
39 | + response=res; | ||
40 | + } | ||
41 | + }); | ||
24 | callSendAPI(sender_psid, response); | 42 | callSendAPI(sender_psid, response); |
25 | - }else if(payload.substr(0,14)=== 'CHOICE_BY_PROF'){ | 43 | + }else if(firstchoice === 'CHOICE_BY_PROF'){ |
26 | handlePostback_choiceByProf(sender_psid, received_postback); | 44 | handlePostback_choiceByProf(sender_psid, received_postback); |
27 | - }else if(payload.substr(0,14)=== 'CHOICE_BY_LECT'){ | 45 | + }else if(firstchoice === 'CHOICE_BY_LECT'){ |
28 | - handlePostback_choiceByLect(sender_psid, recieved_postback); | 46 | + handlePostback_choiceByLect(sender_psid, received_postback); |
29 | - }else if(payload.substr(0,14) ==='CHOICE_BY_HELP'){ | 47 | + }else if(firstchoice === 'CHOICE_BY_HELP'){ |
30 | - handlePostback_help(sender_psid, recieved_postback); | 48 | + handlePostback_help(sender_psid, received_postback); |
31 | }else if(payload.substr(0,8) === 'MoreRate'){ | 49 | }else if(payload.substr(0,8) === 'MoreRate'){ |
32 | - response = template.moreRateTemplate(received_postback); | 50 | + template.moreRateTemplate(payload, function(result, res){ |
51 | + if(result==true){ | ||
52 | + console.log(res); | ||
53 | + response=res; | ||
54 | + } | ||
55 | + }); | ||
33 | callSendAPI(sender_psid, response); | 56 | callSendAPI(sender_psid, response); |
34 | } | 57 | } |
35 | } | 58 | } |
36 | 59 | ||
37 | -const handlePostback_choiceByProf = (sender_psid, received) => { | 60 | +const handlePostback_choiceByProf = function(sender_psid, received){ |
38 | - let response; | 61 | + var response; |
39 | - | 62 | + var payload = received.payload; |
40 | - let stat = received.payload.substr(14,6); | 63 | + var stat = payload.substr(14,6); |
41 | 64 | ||
42 | if(stat === 'stat_0'){ | 65 | if(stat === 'stat_0'){ |
43 | - response = template.getProfNameTemplate(received); | 66 | + template.getProfNameTemplate(function(result, res){ |
67 | + if(result==true){ | ||
68 | + console.log(res); | ||
69 | + response=res; | ||
70 | + } | ||
71 | + }); | ||
44 | }else if(stat === 'stat_2'){ | 72 | }else if(stat === 'stat_2'){ |
45 | - response = template.moreProfTemplate(received); | 73 | + template.moreProfTemplate(payload,function(result, res){ |
74 | + if(result==true){ | ||
75 | + console.log(res); | ||
76 | + response=res; | ||
77 | + } | ||
78 | + }); | ||
46 | }else if(stat === 'stat_3'){ | 79 | }else if(stat === 'stat_3'){ |
47 | - response = template.rateTemplate(received); | 80 | + template.rateTemplate(payload,function(result, res){ |
81 | + if(result==true){ | ||
82 | + console.log(res); | ||
83 | + response=res; | ||
84 | + } | ||
85 | + }); | ||
48 | } | 86 | } |
49 | callSendAPI(sender_psid, response); | 87 | callSendAPI(sender_psid, response); |
50 | } | 88 | } |
51 | 89 | ||
52 | -const handlePostback_choiceByLect = (sender_psid, recieved) => { | 90 | +const handlePostback_choiceByLect = function(sender_psid, received){ |
53 | - let response; | 91 | + var response; |
54 | - | 92 | + var payload = received.payload; |
55 | - let stat = recieved.payload.substr(14,6); | 93 | + var stat = payload.substr(14,6); |
56 | 94 | ||
57 | if(stat === 'stat_0'){ | 95 | if(stat === 'stat_0'){ |
58 | - response = template.getLectNameTemplate(received); | 96 | + template.getLectNameTemplate(function(result, res){ |
97 | + if(result==true){ | ||
98 | + console.log(res); | ||
99 | + response=res; | ||
100 | + } | ||
101 | + }); | ||
59 | }else if(stat === 'stat_2'){ | 102 | }else if(stat === 'stat_2'){ |
60 | - response = template.moreLectTemplate(received); | 103 | + template.moreLectTemplate(payload,function(result, res){ |
104 | + if(result==true){ | ||
105 | + console.log(res); | ||
106 | + response=res; | ||
107 | + } | ||
108 | + }); | ||
61 | }else if(stat === 'stat_3'){ | 109 | }else if(stat === 'stat_3'){ |
62 | - response = template.rateTemplate(received); | 110 | + template.rateTemplate(payload,function(result, res){ |
111 | + if(result==true){ | ||
112 | + console.log(res); | ||
113 | + response=res; | ||
114 | + } | ||
115 | + }); | ||
63 | } | 116 | } |
64 | callSendAPI(sender_psid, response); | 117 | callSendAPI(sender_psid, response); |
65 | } | 118 | } |
66 | 119 | ||
67 | -const handlePostback_help = (sender_psid, received) => { | 120 | +const handlePostback_help = function(sender_psid, received) { |
68 | - let response; | 121 | + var response; |
69 | - | 122 | + var payload = received.payload; |
70 | - let stat = received.payload.substr(14); | 123 | + var stat = payload.substr(14,6); |
71 | 124 | ||
72 | if(stat === 'stat_0'){ | 125 | if(stat === 'stat_0'){ |
73 | - response = template.howToTemplate(received); | 126 | + template.howToTemplate(function(result, res){ |
127 | + if(result==true){ | ||
128 | + console.log(res); | ||
129 | + response=res; | ||
130 | + } | ||
131 | + }); | ||
74 | } | 132 | } |
75 | callSendAPI(sender_psid, response); | 133 | callSendAPI(sender_psid, response); |
76 | } | 134 | } |
77 | 135 | ||
78 | // Sends response messages via the Send API | 136 | // Sends response messages via the Send API |
79 | -const callSendAPI = (sender_psid, response, cb = null) => { | 137 | +const callSendAPI = (sender_psid, response, cb=null) => { |
80 | console.log('response:'+response); | 138 | console.log('response:'+response); |
81 | // Send the HTTP request to the Messenger Platform | 139 | // Send the HTTP request to the Messenger Platform |
82 | request({ | 140 | request({ |
... | @@ -88,7 +146,9 @@ const callSendAPI = (sender_psid, response, cb = null) => { | ... | @@ -88,7 +146,9 @@ const callSendAPI = (sender_psid, response, cb = null) => { |
88 | message: response | 146 | message: response |
89 | } | 147 | } |
90 | }, (err, res, body) => { | 148 | }, (err, res, body) => { |
91 | - if (err) { | 149 | + if (!err) { |
150 | + console.log('message sent!') | ||
151 | + } else { | ||
92 | console.error("Unable to send message:" + err); | 152 | console.error("Unable to send message:" + err); |
93 | } | 153 | } |
94 | }); | 154 | }); | ... | ... |
db.js
deleted
100644 → 0
1 | -const mysql = require('mysql'); | ||
2 | -const db_config = require('./config/db-config.json'); | ||
3 | - | ||
4 | -let db; | ||
5 | -function connectDB() { | ||
6 | - if (!db) { | ||
7 | - db = mysql.createConnection(db_config); | ||
8 | - db.connect(function(err) { | ||
9 | - if (!err) { | ||
10 | - console.log('Database is connected'); | ||
11 | - } else { | ||
12 | - console.log('Error: ', err); | ||
13 | - } | ||
14 | - }); | ||
15 | - } | ||
16 | - | ||
17 | - return db; | ||
18 | -} | ||
19 | - | ||
20 | -module.exports = connectDB(); |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment