오규림

basic function

Showing 1 changed file with 94 additions and 74 deletions
1 -var express = require("express"); 1 +var express = require('express');
2 - 2 +const request = require('request');
3 -var http = require("http"); 3 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 - 4 +const TOKEN = 'dClDD+86CVGoR/ozUsC1ViT0lyRMsFwQ3bOoUejOVI4yJ2AQRKHFyBF+CjYW5DLtF7EztDgigy+VSDFQxYNtvGqLGngGNYz1Fzq6kxxI7YqlDnVRV7gHFVkc42VfcEMQ/ozXTqeCY1JYFAmw30fBXQdB04t89/1O/w1cDnyilFU='
5 -var bodyParser = require("body-parser"); 5 +const MULTI_TARGET_URL = 'https://api.line.me/v2/bot/message/multicast'
6 - 6 +const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
7 +const fs = require('fs');
8 +const path = require('path');
9 +const HTTPS = require('https');
10 +const domain = "2019102195.oss2021.tk"
11 +const sslport = 23023;
12 +
13 +const bodyParser = require('body-parser');
7 var app = express(); 14 var app = express();
8 15
9 -app.get("/Personal_color_chatbot", function (req, res) { 16 +request.post(
10 - var data = { 17 + {
11 - type: "buttons", 18 + url: BROAD_TARGET_URL,
12 - buttons: ["퍼스널 컬러 진단하기", "봄웜", "여름쿨", "가을웜", "겨울쿨"], 19 + headers: {
13 - }; 20 + 'Authorization': `Bearer ${TOKEN}`
14 -
15 - res.json(data);
16 -});
17 -
18 -app.use(bodyParser.urlencoded({ extended: false }));
19 -app.use(bodyParser.json());
20 -
21 -app.post("/message", function (req, res) {
22 - var msg = req.body.content;
23 - console.log("전달받은 메세지: " + msg);
24 -
25 - var send = {};
26 -
27 - switch (msg) {
28 - case "퍼스널 컬러 진단하기":
29 - send = {
30 - message: {
31 - text: "퍼스널 컬러 진단하기 선택",
32 - },
33 - };
34 - break;
35 -
36 - case "봄웜":
37 - send = {
38 - message: {
39 - text: "봄웜 선택",
40 - },
41 - };
42 - break;
43 -
44 - case "여름쿨":
45 - send = {
46 - message: {
47 - text: "여름쿨 선택",
48 }, 21 },
49 - }; 22 + json: {
50 - break; 23 + "messages":[
24 + {
25 + "type":"text",
26 + "text":"안녕하세요! 테스트입니다."
27 + },
28 + {
29 + "type":"text",
30 + "text":"저에게 '테스트'라고 입력해주세요!"
31 + }
32 + ]
33 + }
34 + },(error, response, body) => {
35 + console.log(body)
36 + });
51 37
52 - case "가을웜":
53 - send = {
54 - message: {
55 - text: "가을웜 선택",
56 - },
57 - };
58 - break;
59 38
60 - case "겨울쿨":
61 - send = {
62 - message: {
63 - text: "겨을쿨 선택",
64 - },
65 - };
66 - break;
67 39
68 - default: 40 +app.use(bodyParser.json());
69 - send = { 41 +app.post('/hook', function (req, res) {
70 - message: { 42 +
71 - text: "알 수 없는 명령입니다.", 43 + var eventObj = req.body.events[0];
72 - }, 44 + var source = eventObj.source;
73 - }; 45 + var message = eventObj.message;
74 - break; 46 +
75 - } 47 + // request log
76 - 48 + console.log('======================', new Date() ,'======================');
77 - res.json(send); 49 + console.log('[request]', req.body);
50 + console.log('[request source] ', eventObj.source);
51 + console.log('[request message]', eventObj.message);
52 +
53 + if(eventObj.message.text=='테스트'){
54 + request.post(
55 + {
56 + url: TARGET_URL,
57 + headers: {
58 + 'Authorization': `Bearer ${TOKEN}`
59 + },
60 + json: {
61 + "replyToken":eventObj.replyToken,
62 + "messages":[
63 + {
64 + "type":"text",
65 + "text":"테스트라고 입력해주셨군요!"
66 + },
67 + {
68 + "type":"text",
69 + "text":"다음 단계로 넘어가고 싶으신가요? [Y/N]"
70 + }
71 + ]
72 + }
73 + },(error, response, body) => {
74 + console.log(body)
75 + });
76 + }
77 +
78 +
79 + res.sendStatus(200);
78 }); 80 });
79 81
80 -http.createServer(app).listen(9090, function () { 82 +function color_chatbot_test1(replyToken, message){
81 - console.log("서버실행중.."); 83 +
82 -}); 84 +}
85 +
86 +
87 +
88 +try {
89 + const option = {
90 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
91 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
92 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
93 + };
94 +
95 + HTTPS.createServer(option, app).listen(sslport, () => {
96 + console.log(`[HTTPS] Server is started on port ${sslport}`);
97 + });
98 + } catch (error) {
99 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
100 + console.log(error);
101 + }
102 +
...\ No newline at end of file ...\ No newline at end of file
......