-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathapp.js
252 lines (237 loc) · 8.22 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Dependencies.
var express = require('express');
var path = require('path');
var bodyParser = require("body-parser");
var fs = require('fs');
var request = require('request');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var crypto = require('crypto');
var botAPIAddress;
var channelSecret;
var channelToken;
var pocMode;
var richMenuId;
// Specifie body parsers
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));
app.use(bodyParser.raw({ limit: '50mb', type: '*/*' }));
// Return all static files such as css and js in public folder.
app.use(express.static(__dirname + '/public'))
/* Routing */
// For root, return the emulator
app.get('/', function (req, res) {
res.sendFile(__dirname + "/simulator.html");
});
// Set channelSettings.
app.post('/channelSettings', function (req, res) {
botAPIAddress = req.body.botAPIAddress;
channelSecret = req.body.channelSecret;
channelToken = req.body.channelToken;
request({
headers: { "Authorization": "Bearer " + channelToken },
uri: `${lineAPIUrl}bot/profile/${req.body.userId}`,
method: "GET"
},
function (error, response, body) {
if (body.indexOf("userId") == -1) {
res.status(response.statusCode).send(body);
}
else {
return res.send(body);
}
}
);
});
// Set pocMode.
app.post('/pocMode', function (req, res) {
pocMode = req.body.pocMode;
return res.sendStatus(200);
});
// Receive file from client and send appropriate event to API.
app.post('/upload', function (req, res) {
// Generate contentId by using time and copy the file into upload folder.
var contentId = Date.now().toString();
if (!fs.existsSync(path.join(__dirname, 'public', 'temp'))) {
fs.mkdirSync(path.resolve(__dirname, 'public', 'temp'));
}
if (!fs.existsSync(path.join(__dirname, 'public', 'temp', contentId))) {
fs.mkdirSync(path.resolve(__dirname, 'public', 'temp', contentId));
}
// Create message depending on file type (extension)
var splitFileName = req.body.filename.split('\\');
var filename = splitFileName[splitFileName.length - 1];
var filePath = path.join('temp', contentId, filename);
var fileFullPath = path.join(__dirname, 'public', 'temp', contentId, filename);
if (req.body.base64string) {
fs.writeFileSync(fileFullPath, new Buffer(req.body.base64string.split(',')[1], 'base64'));
} else {
fs.copyFileSync(req.body.filename,
fileFullPath);
}
var fileext = filename.split('.')[1];
var type = "file"
if (fileext === "mp4") {
type = "video";
}
else if (fileext === "png" || fileext === "jpeg" || fileext === "jpg") {
type = "image";
}
else if (fileext === "m4a") {
type = "audio"
}
var sendObject = {
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
"type": "message",
"timestamp": 1462629479859,
"source": {
"type": "user",
"userId": req.body.userId
},
"message": {
"id": contentId,
"type": type
}
};
if (pocMode === "false") {
var jsonData = JSON.stringify({ "events": [sendObject] });
var signature = crypto.createHmac("SHA256", channelSecret)
.update(jsonData)
.digest().toString('base64');
// Send request.
request({
headers: {
"X-Line-Signature": signature,
"Content-Type": "application/json"
},
uri: botAPIAddress,
body: jsonData,
method: 'POST'
},
function (error, response, body) {
// handle result if necessary.
}
);
}
res.send({ "filePath": filePath, "sendObject": sendObject });
});
/* send request to your bot application */
app.post('/send', function (req, res) {
var jsonData = JSON.stringify(req.body);
// Generate hash based on https://developers.line.me/en/docs/messaging-api/reference/#signature-validation
var signature = crypto.createHmac("SHA256", channelSecret)
.update(jsonData)
.digest().toString('base64');
request(
{
headers: {
"X-Line-Signature": signature,
"Content-Type": "application/json"
},
uri: botAPIAddress,
body: jsonData,
method: 'POST'
},
function (error, response, body) {
res.sendStatus(response.statusCode);
}
);
});
/* Get Rich menu for user */
app.get('/richmenu/:userId/:richMenuId', function (req, res) {
// Generate hash based on https://developers.line.me/en/docs/messaging-api/reference/#signature-validation
let url = `bot/user/${req.params.userId}/richmenu`;
richMenuId = req.params.richMenuId;
request({
headers: { 'Authorization': `Bearer ${channelToken}` },
uri: `${lineAPIUrl}${url}`,
method: "GET"
}, function (error, response, body) {
if (JSON.parse(response.body).message === "the user has no richmenu") {
res.send({ "message": "no menu" });
}
// If rich menuId is same as passed one, do nothing.
else if (JSON.parse(response.body).richMenuId == richMenuId) {
res.send(null);
}
// otherwise get the richmenu.
else {
richMenuId = JSON.parse(response.body).richMenuId;
url = `bot/richmenu/${richMenuId}/content`;
request({
headers: { 'Authorization': `Bearer ${channelToken}` },
encoding: null,
uri: `${lineAPIUrl}${url}`,
method: "GET"
}, function (error, response, body) {
var image = body;
url = `bot/richmenu/${richMenuId}`;
request({
headers: { 'Authorization': `Bearer ${channelToken}` },
uri: `${lineAPIUrl}${url}`,
method: "GET"
}, function (error, response, body) {
res.send({ "richMenu": JSON.parse(body), "image": image });
});
});
}
});
});
//#region Behave as LINE Platform
const lineAPIUrl = "https://api.line.me/v2/";
// Receive request from your bot application.
app.all('/*', function (req, res) {
var url = req.url;
// reply, push and multicast will be simply pass to UI.
if (url.indexOf('reply') > -1 || url.indexOf('push') > -1 || url.indexOf('multicast') > -1) {
io.emit('reply', req.body);
res.status(200).send({});
}
// if it request content
else if (url.indexOf('content') > -1 && req.method.toLowerCase() == 'get') {
// The actual file sit in public\temp. Returns the file with messageId
let messageId = url.slice(url.indexOf('message') + 8, url.indexOf('content') - 1);
var files = fs.readdirSync(path.join(__dirname, 'public', 'temp', messageId));
res.sendFile(path.join(__dirname, 'public', 'temp', messageId, files[0]));
}
else {
handleRequest(req, res);
}
});
// Handle all request by add LINE Platform Url.
function handleRequest(req, res) {
// remove host header
delete req.headers['host'];
delete req.headers['content-length'];
// Craft URL for LINE Platform.
var url = req.url;
if (url.indexOf('oauth') > -1) {
url = url.slice(url.indexOf('oauth'), url.length);
}
else if (url.indexOf('bot') > -1) {
url = url.slice(url.indexOf('bot'), url.length);
}
var options = {
headers: req.headers,
uri: `${lineAPIUrl}${url}`,
method: req.method
};
if (req.body && req.body instanceof Buffer) {
Object.assign(options, { body: req.body })
}
else if (req.body && req.body instanceof Object) {
Object.assign(options, { json: req.body })
}
request(options,
function (error, response, body) {
if (error) console.error(error);
res.status(response.statusCode).send(body);
}
);
}
//#endregion
/* Start the service */
http.listen(8080, function () {
console.log('listening on *:8080');
});