forked from hacettepeoyt/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
54 lines (45 loc) · 1.46 KB
/
utils.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
const https = require("https");
async function sendMessageToAdminRoom(message) {
const body = JSON.stringify({
msgtype: "m.text",
body: message
});
var post_options = {
host: 'matrix.org',
path: `/_matrix/client/r0/rooms/${process.env.MATRIX_ADMIN_ROOM}/send/m.room.message?access_token=${process.env.MATRIX_ACCESS_TOKEN}`,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
return new Promise((resolve, reject) => {
const request = https.request(post_options, response => {
const response_data = [];
response.on('data', chunk => {
response_data.push(chunk);
});
if (response.statusCode === 200) {
response.on('end', () => resolve(JSON.parse(Buffer.concat(response_data).toString())));
} else {
response.on('end', () => {
let error = new Error(`Matrix returned status code: ${response.statusCode}`);
error.response = JSON.parse(Buffer.concat(response_data).toString());
reject(error);
});
}
});
request.on('error', reject);
request.write(body);
request.end();
});
}
function validateString(text) {
if (text.length <= 64) {
return true;
}
return false;
}
module.exports = {
sendMessageToAdminRoom,
validateString
};