김대선

merge feature/chatbot

Showing 1 changed file with 147 additions and 64 deletions
...@@ -15,7 +15,6 @@ const data = require('./functions/dataFunctions') ...@@ -15,7 +15,6 @@ const data = require('./functions/dataFunctions')
15 const find = require('./functions/findFunction') 15 const find = require('./functions/findFunction')
16 16
17 // 0초 0분 0시 아무날 아무달 아무년 17 // 0초 0분 0시 아무날 아무달 아무년
18 -
19 const saveData = schedule.scheduleJob('00 0 00 * * *', data.save) 18 const saveData = schedule.scheduleJob('00 0 00 * * *', data.save)
20 19
21 var app = express(); 20 var app = express();
...@@ -29,75 +28,65 @@ app.post('/hook', function (req, res) { ...@@ -29,75 +28,65 @@ app.post('/hook', function (req, res) {
29 console.log('======================', new Date() ,'======================'); 28 console.log('======================', new Date() ,'======================');
30 console.log('[queryString]', req.query) 29 console.log('[queryString]', req.query)
31 console.log('[request]', req.body); 30 console.log('[request]', req.body);
32 - console.log('[request source] ', eventObj.source, eventObj2.source); 31 + console.log('[request source] ', eventObj.source);
33 - console.log('[request message]', eventObj.message, eventObj2.message); 32 + console.log('[request message]', eventObj.message);
34 - console.log('[request postback]', eventObj.postback, eventObj2.postback); 33 + console.log('[request postback]', eventObj.postback);
35 - const messageData = find.byCompanyName('naver') 34 +
35 + let messageData
36 const messageResult = [] 36 const messageResult = []
37 let string 37 let string
38 let start 38 let start
39 let finish 39 let finish
40 let data 40 let data
41 - let button
42 let flag = true 41 let flag = true
43 - if (!eventObj.postback){ 42 + let button
43 + if(eventObj.type == "message"){
44 start = 0; 44 start = 0;
45 finish = start + 4 45 finish = start + 4
46 - } 46 +
47 - else{ 47 + button = {
48 - start = parseInt(eventObj.postback.data.split(" ")[1]) 48 + "type" : "flex",
49 - finish = start + 4 49 + "altText" : "test FLEX",
50 - if (finish >= messageData.length){ 50 + "contents" : {
51 - finish = messageData.length 51 + "type": "bubble",
52 - flag = false 52 + "body": {
53 - } 53 + "type": "box",
54 - } 54 + "layout" : "vertical",
55 - data = `kakao ${finish}` 55 + "contents" : [
56 - button = { 56 + {
57 - "type" : "flex", 57 + "type": "button",
58 - "altText" : "test FLEX", 58 + "action": {
59 - "contents" : { 59 + "type":"postback",
60 - "type": "bubble", 60 + "label":"회사명으로 검색하기",
61 - "body": { 61 + "data": eventObj.message.text + "|||0|||" + "companyName"
62 - "type": "box", 62 + },
63 - "layout" : "vertical", 63 + "style": "primary",
64 - "contents" : [ 64 + "color": "#ff9a9e"
65 - {
66 - "type": "button",
67 - "action": {
68 - "type":"postback",
69 - "label":"다음 보기",
70 - "data": data
71 }, 65 },
72 - "style": "primary", 66 + {
73 - "color": "#0000ff" 67 + "type": "button",
74 - } 68 + "action": {
75 - ] 69 + "type":"postback",
76 - } 70 + "label":"태그로 검색하기",
77 - } 71 + "data": eventObj.message.text + "|||0|||" + "tag"
78 - } 72 + },
79 - console.log(start, finish, data) 73 + "style": "primary",
80 - for(start ; start < finish; start++){ 74 + "color": "#fbc2eb"
81 - string = "제목 : " + messageData[start].title +"\n" + "회사명 : " + messageData[start].companyName + "\n" + "tags : " + messageData[start].tags.toString() +"\n" + "링크 : " + messageData[start].url + "\n" 75 + },
82 - messageResult.push({ 76 + {
83 - "type" : "text", 77 + "type": "button",
84 - "text" : string 78 + "action": {
85 - }) 79 + "type":"postback",
86 - } 80 + "label":"제목으로 검색하기",
87 - if(flag){ 81 + "data": eventObj.message.text + "|||0|||" + "title"
88 - messageResult.push(button) 82 + },
89 - result = { 83 + "style": "primary",
90 - url: TARGET_URL, 84 + "color": "#8fd3f4"
91 - headers: { 85 + }
92 - 'Authorization': `Bearer ${TOKEN}` 86 + ]
93 - }, 87 + }
94 - json: {
95 - "replyToken":eventObj.replyToken,
96 - "messages": messageResult
97 } 88 }
98 } 89 }
99 - }
100 - else{
101 result = { 90 result = {
102 url: TARGET_URL, 91 url: TARGET_URL,
103 headers: { 92 headers: {
...@@ -105,16 +94,110 @@ app.post('/hook', function (req, res) { ...@@ -105,16 +94,110 @@ app.post('/hook', function (req, res) {
105 }, 94 },
106 json: { 95 json: {
107 "replyToken":eventObj.replyToken, 96 "replyToken":eventObj.replyToken,
108 - "messages": messageResult 97 + "messages": [button]
109 } 98 }
110 } 99 }
111 - } 100 + request.post(result ,(error, response, body) => {
112 -
113 - request.post(result ,(error, response, body) => {
114 console.log(body) 101 console.log(body)
115 }); 102 });
116 103
117 - res.sendStatus(200); 104 + res.sendStatus(200);
105 + }
106 + else if(eventObj.type = "postback"){
107 + postbackData = eventObj.postback.data.split("|||")
108 + if(postbackData[2] == "companyName") messageData = find.byCompanyName(postbackData[0])
109 + else if(postbackData[2] == "tag") messageData = find.byTags(postbackData[0])
110 + else if(postbackData[2] == "title") messageData = find.byTitle(postbackData[0])
111 + if(messageData.length == 0){
112 + result = {
113 + url: TARGET_URL,
114 + headers: {
115 + 'Authorization': `Bearer ${TOKEN}`
116 + },
117 + json: {
118 + "replyToken":eventObj.replyToken,
119 + "messages": [{
120 + "type" : "text",
121 + "text" : "검색결과가 없습니다!"
122 + }]
123 + }
124 + }
125 + request.post(result ,(error, response, body) => {
126 + console.log(body)
127 + });
128 +
129 + res.sendStatus(200);
130 + }
131 + else{
132 + start = parseInt(eventObj.postback.data.split("|||")[1])
133 + finish = start + 4
134 + if (finish >= messageData.length){
135 + finish = messageData.length
136 + flag = false
137 + }
138 +
139 + button = {
140 + "type" : "flex",
141 + "altText" : "test FLEX",
142 + "contents" : {
143 + "type": "bubble",
144 + "body": {
145 + "type": "box",
146 + "layout" : "vertical",
147 + "contents" : [
148 + {
149 + "type": "button",
150 + "action": {
151 + "type":"postback",
152 + "label":"다음 보기",
153 + "data": postbackData[0] + " " + finish + " " + postbackData[2]
154 + },
155 + "style": "primary",
156 + "color": "#fbc2eb"
157 + }
158 + ]
159 + }
160 + }
161 + }
162 + for(start ; start < finish; start++){
163 + string = "제목 : " + messageData[start].title +"\n" + "회사명 : " + messageData[start].companyName + "\n" + "tags : " + messageData[start].tags.toString() +"\n" + "링크 : " + messageData[start].url + "\n"
164 + messageResult.push({
165 + "type" : "text",
166 + "text" : string
167 + })
168 + }
169 + if(flag){
170 + messageResult.push(button)
171 + result = {
172 + url: TARGET_URL,
173 + headers: {
174 + 'Authorization': `Bearer ${TOKEN}`
175 + },
176 + json: {
177 + "replyToken":eventObj.replyToken,
178 + "messages": messageResult
179 + }
180 + }
181 + }
182 + else{
183 + result = {
184 + url: TARGET_URL,
185 + headers: {
186 + 'Authorization': `Bearer ${TOKEN}`
187 + },
188 + json: {
189 + "replyToken":eventObj.replyToken,
190 + "messages": messageResult
191 + }
192 + }
193 + }
194 + request.post(result ,(error, response, body) => {
195 + console.log(body)
196 + });
197 +
198 + res.sendStatus(200);
199 + }
200 + }
118 }); 201 });
119 try { 202 try {
120 const option = { 203 const option = {
......