Skip to content

Instantly share code, notes, and snippets.

@NuroDev
Last active October 16, 2025 00:39
Show Gist options
  • Save NuroDev/6f1c62f96c37b7be5901d3386dcf7087 to your computer and use it in GitHub Desktop.
Save NuroDev/6f1c62f96c37b7be5901d3386dcf7087 to your computer and use it in GitHub Desktop.
🚦 UniFi Relay | Remap UniFi Network alert events & forward them to Discord
export default {
fetch: async (request, env) => {
if (request.method !== 'POST')
return new Response('Method Not Allowed', { status: 405 });
const json = await request
.json<{
alarm_id: string;
events: Array<{
alert_id: string;
alert_key: string;
id: string;
scope: {
site_id: string;
};
}>;
}>()
.catch(() => null);
if (!json || typeof json !== 'object')
return new Response('Bad Request', { status: 400 });
const events = Array.isArray(json.events) ? json.events : null;
if (!events) return new Response('Bad Request', { status: 400 });
try {
const discordResponse = await fetch(env.DISCORD_WEBHOOK_URL, {
body: JSON.stringify({
embeds: events.map((event) => ({
title: 'UniFi Network Alert',
color: 0x4797ff,
fields: [
{
name: 'Alert Key',
value: event.alert_key,
inline: false,
},
{
name: 'Alert ID',
value: event.alert_id,
inline: false,
},
{
name: 'ID',
value: event.id,
inline: false,
},
],
footer: {
text: 'UniFi',
icon_url:
'https://pbs.twimg.com/profile_images/1610157462321254402/tMCv8T-y_400x400.png',
},
timestamp: new Date().toISOString(),
})),
}),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
});
if (!discordResponse.ok) throw discordResponse;
return Response.json(
{
data: null,
error: null,
},
{
status: 200,
},
);
} catch (error) {
console.error('Error sending to Discord:', error);
return Response.json(
{
data: null,
error: {
message: 'Failed to send alert to Discord',
},
},
{
status: 500,
},
);
}
},
} satisfies ExportedHandler<Env>;
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "unifi-relay",
"main": "src/index.ts",
"compatibility_date": "2025-10-01",
"observability": {
"enabled": true,
"logs": {
"enabled": true
}
},
"vars": {
"DISCORD_WEBHOOK_URL": "YOUR_DISCORD_WEBHOOK_URL"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment