김대선

채팅 예제 코드 적용

1 datas/ 1 datas/
2 -node_modules/
...\ No newline at end of file ...\ No newline at end of file
2 +node_modules/
3 +config.js
...\ No newline at end of file ...\ No newline at end of file
......
1 -const express = require('express') 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 = require('config').TOKEN
5 +const fs = require('fs');
6 +const path = require('path');
7 +const HTTPS = require('https');
8 +const domain = require('config').domain
9 +const sslport = 23023;
10 +const bodyParser = require('body-parser');
11 +
2 const schedule = require('node-schedule') 12 const schedule = require('node-schedule')
3 13
4 const data = require('./functions/dataFunctions') 14 const data = require('./functions/dataFunctions')
5 const find = require('./functions/findFunction') 15 const find = require('./functions/findFunction')
6 -const app = express();
7 -
8 -
9 16
10 // 0초 0분 0시 아무날 아무달 아무년 17 // 0초 0분 0시 아무날 아무달 아무년
11 const saveData = schedule.scheduleJob('55 45 20 * * *', data.save) 18 const saveData = schedule.scheduleJob('55 45 20 * * *', data.save)
12 19
13 -const server = app.listen(3000,()=>{ 20 +var app = express();
14 - const host = server.address().address 21 +app.use(bodyParser.json());
15 - const port = server.address().port 22 +app.post('/hook', function (req, res) {
16 - console.log("app listening at http://%s:%s", host, port) 23 + var eventObj = req.body.events[0];
17 -}) 24 + var source = eventObj.source;
18 - 25 + var message = eventObj.message;
19 - 26 + // request log
20 -app.get('/', async (req, res)=>{ 27 + console.log('======================', new Date() ,'======================');
28 + console.log('[request]', req.body);
29 + console.log('[request source] ', eventObj.source);
30 + console.log('[request message]', eventObj.message);
31 + request.post(
32 + {
33 + url: TARGET_URL,
34 + headers: {
35 + 'Authorization': `Bearer ${TOKEN}`
36 + },
37 + json: {
38 + "replyToken":eventObj.replyToken,
39 + "messages":[
40 + {
41 + "type":"text",
42 + "text":"Hello, user"
43 + },
44 + {
45 + "type":"text",
46 + "text":"May I help you?"
47 + }
48 + ]
49 + }
50 + },(error, response, body) => {
51 + console.log(body)
52 + });
53 +
54 + res.sendStatus(200);
55 +});
56 +try {
57 + const option = {
58 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
59 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
60 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
61 + };
21 62
22 - console.log(find.byCompanyName('naver')[0], find.byTags('백엔드')[0], find.byTitle('백엔드')[0])
23 - res.send('helloworld')
24 -})
...\ No newline at end of file ...\ No newline at end of file
63 + HTTPS.createServer(option, app).listen(sslport, () => {
64 + console.log(`[HTTPS] Server is started on port ${sslport}`);
65 + });
66 +} catch (error) {
67 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
68 + console.log(error);
69 +}
......