Showing
1 changed file
with
54 additions
and
27 deletions
1 | var express = require('express'); | 1 | var express = require('express'); |
2 | const request = require('request'); | 2 | const request = require('request'); |
3 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | 3 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' |
4 | -const TOKEN = 'zVAczqoJ+L9oykqhEj7HoP7f6Nyb+R3T1TntHXQhYihI+KIxH4SZDGpKu0jKsMXMHKBVXpmbybA+oaV8u/dfLppKe3NHXU9AdBVypy9NgfWiFPLlcwm3GdkVPAZGoS4nvOCXWDKUb+ixPKWjlbnChAdB04t89/1O/w1cDnyilFU=' | 4 | +const TOKEN = '' |
5 | const fs = require('fs'); | 5 | const fs = require('fs'); |
6 | const path = require('path'); | 6 | const path = require('path'); |
7 | const HTTPS = require('https'); | 7 | const HTTPS = require('https'); |
... | @@ -21,13 +21,12 @@ app.post('/hook', function (req, res) { | ... | @@ -21,13 +21,12 @@ app.post('/hook', function (req, res) { |
21 | 21 | ||
22 | // request log | 22 | // request log |
23 | console.log('======================', new Date(), '======================'); | 23 | console.log('======================', new Date(), '======================'); |
24 | - | ||
25 | // mwsql | 24 | // mwsql |
26 | var mysql = require('mysql'); | 25 | var mysql = require('mysql'); |
27 | var db = mysql.createConnection({ | 26 | var db = mysql.createConnection({ |
28 | host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', | 27 | host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', |
29 | user: 'chatbot', | 28 | user: 'chatbot', |
30 | - password: '11111111', | 29 | + password: '', |
31 | database: 'chatbot', | 30 | database: 'chatbot', |
32 | port: '3306' | 31 | port: '3306' |
33 | }); | 32 | }); |
... | @@ -38,31 +37,58 @@ app.post('/hook', function (req, res) { | ... | @@ -38,31 +37,58 @@ app.post('/hook', function (req, res) { |
38 | if (error) { | 37 | if (error) { |
39 | console.log(error); | 38 | console.log(error); |
40 | } | 39 | } |
41 | - for (var i = 0; i < results.length; i++) | 40 | + var index = 0; |
41 | + var istrue = false; | ||
42 | + for (var i = 0; i < results.length; i++) { | ||
42 | if (results[i].ingredients === message.text) { | 43 | if (results[i].ingredients === message.text) { |
43 | - request.post( | 44 | + index = i; |
44 | - { | 45 | + istrue = true; |
45 | - url: TARGET_URL, | ||
46 | - headers: { | ||
47 | - 'Authorization': `Bearer ${TOKEN}` | ||
48 | - }, | ||
49 | - json: { | ||
50 | - "replyToken": eventObj.replyToken, | ||
51 | - "messages": [ | ||
52 | - { | ||
53 | - "type": "text", | ||
54 | - "text": `메뉴는 "${results[i].menu}" 입니다.` | ||
55 | - }, | ||
56 | - { | ||
57 | - "type": "text", | ||
58 | - "text": `레시피\n${results[i].recipe}` | ||
59 | - } | ||
60 | - ] | ||
61 | - } | ||
62 | - }, (error, response, body) => { | ||
63 | - console.log(body) | ||
64 | - }); | ||
65 | } | 46 | } |
47 | + } | ||
48 | + if (istrue) { | ||
49 | + request.post( | ||
50 | + { | ||
51 | + url: TARGET_URL, | ||
52 | + headers: { | ||
53 | + 'Authorization': `Bearer ${TOKEN}` | ||
54 | + }, | ||
55 | + json: { | ||
56 | + "replyToken": eventObj.replyToken, | ||
57 | + "messages": [ | ||
58 | + { | ||
59 | + "type": "text", | ||
60 | + "text": `메뉴는 "${results[index].menu}" 입니다.` | ||
61 | + }, | ||
62 | + { | ||
63 | + "type": "text", | ||
64 | + "text": `레시피\n${results[index].recipe}` | ||
65 | + } | ||
66 | + ] | ||
67 | + } | ||
68 | + }, (error, response, body) => { | ||
69 | + console.log(body) | ||
70 | + }); | ||
71 | + } | ||
72 | + else { | ||
73 | + request.post( | ||
74 | + { | ||
75 | + url: TARGET_URL, | ||
76 | + headers: { | ||
77 | + 'Authorization': `Bearer ${TOKEN}` | ||
78 | + }, | ||
79 | + json: { | ||
80 | + "replyToken": eventObj.replyToken, | ||
81 | + "messages": [ | ||
82 | + { | ||
83 | + "type": "text", | ||
84 | + "text": `재료를 다시 입력해주세요.` | ||
85 | + } | ||
86 | + ] | ||
87 | + } | ||
88 | + }, (error, response, body) => { | ||
89 | + console.log(body) | ||
90 | + }); | ||
91 | + } | ||
66 | }); | 92 | }); |
67 | 93 | ||
68 | db.end(); | 94 | db.end(); |
... | @@ -82,4 +108,5 @@ try { | ... | @@ -82,4 +108,5 @@ try { |
82 | } catch (error) { | 108 | } catch (error) { |
83 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | 109 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); |
84 | console.log(error); | 110 | console.log(error); |
85 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
111 | +} | ||
112 | + | ... | ... |
-
Please register or login to post a comment