김대선

회사명, 태그, 제목으로 검색 챗봇에 적용

Showing 1 changed file with 148 additions and 64 deletions
...@@ -15,7 +15,7 @@ const data = require('./functions/dataFunctions') ...@@ -15,7 +15,7 @@ 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 -const saveData = schedule.scheduleJob('00 0 00 * * *', data.save) 18 +const saveData = schedule.scheduleJob('55 51 19 * * *', data.save)
19 19
20 var app = express(); 20 var app = express();
21 app.use(bodyParser.json()); 21 app.use(bodyParser.json());
...@@ -28,75 +28,65 @@ app.post('/hook', function (req, res) { ...@@ -28,75 +28,65 @@ app.post('/hook', function (req, res) {
28 console.log('======================', new Date() ,'======================'); 28 console.log('======================', new Date() ,'======================');
29 console.log('[queryString]', req.query) 29 console.log('[queryString]', req.query)
30 console.log('[request]', req.body); 30 console.log('[request]', req.body);
31 - console.log('[request source] ', eventObj.source, eventObj2.source); 31 + console.log('[request source] ', eventObj.source);
32 - console.log('[request message]', eventObj.message, eventObj2.message); 32 + console.log('[request message]', eventObj.message);
33 - console.log('[request postback]', eventObj.postback, eventObj2.postback); 33 + console.log('[request postback]', eventObj.postback);
34 - const messageData = find.byCompanyName('naver') 34 +
35 + let messageData
35 const messageResult = [] 36 const messageResult = []
36 let string 37 let string
37 let start 38 let start
38 let finish 39 let finish
39 let data 40 let data
40 - let button
41 let flag = true 41 let flag = true
42 - if (!eventObj.postback){ 42 + let button
43 + if(eventObj.type == "message"){
43 start = 0; 44 start = 0;
44 finish = start + 4 45 finish = start + 4
45 - } 46 +
46 - else{ 47 + button = {
47 - start = parseInt(eventObj.postback.data.split(" ")[1]) 48 + "type" : "flex",
48 - finish = start + 4 49 + "altText" : "test FLEX",
49 - if (finish >= messageData.length){ 50 + "contents" : {
50 - finish = messageData.length 51 + "type": "bubble",
51 - flag = false 52 + "body": {
52 - } 53 + "type": "box",
53 - } 54 + "layout" : "vertical",
54 - data = `kakao ${finish}` 55 + "contents" : [
55 - button = { 56 + {
56 - "type" : "flex", 57 + "type": "button",
57 - "altText" : "test FLEX", 58 + "action": {
58 - "contents" : { 59 + "type":"postback",
59 - "type": "bubble", 60 + "label":"회사명으로 검색하기",
60 - "body": { 61 + "data": eventObj.message.text + "|||0|||" + "companyName"
61 - "type": "box", 62 + },
62 - "layout" : "vertical", 63 + "style": "primary",
63 - "contents" : [ 64 + "color": "#ff9a9e"
64 - {
65 - "type": "button",
66 - "action": {
67 - "type":"postback",
68 - "label":"다음 보기",
69 - "data": data
70 }, 65 },
71 - "style": "primary", 66 + {
72 - "color": "#0000ff" 67 + "type": "button",
73 - } 68 + "action": {
74 - ] 69 + "type":"postback",
75 - } 70 + "label":"태그로 검색하기",
76 - } 71 + "data": eventObj.message.text + "|||0|||" + "tag"
77 - } 72 + },
78 - console.log(start, finish, data) 73 + "style": "primary",
79 - for(start ; start < finish; start++){ 74 + "color": "#fbc2eb"
80 - string = "제목 : " + messageData[start].title +"\n" + "회사명 : " + messageData[start].companyName + "\n" + "tags : " + messageData[start].tags.toString() +"\n" + "링크 : " + messageData[start].url + "\n" 75 + },
81 - messageResult.push({ 76 + {
82 - "type" : "text", 77 + "type": "button",
83 - "text" : string 78 + "action": {
84 - }) 79 + "type":"postback",
85 - } 80 + "label":"제목으로 검색하기",
86 - if(flag){ 81 + "data": eventObj.message.text + "|||0|||" + "title"
87 - messageResult.push(button) 82 + },
88 - result = { 83 + "style": "primary",
89 - url: TARGET_URL, 84 + "color": "#8fd3f4"
90 - headers: { 85 + }
91 - 'Authorization': `Bearer ${TOKEN}` 86 + ]
92 - }, 87 + }
93 - json: {
94 - "replyToken":eventObj.replyToken,
95 - "messages": messageResult
96 } 88 }
97 } 89 }
98 - }
99 - else{
100 result = { 90 result = {
101 url: TARGET_URL, 91 url: TARGET_URL,
102 headers: { 92 headers: {
...@@ -104,16 +94,110 @@ app.post('/hook', function (req, res) { ...@@ -104,16 +94,110 @@ app.post('/hook', function (req, res) {
104 }, 94 },
105 json: { 95 json: {
106 "replyToken":eventObj.replyToken, 96 "replyToken":eventObj.replyToken,
107 - "messages": messageResult 97 + "messages": [button]
108 } 98 }
109 } 99 }
110 - } 100 + request.post(result ,(error, response, body) => {
111 -
112 - request.post(result ,(error, response, body) => {
113 console.log(body) 101 console.log(body)
114 }); 102 });
115 103
116 - 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 + }
117 }); 201 });
118 try { 202 try {
119 const option = { 203 const option = {
......