Discord
Easily send messages to a Discord server using webhooks!
For streamlined usage of Discord inbound webhooks, there is a discord
method under env.webhooks
. This allows you to provide the inbound webhook URL along with a message to easily post a message in a Discord channel.
If you’re not familiar with Discord webhooks, refer to Discord’s introduction to web hooks article.
Signature
discord(
url: string,
message: string | Webhooks.Discord.Payload
): Promise<{ ok: boolean; response?: any; }>
Parameters
- url: string
The url
should be the Discord inbound webhook URL you received when creating a webhook. We recommend storing this as a encrypted variable for your Repeat.
- message: string | Webhooks.Discord.Payload
The
message
can be astring
or you can provide aWebhooks.Discord.Payload
object. This object allows you to utilise more advanced features such asembeds
or overriding theusername
&avatar
of the webhook. Refer to Discord’s developer documentation for the other supported properties.
Returns
- ok: boolean
A boolean
indicating if the request was successful (status code within the range of 200
- 299
.)
- response: Response
If the request was successful, the body of the response as plain-text.
Examples
Post a message whenever you receive an email
This is a simple Repeat that you can use to alert you when you receive a new email.
export default {
async email(email: Repeat.Email, env: Repeat.Env) {
// things
const message = `Received ${email.subject} from ${email.from} at ${Date.now().toTimeString()}.`;
await env.webhooks.discord(env.variables.DISCORD_WEBHOOK_URL, message);
// other things
},
};
The resulting message in Discord will look like this:
Customising the payload
In the above example, we sent a simple plain-text message - we can also send a JSON payload containing various other properties that are supported by Discord. Let’s modify the Repeat to use a different avatar and also use rich embeds instead of plain text.
export default {
async email(email: Repeat.Email, env: Repeat.Env) {
// things
const message: Webhooks.Discord.Payload = {
// @ts-ignore
avatar_url: 'https://dash.repeat.dev/favicon-16x16.png',
embeds: [
{
title: 'New email received!',
fields: [
{
name: 'Subject',
value: email.subject,
},
{
name: 'From',
value: email.from,
},
{
name: 'Date',
value: new Date().toISOString(),
},
],
},
],
};
await env.webhooks.discord(env.variables.DISCORD_WEBHOOK_URL, message);
// other things
},
};
If we deploy our changes & run our Repeat, we should now get a message that looks like this:
Last updated on November 23, 2022