-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathntfy.ts
More file actions
34 lines (32 loc) · 893 Bytes
/
ntfy.ts
File metadata and controls
34 lines (32 loc) · 893 Bytes
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
import { Logger } from "../logging/logger.ts";
export class Ntfy {
public constructor(
private readonly host: string,
private readonly topic: string,
private readonly username: string = '',
private readonly password: string = ''
) {
}
/**
* Send a notification through Ntfy
*
* @param message
*/
public async send(message: string): Promise<void> {
try {
const auth = btoa(`${this.username}:${this.password}`);
const resp = await fetch(`${this.host}/${this.topic}`, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Authorization': `Basic ${auth}`
},
body: message
});
if(resp.status === 200) return;
throw Error(`${resp.status} - ${resp.statusText}`);
} catch(e) {
Logger.error(`Could not send notification: "${e.message}"`, e.stack);
}
}
}