김대선

merge feature/chatbot

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