Skip to content

Instantly share code, notes, and snippets.

@danrspencer
Created August 8, 2023 08:06
Show Gist options
  • Save danrspencer/00e6e7963bb77d439b59db9893e016c2 to your computer and use it in GitHub Desktop.
Save danrspencer/00e6e7963bb77d439b59db9893e016c2 to your computer and use it in GitHub Desktop.
import {
DefineFunction,
DefineType,
Schema,
SlackFunction,
} from "deno-slack-sdk/mod.ts";
export const AlertCustomType = DefineType({
name: "Alert",
type: Schema.types.object,
properties: {
status: {
type: Schema.types.string,
description:
"Status of the individual alert, can be 'resolved' or 'firing'",
enum: ["resolved", "firing"],
},
labels: {
type: Schema.types.object,
description: "Labels for the individual alert",
},
annotations: {
type: Schema.types.object,
description: "Annotations for the individual alert",
},
startsAt: {
type: Schema.types.string,
description: "Start time of the alert in RFC3339 format",
},
endsAt: {
type: Schema.types.string,
description: "End time of the alert in RFC3339 format",
},
generatorURL: {
type: Schema.types.string,
description: "Identifies the entity that caused the alert",
},
fingerprint: {
type: Schema.types.string,
description: "Fingerprint to identify the alert",
},
},
required: [
"status",
"labels",
"annotations",
"startsAt",
"endsAt",
"generatorURL",
"fingerprint",
],
});
/**
* Functions are reusable building blocks of automation that accept
* inputs, perform calculations, and provide outputs. Functions can
* be used independently or as steps in workflows.
* https://api.slack.com/automation/functions/custom
*/
export const GrafanaWebhookFunctionDefinition = DefineFunction({
callback_id: "grafana_webhook_function",
title: "Grafana Webhook function",
description: "A sample function",
source_file: "functions/grafana_webhook_function.ts",
input_parameters: {
properties: {
version: {
type: Schema.types.string,
description: "Version of the alert schema",
},
groupKey: {
type: Schema.types.string,
description:
"Key identifying the group of alerts (e.g. to deduplicate)",
},
truncatedAlerts: {
type: Schema.types.integer,
description: "How many alerts have been truncated due to 'max_alerts'",
},
status: {
type: Schema.types.string,
description: "Status of the alert, can be 'resolved' or 'firing'",
enum: ["resolved", "firing"],
},
receiver: {
type: Schema.types.string,
description: "Receiver of the alert",
},
groupLabels: {
type: Schema.types.object,
description: "Group labels for the alert",
},
commonLabels: {
type: Schema.types.object,
description: "Common labels for the alert",
},
commonAnnotations: {
type: Schema.types.object,
description: "Common annotations for the alert",
},
externalURL: {
type: Schema.types.string,
description: "Backlink to the Alertmanager",
},
alerts: {
type: Schema.types.array,
description: "List of alerts",
items: {
type: AlertCustomType,
},
},
},
required: [
"version",
"groupKey",
"truncatedAlerts",
"status",
"receiver",
"groupLabels",
"commonLabels",
"commonAnnotations",
"externalURL",
"alerts",
],
},
output_parameters: {
properties: {
message: {
type: Schema.types.string,
description: "Message that was sent",
},
user: {
type: Schema.slack.types.user_id,
description: "User the message was sent to",
},
},
required: ["message", "user"],
},
});
/**
* SlackFunction takes in two arguments: the CustomFunction
* definition (see above), as well as a function that contains
* handler logic that's run when the function is executed.
* https://api.slack.com/automation/functions/custom
*/
export default SlackFunction(
GrafanaWebhookFunctionDefinition,
async ({ inputs, client }) => {
// const formattedMessage = `:wave: ` + `<@${user}>` +
// ` submitted the following message: \n\n>${message}`;
await client.chat.postMessage({
channel: "test",
text: "test",
});
// Outputs are made available as variables for use in subsequent functions
return { outputs: { message: "test", user: "test" } };
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment