-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.js
74 lines (62 loc) · 1.92 KB
/
message.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
import got from 'got';
const messageEmojis = {
"report.title": "✍🏻",
"report.url": "🔗",
"cwe": "CWE",
"severity_rating": "Severity",
"reporter.username": "🧑🏻💻",
"team.handle": "🏢",
"total_awarded_amount": "💵",
"report.disclosed_at": "📆",
}
// Thank you ChatGPT
function reduceObject(input1, input2) {
const reducedObject = {};
Object.keys(input2).forEach(key => {
// Access nested properties if key contains dot notation
const value = key.split('.').reduce((obj, k) => obj && obj[k], input1);
// Assign value to the key from input2
reducedObject[input2[key]] = value;
});
return reducedObject;
}
// Thank you ChatGPT
function objectToString(obj) {
let result = '';
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result += `${key}: ${obj[key]}\n `;
}
}
console.log(result);
return result;
}
export class Logger {
constructor(verbose) {
this.verbose = verbose
}
info(message) {
if (this.verbose) {
console.log(`[+]: ${message}`);
}
}
notifFormatter(message) {
const messageObj = reduceObject(message, messageEmojis);
return objectToString(messageObj)
}
}
export async function sendNotification(message, channelID, botToken) {
try {
const apiUrl = `https://api.telegram.org/bot${botToken}/sendMessage?chat_id=${channelID}&text=${encodeURIComponent(message)}`;
console.log(`[+] Sending notification to telegram...`);
const response = await got.get(apiUrl);
if (response.statusCode === 200) {
console.log('[+] Notification sent successfully!');
} else {
throw new Error(`[-] Failed to send notification. Status code: ${response}`);
}
} catch (error) {
console.log(error);
console.error('[-] Error sending notification: ', error.message);
}
}