전세계

테스트: line webhook 테스트

Showing 1 changed file with 59 additions and 3 deletions
...@@ -7,6 +7,15 @@ const axios = require('axios'); ...@@ -7,6 +7,15 @@ const axios = require('axios');
7 const cheerio = require('cheerio'); 7 const cheerio = require('cheerio');
8 const url = 'http://lol.inven.co.kr/dataninfo/proteam/progamer.php?code=135'; 8 const url = 'http://lol.inven.co.kr/dataninfo/proteam/progamer.php?code=135';
9 9
10 +const request = require('request');
11 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply';
12 +const TOKEN = 'ihoYpWsdObaqmCnQ7nqwBfSGiLV4v3OZSqccIK9osz9y9pOMiKgEpmJ8fHiiBT0rF5wZoa2bRGTQjZ7al4yseviaYPsROO4nyiygZi8lvW2xcD6yakxXZ91eNmzz8e6fQ32IPZ577Iagx/kEQ/UMgAdB04t89/1O/w1cDnyilFU=';
13 +const path = require('path');
14 +const HTTPS = require('https');
15 +const domain = "www.sekechatbot.tk"
16 +const sslport = 23023;
17 +const bodyParser = require('body-parser');
18 +
10 const version = '0.1'; 19 const version = '0.1';
11 const dataFileName = 'data.json'; 20 const dataFileName = 'data.json';
12 21
...@@ -204,9 +213,56 @@ async function getData() { ...@@ -204,9 +213,56 @@ async function getData() {
204 }); 213 });
205 } 214 }
206 215
216 +app.use(bodyParser.json());
217 +app.post('/hook', function (req, res) {
218 + var eventObj = req.body.events[0];
219 + var source = eventObj.source;
220 + var message = eventObj.message;
221 +
222 + // request log
223 + console.log('======================', new Date() ,'======================');
224 + console.log('[request]', req.body);
225 + console.log('[request source] ', eventObj.source);
226 + console.log('[request message]', eventObj.message);
207 227
208 -app.get('/', (req, res) => { 228 + sendMessage(eventObj.replyToken, eventObj.message.text);
209 - res.send('Express Test'); 229 +
230 + res.sendStatus(200);
210 }); 231 });
211 232
212 -app.listen(port, () => console.log(`app listening at http://localhost:${port}`));
...\ No newline at end of file ...\ No newline at end of file
233 +function sendMessage(replyToken, message) {
234 + request.post(
235 + {
236 + url: TARGET_URL,
237 + headers: {
238 + 'Authorization': `Bearer ${TOKEN}`
239 + },
240 + json: {
241 + "replyToken":replyToken,
242 + "messages":[
243 + {
244 + "type":"text",
245 + "text":"test"
246 + }
247 + ]
248 + }
249 + },(error, response, body) => {
250 + console.log(body)
251 + }
252 + );
253 +}
254 +
255 +try {
256 + const option = {
257 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
258 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
259 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
260 + };
261 +
262 + HTTPS.createServer(option, app).listen(sslport, () => {
263 + console.log(`[HTTPS] Server is started on port ${sslport}`);
264 + });
265 + } catch (error) {
266 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
267 + console.log(error);
268 + }
...\ No newline at end of file ...\ No newline at end of file
......