김예미

Connect DB, prototype

This diff could not be displayed because it is too large.
...@@ -63,3 +63,4 @@ app.post('/webhook', (req, res) => { ...@@ -63,3 +63,4 @@ app.post('/webhook', (req, res) => {
63 } 63 }
64 }); 64 });
65 65
66 +
......
1 { 1 {
2 "host": "localhost", 2 "host": "localhost",
3 "user": "root", 3 "user": "root",
4 - "password": null, 4 + "password": "111111",
5 - "host" : 3306, 5 + "host" : 3306,
6 - "database": "facebookbot" 6 + "database": "ratedb"
7 } 7 }
......
...@@ -29,7 +29,7 @@ exports.handlePostback = (sender_psid, received_postback) => { ...@@ -29,7 +29,7 @@ exports.handlePostback = (sender_psid, received_postback) => {
29 }else if(payload.substr(0,14) ==='CHOICE_BY_HELP'){ 29 }else if(payload.substr(0,14) ==='CHOICE_BY_HELP'){
30 handlePostback_help(sender_psid, recieved_postback); 30 handlePostback_help(sender_psid, recieved_postback);
31 }else if(payload.substr(0,8) === 'MoreRate'){ 31 }else if(payload.substr(0,8) === 'MoreRate'){
32 - response = template.moreRageTemplate(received_postback); 32 + response = template.moreRateTemplate(received_postback);
33 callSendAPI(sender_psid, response); 33 callSendAPI(sender_psid, response);
34 } 34 }
35 } 35 }
......
File mode changed
...@@ -73,6 +73,11 @@ ...@@ -73,6 +73,11 @@
73 "tweetnacl": "^0.14.3" 73 "tweetnacl": "^0.14.3"
74 } 74 }
75 }, 75 },
76 + "bignumber.js": {
77 + "version": "7.2.1",
78 + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-7.2.1.tgz",
79 + "integrity": "sha512-S4XzBk5sMB+Rcb/LNcpzXr57VRTxgAvaAEDAl1AwRx27j00hT84O6OkteE7u8UB3NuaaygCRrEpqox4uDOrbdQ=="
80 + },
76 "bluebird": { 81 "bluebird": {
77 "version": "3.5.1", 82 "version": "3.5.1",
78 "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", 83 "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
...@@ -369,6 +374,11 @@ ...@@ -369,6 +374,11 @@
369 "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", 374 "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
370 "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" 375 "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo="
371 }, 376 },
377 + "isarray": {
378 + "version": "1.0.0",
379 + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
380 + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
381 + },
372 "isstream": { 382 "isstream": {
373 "version": "0.1.2", 383 "version": "0.1.2",
374 "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", 384 "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
...@@ -538,6 +548,17 @@ ...@@ -538,6 +548,17 @@
538 "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 548 "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
539 "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 549 "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
540 }, 550 },
551 + "mysql": {
552 + "version": "2.17.1",
553 + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.17.1.tgz",
554 + "integrity": "sha512-7vMqHQ673SAk5C8fOzTG2LpPcf3bNt0oL3sFpxPEEFp1mdlDcrLK0On7z8ZYKaaHrHwNcQ/MTUz7/oobZ2OyyA==",
555 + "requires": {
556 + "bignumber.js": "7.2.1",
557 + "readable-stream": "2.3.6",
558 + "safe-buffer": "5.1.2",
559 + "sqlstring": "2.3.1"
560 + }
561 + },
541 "negotiator": { 562 "negotiator": {
542 "version": "0.6.2", 563 "version": "0.6.2",
543 "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", 564 "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
...@@ -571,6 +592,11 @@ ...@@ -571,6 +592,11 @@
571 "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", 592 "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
572 "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" 593 "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
573 }, 594 },
595 + "process-nextick-args": {
596 + "version": "2.0.0",
597 + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz",
598 + "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw=="
599 + },
574 "proxy-addr": { 600 "proxy-addr": {
575 "version": "2.0.5", 601 "version": "2.0.5",
576 "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.5.tgz", 602 "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.5.tgz",
...@@ -611,6 +637,20 @@ ...@@ -611,6 +637,20 @@
611 "unpipe": "1.0.0" 637 "unpipe": "1.0.0"
612 } 638 }
613 }, 639 },
640 + "readable-stream": {
641 + "version": "2.3.6",
642 + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
643 + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
644 + "requires": {
645 + "core-util-is": "~1.0.0",
646 + "inherits": "~2.0.3",
647 + "isarray": "~1.0.0",
648 + "process-nextick-args": "~2.0.0",
649 + "safe-buffer": "~5.1.1",
650 + "string_decoder": "~1.1.1",
651 + "util-deprecate": "~1.0.1"
652 + }
653 + },
614 "regexp-clone": { 654 "regexp-clone": {
615 "version": "0.0.1", 655 "version": "0.0.1",
616 "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-0.0.1.tgz", 656 "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-0.0.1.tgz",
...@@ -750,6 +790,11 @@ ...@@ -750,6 +790,11 @@
750 "memory-pager": "^1.0.2" 790 "memory-pager": "^1.0.2"
751 } 791 }
752 }, 792 },
793 + "sqlstring": {
794 + "version": "2.3.1",
795 + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
796 + "integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A="
797 + },
753 "sshpk": { 798 "sshpk": {
754 "version": "1.16.1", 799 "version": "1.16.1",
755 "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", 800 "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz",
...@@ -771,6 +816,14 @@ ...@@ -771,6 +816,14 @@
771 "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", 816 "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
772 "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" 817 "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
773 }, 818 },
819 + "string_decoder": {
820 + "version": "1.1.1",
821 + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
822 + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
823 + "requires": {
824 + "safe-buffer": "~5.1.0"
825 + }
826 + },
774 "toidentifier": { 827 "toidentifier": {
775 "version": "1.0.0", 828 "version": "1.0.0",
776 "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", 829 "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
...@@ -827,6 +880,11 @@ ...@@ -827,6 +880,11 @@
827 "punycode": "^2.1.0" 880 "punycode": "^2.1.0"
828 } 881 }
829 }, 882 },
883 + "util-deprecate": {
884 + "version": "1.0.2",
885 + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
886 + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
887 + },
830 "utils-merge": { 888 "utils-merge": {
831 "version": "1.0.1", 889 "version": "1.0.1",
832 "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", 890 "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
......
1 -const db = require('./db.js'); 1 +const db = require('./db');
2 2
3 let greetingText = "안녕하세요! 컴공/소융 강의평가 봇이에요. 아래에 버튼 3개를 눌러주세요."; // 인사말 3 let greetingText = "안녕하세요! 컴공/소융 강의평가 봇이에요. 아래에 버튼 3개를 눌러주세요."; // 인사말
4 let greetingTitle = ["교수명으로 검색", "강의명으로 검색", "HELP"]; 4 let greetingTitle = ["교수명으로 검색", "강의명으로 검색", "HELP"];
...@@ -54,30 +54,220 @@ exports.getProfNameTemplate = (received) => { ...@@ -54,30 +54,220 @@ exports.getProfNameTemplate = (received) => {
54 } 54 }
55 55
56 exports.getLectNameTemplate = (received) => { 56 exports.getLectNameTemplate = (received) => {
57 -//메세지 57 + return {
58 - return{text: "성공"} 58 + "attachment":{
59 - 59 + "type":"template",
60 - //텍스트 메세지를 필드로 디비에서 수업/교수명 찾아서 목록 제공 60 + "payload":{
61 - 61 + "template_type":"button",
62 + "text": "강의명을 입력하세요.",
63 + "buttons":[
64 + {
65 + "type":"postback",
66 + "title":"처음으로",
67 + "payload":'Greeting'
68 + }
69 + ]
70 + }
71 + }
72 + }
62 } 73 }
63 74
64 //stat_1 75 //stat_1
65 exports.choiceLectProfTemplate = (received) => { 76 exports.choiceLectProfTemplate = (received) => {
66 -//버튼 77 + let message = received.text;
78 + db.query('SELECT * FROM reviews WHERE lecturename LIKE ? OR proname LIKE ?', [message,message], function(error,results){
79 + if (err){
80 + console.log(err);
81 + res.render('error');
82 + }
83 + if(results==0){
84 + return {text: "검색 결과가 없습니다. 검색어를 다시 입력해주세요."}
85 + }else if(results.length<=2){
86 + return LectProfList(results,0,results.length,false,'0',message);
87 + }else if(results.length>2){
88 + return LectProfList(results,0,2,true,'1',message);
89 + }
90 + });
91 +}
92 +
93 +const LectProfList = (results,start,length,ismore,morecount,message) => {
94 + if(length==1){
95 + return {
96 + "attachment":{
97 + "type":"template",
98 + "payload":{
99 + "template_type":"button",
100 + "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요.",
101 + "buttons":[
102 + {
103 + "type":"postback",
104 + "title":results[start].lecturename+results[start].proname,
105 + "payload":'CHOICE_BY_PROFstat_3'+results[start].idreviews
106 + }
107 + ]
108 + }
109 + }
110 + }
111 + }else if((length == 2)&&(!ismore)){
112 + return {
113 + "attachment":{
114 + "type":"template",
115 + "payload":{
116 + "template_type":"button",
117 + "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요.",
118 + "buttons":[
119 + {
120 + "type":"postback",
121 + "title":results[start].lecturename+results[start].proname,
122 + "payload":'CHOICE_BY_PROFstat_3'+results[start].idreviews
123 + },
124 + {
125 + "type":"postback",
126 + "title":results[start+1].lecturename+results[start+1].proname,
127 + "payload":'CHOICE_BY_PROFstat_3'+results[start+1].idreviews
128 + }
129 + ]
130 + }
131 + }
132 + }
133 + }else if((length == 2)&&(ismore)){
134 + return {
135 + "attachment":{
136 + "type":"template",
137 + "payload":{
138 + "template_type":"button",
139 + "text": "검색 결과입니다. 평가를 조회할 강의를 선택하세요. 원하는 강의가 없다면 [더보기]를 눌러주세요.",
140 + "buttons":[
141 + {
142 + "type":"postback",
143 + "title":results[start].lecturename+results[start].proname,
144 + "payload":'CHOICE_BY_PROFstat_3'+results[start].idreviews
145 + },
146 + {
147 + "type":"postback",
148 + "title":results[start+1].lecturename+results[start+1].proname,
149 + "payload":'CHOICE_BY_PROFstat_3'+results[start+1].idreviews
150 + },
151 + {
152 + "type":"postback",
153 + "title":"더보기",
154 + "payload":'CHOICE_BY_PROFstat_2'+message+'/'+morecount
155 + }
156 + ]
157 + }
158 + }
159 + }
160 + }
161 +
67 } 162 }
68 163
69 //stat_2 164 //stat_2
70 exports.moreProfTemplate = (received) => { 165 exports.moreProfTemplate = (received) => {
71 -//버튼 166 + let ms=received.payload.substr(20).split('/');
167 + let more=1*(ms[0]); //to Number
168 + let message=ms[1];
169 +
170 + db.query('SELECT * FROM reviews WHERE lecturename LIKE ? OR proname LIKE ?', [message,message], function(error,results){
171 + if (err){
172 + console.log(err);
173 + res.render('error');
174 + }
175 + if((results.length-2*(more+1))<=0){
176 + return LectProfList(results,2*more,results.length-2*n,false,'0',message);
177 + }else{
178 + return LectProfList(results,2*more,2,true,""+(more+1),message);
179 + }
180 + });
72 } 181 }
73 182
74 exports.moreLectTemplate= (received) => { 183 exports.moreLectTemplate= (received) => {
75 -//버튼 184 +let ms=received.payload.substr(20).split('/');
185 + let more=1*(ms[0]);
186 + let message=ms[1];
187 +
188 + db.query('SELECT * FROM reviews WHERE lecturename LIKE ? OR proname LIKE ?', [message,message], function(error,results){
189 + if (err){
190 + console.log(err);
191 + res.render('error');
192 + }
193 + if((results.length-2*(more+1))<=0){
194 + return LectProfList(results,2*more,results.length-2*n,false,'0',message);
195 + }else{
196 + return LectProfList(results,2*more,2,true,""+(more+1),message);
197 + }
198 + });
76 } 199 }
77 200
78 //stat_3 201 //stat_3
79 exports.rateTemplate= (received) => { 202 exports.rateTemplate= (received) => {
80 -//버튼 203 + let id = received.payload.substr(20);
204 +
205 +db.query('SELECT * FROM reviews WHERE idreviews LIKE ?', [id], function(error,results){
206 + if (err){
207 + console.log(err);
208 + res.render('error');
209 + }
210 +
211 + let proname=results[0].proname;
212 + let lecname=results[0].lecname;
213 + let reviews=results[0].review.split(',');
214 + let review=reviews[0];
215 + let rate=results[0].avg_rate;
216 + let botsay=[];
217 + let randNum=0;
218 + if(rate != 0){
219 + Math.floor((Math.random() * (3-0+1))+0);
220 + }
221 +
222 + if(rate==0){
223 + botsay=["신설 강좌거나 아직 강의평이 없어요!"];
224 + }else if((rate>1)&&(rate<2.5)){
225 + botsay=["음.. 말을 아낄게요.", "다시 생각해보시는게..","이전 단계로 돌아갈까요?","으응?"];
226 + }else if((rate>=2.5)&&(rate<4.0)){
227 + botsay=["무난무난","그럭저럭한 것 같아요.","안 들어서 나쁠거 없어요.","평범한 수업이네요"];
228 + }else{
229 + botsay=["무조건 들어요!","갓갓 교수님의 갓갓 수업","안 들으면 후회해요ㅠㅠ", "수강신청 1순위!"];
230 + }
231 +
232 + return {
233 + "attachment":{
234 + "type":"template",
235 + "payload":{
236 + "template_type":"button",
237 + "text": proname+" 교수님의 강의 ["+lecname+"]의 강의평입니다.\n **평균 평점: "+rate+"\n **봇의 한줄평: "+botsay[randNum]+"\n **학우의 한줄평: "+reivew,
238 + "buttons":[
239 + {
240 + "type":"postback",
241 + "title":"자세한 강의평 보기",
242 + "payload":'MoreRate'+id
243 + }
244 + ]
245 + }
246 + }
247 + }
248 +
249 + });
250 +}
251 +
252 +exports.moreRateTemplate = (received) => {
253 + let id = received.payload.substr(8);
254 + let total_review="";
255 +
256 + db.query('SELECT * FROM reviews WHERE idreviews LIKE ?', [id], function(error,results){
257 + if (err){
258 + console.log(err);
259 + res.render('error');
260 + }
261 +
262 + let reviews=results[0].review.split(',');
263 + for(var review in reviews){
264 + total_review+=review+"\n";
265 + }
266 +
267 + return {text:total_review}
268 + });
269 + //payload:MoreRate(강의식별번호)
270 + //포스트백 핸들 moreRateTemplate에서 해당 강의의 자세한 평 출력
81 } 271 }
82 272
83 //payload === 'HELP' 273 //payload === 'HELP'
...@@ -118,7 +308,3 @@ return { ...@@ -118,7 +308,3 @@ return {
118 } 308 }
119 } 309 }
120 } 310 }
121 -
122 -exports.moreRageTemplate = (received) => {
123 -
124 -}
...\ No newline at end of file ...\ No newline at end of file
......