Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

This webhook handler sends messages in the specificed thread in response to events from Axiom, a log monitoring service,

Campsite API Documentation: https://campsite.com/docs

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
/**
* Axiom Notifier
*
* This webhook handler sends messages in the specificed thread in response to events
* from Axiom [1], a log monitoring service,
*
* [1] https://axiom.co
*
* Campsite API docs:
* https://campsite.com/docs
*/
// These environment variables are required:
const CAMPSITE_API_KEY = Deno.env.get("CAMPSITE_API_KEY");
// Set these to your own values:
const CAMPSITE_ALERTS_THREAD_ID = "sdcup465jb7y";
export default async function server(request: Request): Promise<Response> {
if (request.method !== "POST") {
return new Response("Method not allowed", { status: 405 });
}
let payload;
try {
payload = await request.json();
// Differentiate between match monitors vs. threshold monitors
// https://axiom.co/docs/monitor-data/monitors
const isMonitor = payload.event.body.startsWith("Current value");
const status = isMonitor
? (
payload.action === "Open" ? "🔴 Monitor triggered" : "🟢 Monitor resolved"
)
: "Match triggered";
const message = `**${status}: ${payload.event.title}**\n${payload.event.body}`;
const campsiteResponse = await fetch(
`https://api.campsite.com/v2/threads/${CAMPSITE_ALERTS_THREAD_ID}/messages`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${CAMPSITE_API_KEY}`,
},
body: JSON.stringify({
content_markdown: message,
}),
},
);
if (!campsiteResponse.ok) {
throw new Error(`Campsite API error: ${campsiteResponse.statusText}`);
}
return new Response("OK", { status: 200 });
} catch (error) {
console.error("Error posting to Campsite:", error);
console.log("Payload:", payload);
return new Response("Error posting to Campsite", { status: 500 });
}
}
campsite-axiomnotifier.web.val.run
September 18, 2024