const CAMPSITE_API_KEY = Deno.env.get("CAMPSITE_API_KEY");
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();
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 });
}
}