Seungmi

기본 세팅

Showing 1 changed file with 134 additions and 0 deletions
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 = 'BDWbq6IfUnMHJrG2k80BvhNly63c/K0TURS26/Kx1EWW82d3o767nHhGfu1G9PbJDye2vg6blpqTUdEU3ATMwN1NEZd0GggujmBbHuUk3iBrzBCTm5LUqHlf4+5lFS8eQb7i9WCcYjdakEt2EAYiZQdB04t89/1O/w1cDnyilFU='
5 +const fs = require('fs');
6 +const path = require('path');
7 +const HTTPS = require('https');
8 +const domain = "www.weatherchatbot.ml"
9 +const sslport = 23023;
10 +const bodyParser = require('body-parser');
11 +var app = express();
12 +const { info } = require('console');
13 +
14 +var PythonShell = require('python-shell');
15 +
16 +var options = {
17 + mode: 'text',
18 + pythonPath: '',
19 + pythonOptions: ['-u'],
20 + scriptPath: '',
21 + args: ['value1', 'value2', 'value3']
22 +};
23 +
24 +app.use(bodyParser.json());
25 +
26 +app.post('/hook', function (req, res) {
27 +
28 + var eventObj = req.body.events[0];
29 + var source = eventObj.source;
30 + var message = eventObj.message;
31 +
32 + // request log
33 + console.log('======================', new Date() ,'======================');
34 + console.log('[request]', req.body);
35 + console.log('[request source] ', eventObj.source);
36 + console.log('[request message]', eventObj.message);
37 +
38 + if(eventObj.message.text == "설명"){
39 + chatInfo(eventObj.replyToken);
40 + }
41 + else if(eventObj.message.text == "이용방법") {
42 + chatUse(eventObj.replyToken);
43 + }
44 + else if(eventObj.message.text == "오늘의 날씨는") {
45 + PythonShell.run('weather_chat.py', options, function(err, results) {
46 + if(err) throw err;
47 + console.log('results: %j', results);
48 + });
49 + }
50 + else {
51 + chatWrong(eventObj.replyToken);
52 + }
53 +
54 + res.sendStatus(200);
55 +});
56 +
57 +function chatInfo(replyToken){
58 + request.post(
59 + {
60 + url: TARGET_URL,
61 + headers: {
62 + 'Authorization': `Bearer ${TOKEN}`
63 + },
64 + json: {
65 + "replyToken":replyToken,
66 + "messages":[
67 + {
68 + "type":"text",
69 + "text":"서울 지역의 날씨와 기온에 따른 옷차림새를 추천해줍니다."
70 + }
71 + ]
72 + }
73 + },(error, response, body) => {
74 + console.log(body)
75 + });
76 +}
77 +
78 +function chatUse(replyToken){
79 + request.post(
80 + {
81 + url: TARGET_URL,
82 + headers: {
83 + 'Authorization': `Bearer ${TOKEN}`
84 + },
85 + json: {
86 + "replyToken":replyToken,
87 + "messages":[
88 + {
89 + "type":"text",
90 + "text":"오늘의 날씨는? 을 똑같이\n 입력해주시면 작동합니다."
91 + }
92 + ]
93 + }
94 + },(error, response, body) => {
95 + console.log(body)
96 + });
97 +}
98 +
99 +function chatWrong(replyToken){
100 + request.post(
101 + {
102 + url: TARGET_URL,
103 + headers: {
104 + 'Authorization': `Bearer ${TOKEN}`
105 + },
106 + json: {
107 + "replyToken":replyToken,
108 + "messages":[
109 + {
110 + "type":"text",
111 + "text":"잘못된 입력입니다.\n '이용방법' 혹은 '오늘의 날씨는?'을 입력해주세요."
112 + }
113 + ]
114 + }
115 + },(error, response, body) => {
116 + console.log(body)
117 + });
118 +}
119 +
120 +try {
121 + const option = {
122 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
123 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
124 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
125 + };
126 +
127 + HTTPS.createServer(option, app).listen(sslport, () => {
128 + console.log(`[HTTPS] Server is started on port ${sslport}`);
129 + });
130 + } catch (error) {
131 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
132 + console.log(error);
133 + }
134 +
...\ No newline at end of file ...\ No newline at end of file