Showing
4 changed files
with
286 additions
and
221 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(); |
1 | -const db = require('./db'); | 1 | +var db=require('./db.js'); |
2 | 2 | ||
3 | -let greetingText = "안녕하세요! 컴공/소융 강의평가 봇이에요. 아래에 버튼 3개를 눌러주세요."; // 인사말 | 3 | +var greetingText = "안녕하세요! 컴공/소융 강의평가 봇이에요. 아래에 버튼 3개를 눌러주세요."; // 인사말 |
4 | -let greetingTitle = ["교수명으로 검색", "강의명으로 검색", "HELP"]; | 4 | +var greetingTitle = ["교수명으로 검색", "강의명으로 검색", "HELP"]; |
5 | 5 | ||
6 | 6 | ||
7 | -exports.greetingTemplate = () => { | 7 | +exports.greetingTemplate = function(cb){ |
8 | - return { | 8 | + var text={ |
9 | "attachment":{ | 9 | "attachment":{ |
10 | "type":"template", | 10 | "type":"template", |
11 | "payload":{ | 11 | "payload":{ |
... | @@ -30,191 +30,232 @@ exports.greetingTemplate = () => { | ... | @@ -30,191 +30,232 @@ exports.greetingTemplate = () => { |
30 | ] | 30 | ] |
31 | } | 31 | } |
32 | } | 32 | } |
33 | - } | 33 | + }; |
34 | + cb(true, text); | ||
34 | } | 35 | } |
35 | 36 | ||
36 | //stat_0 | 37 | //stat_0 |
37 | -exports.getProfNameTemplate = (received) => { | 38 | +exports.getProfNameTemplate = function(cb){ |
38 | - return { | 39 | + var text={ |
39 | "attachment":{ | 40 | "attachment":{ |
40 | "type":"template", | 41 | "type":"template", |
41 | "payload":{ | 42 | "payload":{ |
42 | "template_type":"button", | 43 | "template_type":"button", |
43 | - "text": "교수명을 입력하세요.", | 44 | + "text": "교수명을 입력하세요.", |
44 | - "buttons":[ | 45 | + "buttons":[ |
45 | { | 46 | { |
46 | - "type":"postback", | 47 | + "type":"postback", |
47 | - "title":"처음으로", | 48 | + "title":"처음으로", |
48 | - "payload":'Greeting' | 49 | + "payload":"Greeting" |
49 | - } | 50 | + } |
50 | - ] | 51 | + ] |
51 | } | 52 | } |
52 | } | 53 | } |
53 | - } | 54 | + }; |
55 | + cb(true, text); | ||
54 | } | 56 | } |
55 | 57 | ||
56 | -exports.getLectNameTemplate = (received) => { | 58 | +exports.getLectNameTemplate = function(cb){ |
57 | - return { | 59 | + var text={ |
58 | "attachment":{ | 60 | "attachment":{ |
59 | "type":"template", | 61 | "type":"template", |
60 | "payload":{ | 62 | "payload":{ |
61 | "template_type":"button", | 63 | "template_type":"button", |
62 | - "text": "강의명을 입력하세요.", | 64 | + "text": "강의명을 입력하세요.", |
63 | - "buttons":[ | 65 | + "buttons":[ |
64 | { | 66 | { |
65 | - "type":"postback", | 67 | + "type":"postback", |
66 | - "title":"처음으로", | 68 | + "title":"처음으로", |
67 | - "payload":'Greeting' | 69 | + "payload":"Greeting" |
68 | - } | 70 | + } |
69 | - ] | 71 | + ] |
70 | } | 72 | } |
71 | } | 73 | } |
72 | - } | 74 | + }; |
75 | + cb(true, text); | ||
73 | } | 76 | } |
74 | 77 | ||
75 | -//stat_1 | 78 | +exports.choiceLectProfTemplate=function(message,cb) { |
76 | -exports.choiceLectProfTemplate = (received) => { | 79 | + var sqlquery = 'SELECT * FROM reviews WHERE lecturename = ? OR proname = ?'; |
77 | - let message = received.text; | 80 | + var par = [message,message]; |
78 | - db.query('SELECT * FROM reviews WHERE lecturename LIKE ? OR proname LIKE ?', [message,message], function(error,results){ | 81 | + db.query(sqlquery, par, function(error,results){ |
79 | - if (err){ | 82 | + if (error){ |
80 | - console.log(err); | 83 | + console.log(error); |
81 | res.render('error'); | 84 | res.render('error'); |
82 | } | 85 | } |
83 | - if(results==0){ | 86 | + if(results.length==0){ |
84 | - return {text: "검색 결과가 없습니다. 검색어를 다시 입력해주세요."} | 87 | + cb(true, {text: "검색 결과가 없습니다. 검색어를 다시 입력해주세요."}); |
85 | }else if(results.length<=2){ | 88 | }else if(results.length<=2){ |
86 | - return LectProfList(results,0,results.length,false,'0',message); | 89 | + LectProfList(results,0,results.length,false,'0',message, function(result, text){ |
90 | + if(result==true){ | ||
91 | + cb(true, text); | ||
92 | + } | ||
93 | + }); | ||
87 | }else if(results.length>2){ | 94 | }else if(results.length>2){ |
88 | - return LectProfList(results,0,2,true,'1',message); | 95 | + LectProfList(results,0,2,true,'1',message, function(result, text){ |
96 | + if(result==true){ | ||
97 | + cb(true, text); | ||
98 | + } | ||
99 | + }); | ||
89 | } | 100 | } |
90 | }); | 101 | }); |
91 | } | 102 | } |
92 | 103 | ||
93 | -const LectProfList = (results,start,length,ismore,morecount,message) => { | 104 | +const LectProfList = function(results,start,length,ismore,morecount,message,cb){ |
94 | if(length==1){ | 105 | if(length==1){ |
95 | - return { | 106 | + var title=results[start].lecturename+results[start].proname; |
96 | - "attachment":{ | 107 | + var payload='CHOICE_BY_PROFstat_3'+results[start].idreviews; |
97 | - "type":"template", | 108 | + var text={ |
98 | - "payload":{ | 109 | + "attachment":{ |
99 | - "template_type":"button", | 110 | + "type":"template", |
100 | - "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요.", | 111 | + "payload":{ |
101 | - "buttons":[ | 112 | + "template_type":"button", |
102 | - { | 113 | + "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요.", |
103 | - "type":"postback", | 114 | + "buttons":[ |
104 | - "title":results[start].lecturename+results[start].proname, | 115 | + { |
105 | - "payload":'CHOICE_BY_PROFstat_3'+results[start].idreviews | 116 | + "type":"postback", |
106 | - } | 117 | + "title":title, |
118 | + "payload":payload | ||
119 | + } | ||
107 | ] | 120 | ] |
108 | } | 121 | } |
109 | } | 122 | } |
110 | - } | 123 | + }; |
124 | + cb(true, text); | ||
111 | }else if((length == 2)&&(!ismore)){ | 125 | }else if((length == 2)&&(!ismore)){ |
112 | - return { | 126 | + var title=[results[start].lecturename+results[start].proname, results[start+1].lecturename+results[start+1].proname]; |
113 | - "attachment":{ | 127 | + var payload=['CHOICE_BY_PROFstat_3'+results[start].idreviews, 'CHOICE_BY_PROFstat_3'+results[start+1].idreviews]; |
114 | - "type":"template", | 128 | + var text={ |
115 | - "payload":{ | 129 | + "attachment":{ |
116 | - "template_type":"button", | 130 | + "type":"template", |
117 | - "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요.", | 131 | + "payload":{ |
118 | - "buttons":[ | 132 | + "template_type":"button", |
133 | + "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요.", | ||
134 | + "buttons":[ | ||
119 | { | 135 | { |
120 | "type":"postback", | 136 | "type":"postback", |
121 | - "title":results[start].lecturename+results[start].proname, | 137 | + "title":title[0], |
122 | - "payload":'CHOICE_BY_PROFstat_3'+results[start].idreviews | 138 | + "payload":title[0] |
123 | }, | 139 | }, |
124 | { | 140 | { |
125 | "type":"postback", | 141 | "type":"postback", |
126 | - "title":results[start+1].lecturename+results[start+1].proname, | 142 | + "title":title[1], |
127 | - "payload":'CHOICE_BY_PROFstat_3'+results[start+1].idreviews | 143 | + "payload":payload[1] |
128 | - } | ||
129 | - ] | ||
130 | } | 144 | } |
145 | + ] | ||
131 | } | 146 | } |
132 | } | 147 | } |
133 | - }else if((length == 2)&&(ismore)){ | 148 | + }; |
134 | - return { | 149 | + cb(true, text); |
135 | - "attachment":{ | 150 | + }else if((length == 2)&&(ismore)){ |
136 | - "type":"template", | 151 | + var title=[results[start].lecturename+results[start].proname, results[start+1].lecturename+results[start+1].proname]; |
137 | - "payload":{ | 152 | + var payload=['CHOICE_BY_PROFstat_3'+results[start].idreviews, 'CHOICE_BY_PROFstat_3'+results[start+1].idreviews, 'CHOICE_BY_PROFstat_2'+message+'/'+morecount]; |
138 | - "template_type":"button", | 153 | + var text={ |
139 | - "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요. 원하는 강의가 없다면 [더보기]를 눌러주세요.", | 154 | + "attachment":{ |
140 | - "buttons":[ | 155 | + "type":"template", |
156 | + "payload":{ | ||
157 | + "template_type":"button", | ||
158 | + "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요. 원하는 강의가 없다면 [더보기]를 눌러주세요.", | ||
159 | + "buttons":[ | ||
141 | { | 160 | { |
142 | "type":"postback", | 161 | "type":"postback", |
143 | - "title":results[start].lecturename+results[start].proname, | 162 | + "title":title[0], |
144 | - "payload":'CHOICE_BY_PROFstat_3'+results[start].idreviews | 163 | + "payload":payload[0] |
145 | }, | 164 | }, |
146 | { | 165 | { |
147 | "type":"postback", | 166 | "type":"postback", |
148 | - "title":results[start+1].lecturename+results[start+1].proname, | 167 | + "title":title[1], |
149 | - "payload":'CHOICE_BY_PROFstat_3'+results[start+1].idreviews | 168 | + "payload":payload[1] |
150 | }, | 169 | }, |
151 | { | 170 | { |
152 | "type":"postback", | 171 | "type":"postback", |
153 | "title":"더보기", | 172 | "title":"더보기", |
154 | - "payload":'CHOICE_BY_PROFstat_2'+message+'/'+morecount | 173 | + "payload":payload[2] |
155 | - } | ||
156 | - ] | ||
157 | } | 174 | } |
175 | + ] | ||
158 | } | 176 | } |
159 | } | 177 | } |
178 | + }; | ||
179 | + cb(true, text); | ||
160 | } | 180 | } |
161 | - | ||
162 | } | 181 | } |
163 | 182 | ||
164 | //stat_2 | 183 | //stat_2 |
165 | -exports.moreProfTemplate = (received) => { | 184 | +exports.moreProfTemplate = function(payload,cb){ |
166 | - let ms=received.payload.substr(20).split('/'); | 185 | + var ms=payload.substr(20).split('/'); |
167 | - let more=1*(ms[0]); //to Number | 186 | + var more=1*(ms[0]); //to Number |
168 | - let message=ms[1]; | 187 | + var message=ms[1]; |
169 | 188 | ||
170 | - db.query('SELECT * FROM reviews WHERE lecturename LIKE ? OR proname LIKE ?', [message,message], function(error,results){ | 189 | + var sqlquery='SELECT * FROM reviews WHERE lecturename = ? OR proname = ?'; |
171 | - if (err){ | 190 | + var par=[message,message]; |
172 | - console.log(err); | 191 | + db.query(sqlquery, par, function(error,results){ |
192 | + if (error){ | ||
193 | + console.log(error); | ||
173 | res.render('error'); | 194 | res.render('error'); |
174 | } | 195 | } |
175 | if((results.length-2*(more+1))<=0){ | 196 | if((results.length-2*(more+1))<=0){ |
176 | - return LectProfList(results,2*more,results.length-2*n,false,'0',message); | 197 | + LectProfList(results,2*more,results.length-2*n,false,'0',message, function(result, text){ |
198 | + if(result==true){ | ||
199 | + cb(true, text); | ||
200 | + } | ||
201 | + }); | ||
177 | }else{ | 202 | }else{ |
178 | - return LectProfList(results,2*more,2,true,""+(more+1),message); | 203 | + LectProfList(results,2*more,2,true,""+(more+1),message, function(result, text){ |
204 | + if(result==true){ | ||
205 | + cb(true, text); | ||
206 | + } | ||
207 | + }); | ||
179 | } | 208 | } |
180 | }); | 209 | }); |
181 | } | 210 | } |
182 | 211 | ||
183 | -exports.moreLectTemplate= (received) => { | 212 | +exports.moreLectTemplate= function(payload,cb){ |
184 | -let ms=received.payload.substr(20).split('/'); | 213 | + var ms=payload.substr(20).split('/'); |
185 | - let more=1*(ms[0]); | 214 | + var more=1*(ms[0]); |
186 | - let message=ms[1]; | 215 | + var message=ms[1]; |
187 | 216 | ||
188 | - db.query('SELECT * FROM reviews WHERE lecturename LIKE ? OR proname LIKE ?', [message,message], function(error,results){ | 217 | + var sqlquery='SELECT * FROM reviews WHERE lecturename = ? OR proname = ?'; |
189 | - if (err){ | 218 | + var par=[message,message]; |
190 | - console.log(err); | 219 | + db.query(sqlquery, par, function(error,results){ |
220 | + if (error){ | ||
221 | + console.log(error); | ||
191 | res.render('error'); | 222 | res.render('error'); |
192 | } | 223 | } |
193 | if((results.length-2*(more+1))<=0){ | 224 | if((results.length-2*(more+1))<=0){ |
194 | - return LectProfList(results,2*more,results.length-2*n,false,'0',message); | 225 | + LectProfList(results,2*more,results.length-2*n,false,'0',message, function(result, text){ |
226 | + if(result==true){ | ||
227 | + cb(true, text); | ||
228 | + } | ||
229 | + }); | ||
195 | }else{ | 230 | }else{ |
196 | - return LectProfList(results,2*more,2,true,""+(more+1),message); | 231 | + LectProfList(results,2*more,2,true,""+(more+1),message, function(result, text){ |
232 | + if(result==true){ | ||
233 | + cb(true, text); | ||
234 | + } | ||
235 | + }); | ||
197 | } | 236 | } |
198 | }); | 237 | }); |
199 | } | 238 | } |
200 | 239 | ||
201 | //stat_3 | 240 | //stat_3 |
202 | -exports.rateTemplate= (received) => { | 241 | +exports.rateTemplate= function(payload,cb){ |
203 | - let id = received.payload.substr(20); | 242 | + var id = payload.substr(20); |
204 | 243 | ||
205 | -db.query('SELECT * FROM reviews WHERE idreviews LIKE ?', [id], function(error,results){ | 244 | + var sqlquery='SELECT * FROM reviews WHERE idreviews = ?'; |
206 | - if (err){ | 245 | + var par=[id]; |
207 | - console.log(err); | 246 | + db.query(sqlquery, par, function(error,results){ |
247 | + if (error){ | ||
248 | + console.log(error); | ||
208 | res.render('error'); | 249 | res.render('error'); |
209 | } | 250 | } |
210 | 251 | ||
211 | - let proname=results[0].proname; | 252 | + var proname=results[0].proname; |
212 | - let lecname=results[0].lecname; | 253 | + var lecname=results[0].lecname; |
213 | - let reviews=results[0].review.split(','); | 254 | + var reviews=results[0].review.split(','); |
214 | - let review=reviews[0]; | 255 | + var review=reviews[0]; |
215 | - let rate=results[0].avg_rate; | 256 | + var rate=results[0].avg_rate; |
216 | - let botsay=[]; | 257 | + var botsay=[]; |
217 | - let randNum=0; | 258 | + var randNum=0; |
218 | if(rate != 0){ | 259 | if(rate != 0){ |
219 | Math.floor((Math.random() * (3-0+1))+0); | 260 | Math.floor((Math.random() * (3-0+1))+0); |
220 | } | 261 | } |
... | @@ -229,82 +270,67 @@ db.query('SELECT * FROM reviews WHERE idreviews LIKE ?', [id], function(error,re | ... | @@ -229,82 +270,67 @@ db.query('SELECT * FROM reviews WHERE idreviews LIKE ?', [id], function(error,re |
229 | botsay=["무조건 들어요!","갓갓 교수님의 갓갓 수업","안 들으면 후회해요ㅠㅠ", "수강신청 1순위!"]; | 270 | botsay=["무조건 들어요!","갓갓 교수님의 갓갓 수업","안 들으면 후회해요ㅠㅠ", "수강신청 1순위!"]; |
230 | } | 271 | } |
231 | 272 | ||
232 | - return { | 273 | + var payload='MoreRate'+id; |
274 | + var text={ | ||
233 | "attachment":{ | 275 | "attachment":{ |
234 | "type":"template", | 276 | "type":"template", |
235 | "payload":{ | 277 | "payload":{ |
236 | "template_type":"button", | 278 | "template_type":"button", |
237 | - "text": proname+" 교수님의 강의 ["+lecname+"]의 강의평입니다.\n **평균 평점: "+rate+"\n **봇의 한줄평: "+botsay[randNum]+"\n **학우의 한줄평: "+reivew, | 279 | + "text": proname+" 교수님의 강의 ["+lecname+"]의 강의평입니다.\n **평균 평점: "+rate+"\n **봇의 한줄평: "+botsay[randNum]+"\n **학우의 한줄평: "+reivew, |
238 | - "buttons":[ | 280 | + "buttons":[ |
239 | { | 281 | { |
240 | - "type":"postback", | 282 | + "type":"postback", |
241 | - "title":"자세한 강의평 보기", | 283 | + "title":"자세한 강의평 보기", |
242 | - "payload":'MoreRate'+id | 284 | + "payload":payload |
243 | - } | 285 | + } |
244 | - ] | 286 | + ] |
245 | } | 287 | } |
246 | } | 288 | } |
247 | - } | 289 | + }; |
248 | - | 290 | + cb(true, text); |
249 | }); | 291 | }); |
250 | } | 292 | } |
251 | 293 | ||
252 | -exports.moreRateTemplate = (received) => { | 294 | +exports.moreRateTemplate = function(payload,cb){ |
253 | - let id = received.payload.substr(8); | 295 | + var id = payload.substr(8); |
254 | - let total_review=""; | 296 | + var total_review=""; |
255 | 297 | ||
256 | - db.query('SELECT * FROM reviews WHERE idreviews LIKE ?', [id], function(error,results){ | 298 | + var sqlquery='SELECT * FROM reviews WHERE idreviews = ?'; |
257 | - if (err){ | 299 | + var par=[id]; |
258 | - console.log(err); | 300 | + db.query(sqlquery, par, function(error,results){ |
301 | + if (error){ | ||
302 | + console.log(error); | ||
259 | res.render('error'); | 303 | res.render('error'); |
260 | } | 304 | } |
261 | 305 | ||
262 | - let reviews=results[0].review.split(','); | 306 | + var reviews=results[0].review.split(','); |
263 | for(var review in reviews){ | 307 | for(var review in reviews){ |
264 | total_review+=review+"\n"; | 308 | total_review+=review+"\n"; |
265 | } | 309 | } |
266 | 310 | ||
267 | - return {text:total_review} | 311 | + cb(true,{text:total_review}); |
268 | }); | 312 | }); |
269 | - //payload:MoreRate(강의식별번호) | ||
270 | - //포스트백 핸들 moreRateTemplate에서 해당 강의의 자세한 평 출력 | ||
271 | } | 313 | } |
272 | 314 | ||
273 | -//payload === 'HELP' | ||
274 | -exports.howToTemplate= (received) => { | ||
275 | -return { | ||
276 | - "attachment":{ | ||
277 | - "type":"template", | ||
278 | - "payload":{ | ||
279 | - "template_type":"button", | ||
280 | - "text": "안녕하세요. 경희대학교 컴공/소융 강의평가봇입니다. \n 자세한 사용방법은 아래를 참고하세요. \n\n 1. [교수명으로 검색], [강의명으로 검색] 중 하나를 선택한다. \n 2. 찾고자하는 강의의 교수명/강의명을 입력한다. \n 3. 입력하신 정보에 일치하는 강의/교수 목록 버튼 중 하나를 선택한다. \n 4. 강의/교수 버튼을 선택하면 해당 강의의 강의평가가 제공된다. \n 4-1. 더보기 버튼을 선택하면 해당 조건의 강의를 더 볼 수 있다. \n 5. [자세한 평보기] 버튼을 눌러 학우들의 생생한 강의평가를 자세히 본다. \n\n *언제든지 \"처음으로\"를 입력하면 첫 화면으로 돌아갑니다. \n 그럼 아래 버튼을 눌러 강의평가를 조회해보세요.", | ||
281 | - "buttons":[ | ||
282 | - { | ||
283 | - "type":"postback", | ||
284 | - "title":"처음으로", | ||
285 | - "payload":'Greeting' | ||
286 | - } | ||
287 | - ] | ||
288 | - } | ||
289 | - } | ||
290 | - } | ||
291 | -} | ||
292 | 315 | ||
293 | -exports.goToGreetTemplate = (received) => { | 316 | +//payload === 'HELP' |
294 | -return { | 317 | +exports.howToTemplate = function(cb){ |
318 | + var helptext="안녕하세요. 경희대학교 컴공/소융 강의평가봇입니다. \n\n 1. [교수명으로 검색], [강의명으로 검색] 중 하나를 선택한다. \n 2. 찾고자하는 강의의 교수명/강의명을 입력한다. \n 3. 입력하신 정보에 일치하는 강의/교수 목록 버튼 중 하나를 선택한다. \n 4. 강의/교수 버튼을 선택하면 해당 강의의 강의평가가 제공된다. \n 4-1. 더보기 버튼을 선택하면 해당 조건의 강의를 더 볼 수 있다. \n 5. [자세한 평보기] 버튼을 눌러 학우들의 생생한 강의평가를 자세히 본다. \n\n *언제든지 \"처음으로\"를 입력하면 첫 화면으로 돌아갑니다. \n\n 그럼 아래 버튼을 눌러 강의평가를 조회해보세요."; | ||
319 | + var text={ | ||
295 | "attachment":{ | 320 | "attachment":{ |
296 | "type":"template", | 321 | "type":"template", |
297 | "payload":{ | 322 | "payload":{ |
298 | "template_type":"button", | 323 | "template_type":"button", |
299 | - "text": "", | 324 | + "text": helptext, |
300 | - "buttons":[ | 325 | + "buttons":[ |
301 | { | 326 | { |
302 | - "type":"postback", | 327 | + "type":"postback", |
303 | - "title":"처음으로", | 328 | + "title":"처음으로", |
304 | - "payload":'Greeting' | 329 | + "payload":"Greeting" |
305 | - } | 330 | + } |
306 | - ] | 331 | + ] |
307 | } | 332 | } |
308 | } | 333 | } |
309 | - } | 334 | + }; |
335 | + cb(true,text); | ||
310 | } | 336 | } | ... | ... |
-
Please register or login to post a comment