Showing
1 changed file
with
97 additions
and
14 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,30 +28,114 @@ app.post('/hook', function (req, res) { | ... | @@ -29,30 +28,114 @@ 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 | + | ||
47 | + button = { | ||
48 | + "type" : "flex", | ||
49 | + "altText" : "test FLEX", | ||
50 | + "contents" : { | ||
51 | + "type": "bubble", | ||
52 | + "body": { | ||
53 | + "type": "box", | ||
54 | + "layout" : "vertical", | ||
55 | + "contents" : [ | ||
56 | + { | ||
57 | + "type": "button", | ||
58 | + "action": { | ||
59 | + "type":"postback", | ||
60 | + "label":"회사명으로 검색하기", | ||
61 | + "data": eventObj.message.text + "|||0|||" + "companyName" | ||
62 | + }, | ||
63 | + "style": "primary", | ||
64 | + "color": "#ff9a9e" | ||
65 | + }, | ||
66 | + { | ||
67 | + "type": "button", | ||
68 | + "action": { | ||
69 | + "type":"postback", | ||
70 | + "label":"태그로 검색하기", | ||
71 | + "data": eventObj.message.text + "|||0|||" + "tag" | ||
72 | + }, | ||
73 | + "style": "primary", | ||
74 | + "color": "#fbc2eb" | ||
75 | + }, | ||
76 | + { | ||
77 | + "type": "button", | ||
78 | + "action": { | ||
79 | + "type":"postback", | ||
80 | + "label":"제목으로 검색하기", | ||
81 | + "data": eventObj.message.text + "|||0|||" + "title" | ||
82 | + }, | ||
83 | + "style": "primary", | ||
84 | + "color": "#8fd3f4" | ||
85 | + } | ||
86 | + ] | ||
87 | + } | ||
88 | + } | ||
89 | + } | ||
90 | + result = { | ||
91 | + url: TARGET_URL, | ||
92 | + headers: { | ||
93 | + 'Authorization': `Bearer ${TOKEN}` | ||
94 | + }, | ||
95 | + json: { | ||
96 | + "replyToken":eventObj.replyToken, | ||
97 | + "messages": [button] | ||
98 | + } | ||
99 | + } | ||
100 | + request.post(result ,(error, response, body) => { | ||
101 | + console.log(body) | ||
102 | + }); | ||
103 | + | ||
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); | ||
46 | } | 130 | } |
47 | else{ | 131 | else{ |
48 | - start = parseInt(eventObj.postback.data.split(" ")[1]) | 132 | + start = parseInt(eventObj.postback.data.split("|||")[1]) |
49 | finish = start + 4 | 133 | finish = start + 4 |
50 | if (finish >= messageData.length){ | 134 | if (finish >= messageData.length){ |
51 | finish = messageData.length | 135 | finish = messageData.length |
52 | flag = false | 136 | flag = false |
53 | } | 137 | } |
54 | - } | 138 | + |
55 | - data = `kakao ${finish}` | ||
56 | button = { | 139 | button = { |
57 | "type" : "flex", | 140 | "type" : "flex", |
58 | "altText" : "test FLEX", | 141 | "altText" : "test FLEX", |
... | @@ -67,16 +150,15 @@ app.post('/hook', function (req, res) { | ... | @@ -67,16 +150,15 @@ app.post('/hook', function (req, res) { |
67 | "action": { | 150 | "action": { |
68 | "type":"postback", | 151 | "type":"postback", |
69 | "label":"다음 보기", | 152 | "label":"다음 보기", |
70 | - "data": data | 153 | + "data": postbackData[0] + " " + finish + " " + postbackData[2] |
71 | }, | 154 | }, |
72 | "style": "primary", | 155 | "style": "primary", |
73 | - "color": "#0000ff" | 156 | + "color": "#fbc2eb" |
74 | } | 157 | } |
75 | ] | 158 | ] |
76 | } | 159 | } |
77 | } | 160 | } |
78 | } | 161 | } |
79 | - console.log(start, finish, data) | ||
80 | for(start ; start < finish; start++){ | 162 | for(start ; start < finish; start++){ |
81 | string = "제목 : " + messageData[start].title +"\n" + "회사명 : " + messageData[start].companyName + "\n" + "tags : " + messageData[start].tags.toString() +"\n" + "링크 : " + messageData[start].url + "\n" | 163 | string = "제목 : " + messageData[start].title +"\n" + "회사명 : " + messageData[start].companyName + "\n" + "tags : " + messageData[start].tags.toString() +"\n" + "링크 : " + messageData[start].url + "\n" |
82 | messageResult.push({ | 164 | messageResult.push({ |
... | @@ -109,12 +191,13 @@ app.post('/hook', function (req, res) { | ... | @@ -109,12 +191,13 @@ app.post('/hook', function (req, res) { |
109 | } | 191 | } |
110 | } | 192 | } |
111 | } | 193 | } |
112 | - | ||
113 | request.post(result ,(error, response, body) => { | 194 | request.post(result ,(error, response, body) => { |
114 | console.log(body) | 195 | console.log(body) |
115 | }); | 196 | }); |
116 | 197 | ||
117 | res.sendStatus(200); | 198 | res.sendStatus(200); |
199 | + } | ||
200 | + } | ||
118 | }); | 201 | }); |
119 | try { | 202 | try { |
120 | const option = { | 203 | const option = { | ... | ... |
-
Please register or login to post a comment