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
export const ntfy = () => {
class NtfyPublisher {
private serverUrl: string = "ntfy.sh";
private topic: string | null = null;
private message: string | null = null;
private file: any | null = null;
private actions: string[] = [];
private headers: Record<string, string> = {};
toServer(url: string): NtfyPublisher {
this.serverUrl = url;
return this;
}
asUser(user: string, password: string) {
const payload = btoa(`${user}:${password}`);
this.headers["Authorization"] = `Basic ${payload}`;
return this;
}
usingToken(token: string) {
this.headers["Authorization"] = `Bearer ${token}`;
return this;
}
toTopic(topic: string) {
this.topic = topic;
return this;
}
withMessage(message: string, markdown = false) {
if (this.file != null)
throw new Error(
"A file is already attached. Can't send a message and a file at once.",
);
if (this.message != null)
throw new Error(
"A message has been already set. Can't send 2 messages at once.",
);
this.message = message;
if (markdown) {
this.headers["Markdown"] = "yes";
}
return this;
}
withTitle(title: string) {
this.headers["Title"] = title;
return this;
}
withPriority(priority: "min" | "low" | "default" | "high" | "max") {
this.headers["Priority"] = priority;
return this;
}
withMinPriority() {
return this.withPriority("min");
}
withLowPriority() {
return this.withPriority("low");
}
withDefaultPriority() {
return this.withPriority("default");
}
withHighPriority() {
return this.withPriority("high");
}
withMaxPriority() {
return this.withPriority("max");
}
withTags(...tags: string[]) {
this.headers["Tags"] = tags.join(",");
return this;
}
withDelay(when: string) {
this.headers["Delay"] = when;
return this;
}
withViewAction(label: string, url: string, clear: string | null = null) {
if (this.actions.length >= 3) {
throw new Error("Max of 3 actions are allowed.");
}
this.actions.push(
`view, ${label}, ${url}${clear == null ? "" : ", clear=" + clear}`,
);
return this;
}
withBroadcastAction(
label: string,
intent: string | null = null,
extras: Record<string, string> = {},
clear: boolean | null = null,
) {
if (this.actions.length >= 3) {
throw new Error("Max of 3 actions are allowed.");
}
const segments = ["broadcast", label];
if (intent != null) {
segments.push(`intent=${intent}`);
}
if (clear != null) {
segments.push(`clear=${clear}`);
}
for (const [key, value] of Object.entries(extras)) {
segments.push(`extras.${key}=${value}`);
}
this.actions.push(segments.join(", "));