Showing
1 changed file
with
36 additions
and
54 deletions
... | @@ -19,6 +19,36 @@ app.post('/hook', function (req, res) { | ... | @@ -19,6 +19,36 @@ app.post('/hook', function (req, res) { |
19 | var source = eventObj.source; | 19 | var source = eventObj.source; |
20 | var message = eventObj.message; | 20 | var message = eventObj.message; |
21 | 21 | ||
22 | + function output_message(results, list) { | ||
23 | + var num = list[Math.floor(Math.random() * list.length)]; | ||
24 | + request.post( | ||
25 | + { | ||
26 | + url: TARGET_URL, | ||
27 | + headers: { | ||
28 | + 'Authorization': `Bearer ${TOKEN}` | ||
29 | + }, | ||
30 | + json: { | ||
31 | + "replyToken": eventObj.replyToken, | ||
32 | + "messages": [ | ||
33 | + { | ||
34 | + "type": "text", | ||
35 | + "text": `메뉴는 "${results[[num]].menu}" 입니다.` | ||
36 | + }, | ||
37 | + { | ||
38 | + "type": "text", | ||
39 | + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.` | ||
40 | + }, | ||
41 | + { | ||
42 | + "type": "text", | ||
43 | + "text": `레시피\n\n${results[[num]].recipe}` | ||
44 | + } | ||
45 | + ] | ||
46 | + } | ||
47 | + }, (error, response, body) => { | ||
48 | + console.log(body) | ||
49 | + }); | ||
50 | + } | ||
51 | + | ||
22 | // request log | 52 | // request log |
23 | console.log('======================', new Date(), '======================'); | 53 | console.log('======================', new Date(), '======================'); |
24 | // mwsql | 54 | // mwsql |
... | @@ -56,63 +86,13 @@ app.post('/hook', function (req, res) { | ... | @@ -56,63 +86,13 @@ app.post('/hook', function (req, res) { |
56 | arr2.push(i); | 86 | arr2.push(i); |
57 | } | 87 | } |
58 | } | 88 | } |
89 | + | ||
59 | if (arr1.length != 0) { | 90 | if (arr1.length != 0) { |
60 | - var num = arr1[Math.floor(Math.random() * arr1.length)]; | 91 | + output_message(results, arr1); |
61 | - request.post( | ||
62 | - { | ||
63 | - url: TARGET_URL, | ||
64 | - headers: { | ||
65 | - 'Authorization': `Bearer ${TOKEN}` | ||
66 | - }, | ||
67 | - json: { | ||
68 | - "replyToken": eventObj.replyToken, | ||
69 | - "messages": [ | ||
70 | - { | ||
71 | - "type": "text", | ||
72 | - "text": `메뉴는 "${results[[num]].menu}" 입니다.` | ||
73 | - }, | ||
74 | - { | ||
75 | - "type": "text", | ||
76 | - "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.` | ||
77 | - }, | ||
78 | - { | ||
79 | - "type": "text", | ||
80 | - "text": `레시피\n\n${results[[num]].recipe}` | ||
81 | - } | ||
82 | - ] | ||
83 | - } | ||
84 | - }, (error, response, body) => { | ||
85 | - console.log(body) | ||
86 | - }); | ||
87 | } | 92 | } |
93 | + | ||
88 | else if (arr2.length != 0) { | 94 | else if (arr2.length != 0) { |
89 | - var num = arr2[Math.floor(Math.random() * arr2.length)]; | 95 | + output_message(results, arr2); |
90 | - request.post( | ||
91 | - { | ||
92 | - url: TARGET_URL, | ||
93 | - headers: { | ||
94 | - 'Authorization': `Bearer ${TOKEN}` | ||
95 | - }, | ||
96 | - json: { | ||
97 | - "replyToken": eventObj.replyToken, | ||
98 | - "messages": [ | ||
99 | - { | ||
100 | - "type": "text", | ||
101 | - "text": `메뉴는 "${results[[num]].menu}" 입니다.` | ||
102 | - }, | ||
103 | - { | ||
104 | - "type": "text", | ||
105 | - "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.` | ||
106 | - }, | ||
107 | - { | ||
108 | - "type": "text", | ||
109 | - "text": `레시피\n\n${results[[num]].recipe}` | ||
110 | - } | ||
111 | - ] | ||
112 | - } | ||
113 | - }, (error, response, body) => { | ||
114 | - console.log(body) | ||
115 | - }); | ||
116 | } | 96 | } |
117 | else { | 97 | else { |
118 | request.post( | 98 | request.post( |
... | @@ -134,9 +114,11 @@ app.post('/hook', function (req, res) { | ... | @@ -134,9 +114,11 @@ app.post('/hook', function (req, res) { |
134 | console.log(body) | 114 | console.log(body) |
135 | }); | 115 | }); |
136 | } | 116 | } |
117 | + | ||
137 | }); | 118 | }); |
138 | 119 | ||
139 | db.end(); | 120 | db.end(); |
121 | + | ||
140 | res.sendStatus(200); | 122 | res.sendStatus(200); |
141 | }); | 123 | }); |
142 | 124 | ... | ... |
-
Please register or login to post a comment