박주영

Merge branch 'master' of http://khuhub.khu.ac.kr/term-project/project into master

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 = 'z5iy5sMU1W4xZAlwvn0/5x4U+4ZsqI0hKO1ZZNFxUGlNzGBjFg2D1u6/Ij5C/Sbkncx3hyYg7Nfz5JnMD8BG/9Z3TEEHPvy1A2XhkPKs04v0/n6TjH1A3e9X23zYdYmNSGyPn2hDGglgm2p3YmtLSwdB04t89/1O/w1cDnyilFU=';
5 -const fs = require('fs');
6 -const path = require('path');
7 -const HTTPS = require('https');
8 -const domain = 'www.foodbot2020.ml';
9 -const sslport = 23023;
10 -
11 -const bodyParser = require('body-parser');
12 -var app =express();
13 -app.use(bodyParser.json());
14 -app.post('/hook', function (req, res) {
15 -
16 - var eventObj = req.body.events[0];
17 - var source = eventObj.source;
18 - var message = eventObj.message;
19 -
20 - console.log('======================', new Date() ,'======================');
21 - console.log('[request]', req.body);
22 - console.log('[request source] ', eventObj.source);
23 - console.log('[request message]', eventObj.message);
24 -
25 - request.post(
26 - {
27 - url: TARGET_URL,
28 - headers: {
29 - 'Authorization': `Bearer ${TOKEN}`
30 - },
31 - json: {
32 - "replyToken":eventObj.replyToken,
33 - "messages":[
34 - {
35 - "type":"text",
36 - "text":"Hello, this is foodbot2020. "
37 - },
38 - {
39 - "type":"text",
40 - "text":"May I help you?"
41 - }
42 - ]
43 - }
44 - },(error, response, body) => {
45 - console.log(body)
46 - });
47 -
48 -
49 - res.sendStatus(200);
50 -});
51 -
52 -try {
53 - const option = {
54 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
55 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
56 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
57 - };
58 -
59 - HTTPS.createServer(option, app).listen(sslport, () => {
60 - console.log(`[HTTPS] Server is started on port ${sslport}`);
61 - });
62 - } catch (error) {
63 - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
64 - console.log(error);
65 - }
...\ No newline at end of file ...\ No newline at end of file