Showing
1 changed file
with
95 additions
and
0 deletions
I_to_D_chatbot/reply.js
0 → 100644
1 | +var express = require('express'); | ||
2 | +const request = require('request'); | ||
3 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
4 | +const TOKEN = 'zVAczqoJ+L9oykqhEj7HoP7f6Nyb+R3T1TntHXQhYihI+KIxH4SZDGpKu0jKsMXMHKBVXpmbybA+oaV8u/dfLppKe3NHXU9AdBVypy9NgfWiFPLlcwm3GdkVPAZGoS4nvOCXWDKUb+ixPKWjlbnChAdB04t89/1O/w1cDnyilFU=' | ||
5 | +const fs = require('fs'); | ||
6 | +const path = require('path'); | ||
7 | +const HTTPS = require('https'); | ||
8 | +const domain = "2019102226.osschatbot2022.ml" | ||
9 | +const sslport = 23023; | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | +const bodyParser = require('body-parser'); | ||
14 | +var app = express(); | ||
15 | +app.use(bodyParser.json()); | ||
16 | +app.post('/hook', function (req, res) { | ||
17 | + | ||
18 | + var eventObj = req.body.events[0]; | ||
19 | + var source = eventObj.source; | ||
20 | + var message = eventObj.message; | ||
21 | + | ||
22 | + // request log | ||
23 | + console.log('======================', new Date(), '======================'); | ||
24 | + console.log('[request]', req.body); | ||
25 | + console.log('[request source] ', eventObj.source); | ||
26 | + console.log('[request message]', eventObj.message); | ||
27 | + | ||
28 | + var incredients_list = message.text.split(" "); | ||
29 | + | ||
30 | + function findingredients(item) { return item === "김치"; } | ||
31 | + | ||
32 | + // mwsql | ||
33 | + // mysql -u chatbot -p -h chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com | ||
34 | + var mysql = require('mysql'); | ||
35 | + var db = mysql.createConnection({ | ||
36 | + host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', | ||
37 | + user: 'chatbot', | ||
38 | + password: '11111111', | ||
39 | + database: 'chatbot', | ||
40 | + port: '3306' | ||
41 | + }); | ||
42 | + | ||
43 | + db.connect(); | ||
44 | + | ||
45 | + db.query('SELECT * FROM data', function (error, results, fields) { | ||
46 | + if (error) { | ||
47 | + console.log(error); | ||
48 | + } | ||
49 | + for (var i = 0; i < results.length; i++) | ||
50 | + if (results[i].ingredients === message.text) { | ||
51 | + console.log(`메뉴 : ${results[i].menu}, 레시피 : ${results[i].recipe}`); | ||
52 | + request.post( | ||
53 | + { | ||
54 | + url: TARGET_URL, | ||
55 | + headers: { | ||
56 | + 'Authorization': `Bearer ${TOKEN}` | ||
57 | + }, | ||
58 | + json: { | ||
59 | + "replyToken": eventObj.replyToken, | ||
60 | + "messages": [ | ||
61 | + { | ||
62 | + "type": "text", | ||
63 | + "text": `${results[i].menu}` | ||
64 | + }, | ||
65 | + { | ||
66 | + "type": "text", | ||
67 | + "text": `${results[i].recipe}` | ||
68 | + } | ||
69 | + ] | ||
70 | + } | ||
71 | + }, (error, response, body) => { | ||
72 | + console.log(body) | ||
73 | + }); | ||
74 | + } | ||
75 | + }); | ||
76 | + | ||
77 | + db.end(); | ||
78 | + res.sendStatus(200); | ||
79 | +}); | ||
80 | + | ||
81 | +try { | ||
82 | + const option = { | ||
83 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), | ||
84 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), | ||
85 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), | ||
86 | + }; | ||
87 | + | ||
88 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
89 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
90 | + }); | ||
91 | +} catch (error) { | ||
92 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
93 | + console.log(error); | ||
94 | +} | ||
95 | + |
-
Please register or login to post a comment