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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export async function notify(request: Request) {
if (request.method === "OPTIONS") {
return new Response("", {
headers: {
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
},
});
}
const body = await request.json();
if (!body.message) {
return Response.error();
}
const resp = await fetch("https://api.pushover.net/1/messages.json", {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
token: Deno.env.get("pushoverToken"),
user: Deno.env.get("pushoverUser"),
message: body.message,
title: body.title ?? "val.town",
}),
});
if (!resp.ok) {
return Response.json({
error: "Failed to send notification. Try again later.",
}, {
status: 503,
});
}
return Response.json({}, {
status: 200,
});
}