Send the error to discord

Send the error to a discord webhook channel

Script· failure discord Verified

by rubenfiszel · 1/27/2023

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 days ago
1
//native
2

3
type DiscordWebhook = {
4
  webhook_url: string;
5
};
6
export async function main(
7
  discord_webhook: DiscordWebhook,
8
  message: string,
9
  name: string,
10
) {
11
  const flow_id = Bun.env.WM_FLOW_JOB_ID;
12
  message = `Flow [${flow_id}](${Bun.env.WM_BASE_URL}/run/${flow_id}?workspace=${Bun.env.WM_WORKSPACE}) had an error:\n${name}: ${message}`;
13
  const response = await fetch(`${discord_webhook.webhook_url}?wait=true`, {
14
    method: "POST",
15
    headers: {
16
      "Content-Type": "application/json",
17
    },
18
    body: JSON.stringify({ content: message }),
19
  });
20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`);
22
  }
23
  return await response.json();
24
}
25

Other submissions
  • Submitted by rubenfiszel Deno
    Created 396 days ago
    1
    import discordwebhook from "https://deno.land/x/discordwebhook/mod.ts";
    2
    
    
    3
    type DiscordWebhook = {
    4
      webhook_url: string;
    5
    };
    6
    export async function main(
    7
      discord_webhook: DiscordWebhook,
    8
      message: string,
    9
      name: string,
    10
    ) {
    11
      const flow_id = Deno.env.get("WM_FLOW_JOB_ID");
    12
      message = `Flow [${flow_id}](${Deno.env.get(
    13
        "WM_BASE_URL",
    14
      )}/run/${flow_id}?workspace=${Deno.env.get(
    15
        "WM_WORKSPACE",
    16
      )}) had an error:\n${name}: ${message}`;
    17
      const webhook = new discordwebhook(discord_webhook.webhook_url);
    18
      const ret = await webhook.createMessage(message);
    19
      return ret;
    20
    }
    21