장수창

added status reply

1 var express = require('express'); 1 var express = require('express');
2 const request = require('request'); 2 const request = require('request');
3 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 3 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 -const TOKEN = 'vGuGN1jGbvuOH79MdjIY00fdDr+nDp0ZGDUqzidK5u4c1KiMJQ1UjrgekSiptVVK2sMymCx7U761J1M/8So3gCWWRR34zvPp9YZpUFn07/gPd7yKVFPgZO/kTaEwyjvBk9WI+OfZo1ch8YD8x6iUzgdB04t89/1O/w1cDnyilFU=' 4 +const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
5 +const TOKEN = ''
5 const fs = require('fs'); 6 const fs = require('fs');
6 const path = require('path'); 7 const path = require('path');
7 const HTTPS = require('https'); 8 const HTTPS = require('https');
...@@ -10,6 +11,7 @@ const sslport = 23023; ...@@ -10,6 +11,7 @@ const sslport = 23023;
10 11
11 const bodyParser = require('body-parser'); 12 const bodyParser = require('body-parser');
12 var app = express(); 13 var app = express();
14 +
13 app.use(bodyParser.json()); 15 app.use(bodyParser.json());
14 app.post('/hook', function (req, res) { 16 app.post('/hook', function (req, res) {
15 17
...@@ -23,6 +25,28 @@ app.post('/hook', function (req, res) { ...@@ -23,6 +25,28 @@ app.post('/hook', function (req, res) {
23 console.log('[request source] ', eventObj.source); 25 console.log('[request source] ', eventObj.source);
24 console.log('[request message]', eventObj.message); 26 console.log('[request message]', eventObj.message);
25 27
28 + var isNationWide = message.text;
29 + var sub_nationwide = "국내";
30 + if(isNationWide.indexOf(sub_nationwide) !== -1){
31 + replyNationWide(eventObj);
32 + }
33 + else if(message.text == "야" || message.text == "도움말"){
34 + hello(eventObj)
35 + }
36 +
37 + res.sendStatus(200);
38 +});
39 +
40 +//reply
41 +function replyNationWide(eventObj) {
42 + //read status
43 + var strings = ['날짜', '확진환자', '격리해제', '검사진행', '사망자']
44 + var fs = require('fs');
45 + var array = fs.readFileSync('status.txt').toString().split("\n");
46 + for(i in array) {
47 + strings[i] = array[i]
48 + }
49 +
26 request.post( 50 request.post(
27 { 51 {
28 url: TARGET_URL, 52 url: TARGET_URL,
...@@ -34,6 +58,76 @@ app.post('/hook', function (req, res) { ...@@ -34,6 +58,76 @@ app.post('/hook', function (req, res) {
34 "messages":[ 58 "messages":[
35 { 59 {
36 "type":"text", 60 "type":"text",
61 + "text":strings[0]
62 + },
63 + {
64 + "type":"text",
65 + "text":strings[1]
66 + },
67 + {
68 + "type":"text",
69 + "text":strings[2]
70 + },
71 + {
72 + "type":"text",
73 + "text":strings[3]
74 + },
75 + {
76 + "type":"text",
77 + "text":strings[4]
78 + },
79 + ]
80 + }
81 + },(error, response, body) => {
82 + console.log(body)
83 + });
84 +}
85 +
86 +//hello
87 +function hello(eventObj) {
88 +
89 + request.post(
90 + {
91 + url: TARGET_URL,
92 + headers: {
93 + 'Authorization': `Bearer ${TOKEN}`
94 + },
95 + json: {
96 + "replyToken":eventObj.replyToken,
97 + "messages":[
98 + {
99 + "type":"text",
100 + "text":"안녕하십니까. 코로나 도우미 입니다."
101 + },
102 + {
103 + "type":"text",
104 + "text":"국내 현황은 '국내'를 타이핑 해주세요."
105 + },
106 + {
107 + "type": "sticker",
108 + "packageId": "11539",
109 + "stickerId": "52114110"
110 + }
111 +
112 + ]
113 + }
114 + },(error, response, body) => {
115 + console.log(body)
116 + });
117 +}
118 +
119 +// Broadcast
120 +function broadcast(eventObj){
121 + request.post(
122 + {
123 + url: BROAD_TARGET_URL,
124 + headers: {
125 + 'Authorization': `Bearer ${TOKEN}`
126 + },
127 + json: {
128 + "messages":[
129 + {
130 + "type":"text",
37 "text":"Hello, user" 131 "text":"Hello, user"
38 }, 132 },
39 { 133 {
...@@ -45,10 +139,7 @@ app.post('/hook', function (req, res) { ...@@ -45,10 +139,7 @@ app.post('/hook', function (req, res) {
45 },(error, response, body) => { 139 },(error, response, body) => {
46 console.log(body) 140 console.log(body)
47 }); 141 });
48 - 142 +}
49 -
50 - res.sendStatus(200);
51 -});
52 143
53 try { 144 try {
54 const option = { 145 const option = {
......