-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
145 lines (132 loc) · 3.68 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/env node
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const fs = require('fs');
const request = require("request");
const http = require('http');
const app = express();
const PORT = process.env.OPENSHIFT_NODEJS_PORT || 8888 ;
const ADDR = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0' ;
// LINE Notify
const OAUTH_PAGE = 'https://linebot.rhcloud.com/oauth.html';
const OAUTH_REDIRECT = 'https://linebot.rhcloud.com/add-notify-user';
const NFY_API = 'https://notify-api.line.me/api/notify';
const NFY_TOKEN_API = 'https://notify-bot.line.me/oauth/token';
const NFY_CLIENT_ID = '';
const NFY_CLIENT_SECRET = '';
const NOTIFY_FILE = 'notify-users.json';
let notifyUsers = {};
const sendNotify = (token, message) => {
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: NFY_API,
headers: {
'Authorization': 'Bearer ' + token,
},
form: {
'message': message
}
}, (err, response, body) => {
if (err) {
return reject(err);
} else {
console.log('Response: ', body);
return resolve(body);
}
});
});
};
const getNotifyToken = (code) => {
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: NFY_TOKEN_API,
form: {
"grant_type": "authorization_code",
"redirect_uri": OAUTH_REDIRECT,
"client_id": NFY_CLIENT_ID,
"client_secret": NFY_CLIENT_SECRET,
"code": code,
},
}, (err, response, body) => {
if (err) {
return reject(err);
} else {
console.log('Response: ', body);
return resolve(body);
}
});
});
};
const readFile = (file) => {
let ret = {};
try {
let data = fs.readFileSync(file, 'utf8');
if(data) {
ret = JSON.parse(data);
}
} catch(e) {
console.dir(e);
} finally {
return ret;
}
};
const writeFile = (file, data) => {
try {
let _data = JSON.stringify(data);
fs.writeFileSync(file, _data, 'utf8');
} catch(e) {
console.dir(e);
}
};
app.use(express.static(path.join(__dirname, 'static')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res, next) => {
notifyUsers = readFile(NOTIFY_FILE);
return next();
});
app.get('/health', (req, res) => {
return res.end();
});
// callback function after user sign up to LINE Nofigy
app.get('/add-notify-user', async(req, res) => {
try {
let code = req.query.code;
let body = await getNotifyToken(code);
if(!body) {
throw 'Not found access_token!';
} else {
body = JSON.parse(body);
}
notifyUsers[code] = {};
notifyUsers[code].token = body.access_token;
notifyUsers[code].state = req.query.state;
writeFile(NOTIFY_FILE, notifyUsers);
return res.send('Success to save! Code=' + req.query.code + '\nState=' + req.query.state);
} catch(e) {
console.dir(e);
return res.status(400).send('Failed to add user! Code=' + req.query.code + '\nState=' + req.query.state);
}
});
// Send LINE Notify test message
app.get('/line-notify', async(req, res) => {
let message = req.query.message ? req.query.message : 'Test Message!';
for(let code in notifyUsers) {
if(!(notifyUsers[code] && notifyUsers[code].token)) {
continue;
}
try {
let token = notifyUsers[code].token;
await sendNotify(token, message);
} catch(e) {
console.dir(e);
}
}
return res.end();
});
app.listen(PORT, ADDR, () => {
console.log(`Worker ${process.pid} started at Port ${PORT}`);
});