Readme

Send a Discord message

Send a message to a Discord channel from Val Town. It's useful for notifying your team or community when someone interesting happens, like a user signup, Stripe payment, mention on social media, etc.

import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook"; await discordWebhook({ url: Deno.env.get("engDiscord"), content: "Hi from val town!", });

Example val: https://www.val.town/v/stevekrouse.discordWebhookEx

Setup

1. Create a Discord Webhook

Follow the instructions here: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks

It really only takes 2 minutes.

2. Copy webhook URL

Paste it into your secrets as discordWebhook.

3. Send a message!

import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook"; await discordWebhook({ url: Deno.env.get("engDiscord"), content: "Hi from val town!", });

Example val: https://www.val.town/v/stevekrouse.discordWebhookEx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=5"; // pin to proxied fetch
export const discordWebhook = async ({
url,
content,
}: {
url: string;
content: string;
}) => {
const text = await fetchText(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: content.slice(0, 2000) }),
});
if (text.length) throw Error("Discord Webhook error: " + text);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v6
January 5, 2024