-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added notification support for DNS toggling and validation logic
- Loading branch information
1 parent
0e174f9
commit b0e7dac
Showing
3 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import https from "node:https"; | ||
import { URLSearchParams } from "node:url"; | ||
import env from "./env"; | ||
|
||
export interface PushoverMessage { | ||
message: string; | ||
title: string; | ||
url?: string; | ||
url_title?: string; | ||
priority?: number; | ||
sound?: string; | ||
timestamp?: number; | ||
} | ||
|
||
export async function sendNotification(message: PushoverMessage): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const params = new URLSearchParams({ | ||
token: env.PUSHOVER_TOKEN, | ||
user: env.PUSHOVER_USER, | ||
message: message.message, | ||
...(message.title && { title: message.title }), | ||
...(message.url && { url: message.url }), | ||
...(message.url_title && { url_title: message.url_title }), | ||
...(message.priority !== undefined && { | ||
priority: message.priority.toString(), | ||
}), | ||
...(message.sound && { sound: message.sound }), | ||
...(message.timestamp && { timestamp: message.timestamp.toString() }), | ||
}); | ||
|
||
const options = { | ||
hostname: "api.pushover.net", | ||
port: 443, | ||
path: "/1/messages.json", | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Content-Length": params.toString().length, | ||
}, | ||
}; | ||
|
||
const req = https.request(options, (res) => { | ||
let data = ""; | ||
|
||
res.on("data", (chunk) => { | ||
data += chunk; | ||
}); | ||
|
||
res.on("end", () => { | ||
if (res.statusCode === 200) { | ||
resolve(); | ||
} else { | ||
reject( | ||
new Error(`Pushover API returned status code ${res.statusCode}: ${data}`), | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
req.on("error", (error) => { | ||
reject(error); | ||
}); | ||
|
||
req.write(params.toString()); | ||
req.end(); | ||
}); | ||
} |