Skip to content

Instantly share code, notes, and snippets.

@Birdie0
Last active March 6, 2024 13:38
Star You must be signed in to star a gist
Save Birdie0/78ee79402a4301b1faf412ab5f1cdcf9 to your computer and use it in GitHub Desktop.
How to use Discord Webhooks

⚠️ This gist is no longer updated! For maintained, improved and even more extended guide click here.


How to use Discord Webhook

It's a JSON

First, learn JSON. It's not programming language, not even close. Just follow syntax rules and you will be fine.

Recommended sources:

Structure of Webhook Body

Second, learn the structure. All root elements are optional unless stated otherwise. Body have to include content or embeds, otherwise request will fail.

element : [data type] - description

  • [string] - text

    • [website url] - ex.: https://google.com
      • [image url] - ex.: https://www.w3schools.com/html/pic_mountain.jpg
  • [array] - comma-separated elements. ex.: [1, 2, 3, 4]

  • [number] - ex. 1337, 420.69, -1000, etc.

  • [boolean] - can be true or false only.

  • [object] - can include key: values

  • username : [string] - overrides the default username of the webhook

  • avatar_url : [image url] - overrides the default avatar of the webhook

  • content : [string] - simple message, the message contains (up to 2000 characters)

  • embeds : [array] - array of embed objects. That means, you can use more than one in the same body

    • author : [object] - embed author object
      • name : [string] - name of author
      • url : [website url] - url of author. If name was used, it becomes a hyperlink
      • icon_url : [image url] - url of author icon
    • title : [string] - title of embed
    • url : [website url] - url of embed. If title was used, it becomes hyperlink
    • description : [string] - description text
    • color : [number] - color code of the embed. You have to use Decimal numeral system, not Hexadecimal. Use color picker
    • fields : [array] - array of embed field objects
      • name : [string] - name of the field
      • value : [string] - value of the field
      • inline : [boolean] - if true, fields will be displayed in same line, but there can only be 3 max in same line or 2 max if you used thumbnail
    • thumbnail : [object] - embed thumbnail object
      • url : [image url] - url of thumbnail
    • image : [object] - embed image object
      • url : [image url] - image url
    • footer : [object] - embed footer object
      • text : [string] - footer text, doesn't support Markdown
      • icon_url : [image url] - url of footer icon

Example for a webhook

{
  "username": "Webhook",
  "avatar_url": "https://i.imgur.com/4M34hi2.png",
  "content": "Text message. Up to 2000 characters.",
  "embeds": [
    {
      "author": {
        "name": "Birdie♫",
        "url": "https://www.reddit.com/r/cats/",
        "icon_url": "https://i.imgur.com/R66g1Pe.jpg"
      },
      "title": "Title",
      "url": "https://google.com/",
      "description": "Text message. You can use Markdown here. *Italic* **bold** __underline__ ~~strikeout~~ [hyperlink](https://google.com) `code`",
      "color": 15258703,
      "fields": [
        {
          "name": "Text",
          "value": "More text",
          "inline": true
        },
        {
          "name": "Even more text",
          "value": "Yup",
          "inline": true
        },
        {
          "name": "Use `\"inline\": true` parameter, if you want to display fields in the same line.",
          "value": "okay..."
        },
        {
          "name": "Thanks!",
          "value": "You're welcome :wink:"
        }
      ],
      "thumbnail": {
        "url": "https://upload.wikimedia.org/wikipedia/commons/3/38/4-Nature-Wallpapers-2014-1_ukaavUI.jpg"
      },
      "image": {
        "url": "https://upload.wikimedia.org/wikipedia/commons/5/5a/A_picture_from_China_every_day_108.jpg"
      },
      "footer": {
        "text": "Woah! So cool! :smirk:",
        "icon_url": "https://i.imgur.com/fKL31aD.jpg"
      }
    }
  ]
}

And how it looks

example

Getting Started

Account on IFTTT

Visit IFTTT and create an account (if you haven't one).

Webhook on Discord

  1. Go to Server settings -> Webhooks -> Create Webhook
  2. Setup name, avatar and the channel, where it will be posted. Copy Webhook URL. Do not share! Very dangerous!
  3. Click Save and then the Done button

Creating an Applet

If this

  1. Go to My Applets -> New Applet
  2. Click [+]this
  3. Choose a service. In theory, you could use all of them
  4. Choose the trigger. Read the description below every trigger and choose the needed one
  5. Complete trigger fields. There can be more than one step. Read the descriptions and examples

Then that

  1. Click [+]that
  2. Choose the action service. You need Webhooks. Use the search bar
  3. Choose action. Choose Make a web request
  4. Paste your Webhook URL to URL field
  5. Select POST method
  6. Select application/json content type
  7. And now the hardest part™. You need to create your JSON body. Follow the structure, use it as an example and don't forget about common sense. Press + Ingredient button and put the Ingredients into appropriate fields. If something says URL put it in "url":"{{URL}}", if something says ImageURL, try to put that into "image": {"url":"{{ImageURL}}"}. There's no universal solution
  8. Click Create Action and then Finish
  9. Done!

Tips

  • Use this awesome website to generate valid json wirh built-in preview: Discohook, also you can send webhooks to your server with it if you just wan't fancy embed in your channel without any automatization.
  • Don't forget to check your JSON body with a JSON validator. If you don't know any use one of these:
  • If the webhook doesn't work, check log for errors. My Applets -> choose applet -> click gear -> View activity log. Maker error means your JSON body has errors.
  • Discord has built-in embeds for Twitter, Youtube and other sites so you can just add the link to the webhook: {"content": "{{Link}}"}.

Examples

Check https://birdie0.github.io/discord-webhooks-guide Examples section!

@Birdie0
Copy link
Author

Birdie0 commented Mar 25, 2021

you don't need BeautifulSoup, as data that comes from api is json and as i said before html parsing is not possible here as initial html doesn't contain data.
here's script that might fir your needs or help to reach your goal

first, install requests and tabulate packages: pip install -U --user requests tabulate
second, replace put url here in code with webhook url

import requests
from tabulate import tabulate

url = "https://api.playvs.com/graphql"
body = {
	"operationName": "getPhaseResults",
	"variables": {
		"phaseId": "0f4468aa-079b-4391-adbc-1fe971f64059"
	},
	"query": "query getPhaseResults($phaseId: ID!) {\n  phase(id: $phaseId) {\n    id\n    name\n    type\n    phaseResults {\n      id\n      phaseTiebreakerPoints\n      gamesWon\n      gamesLost\n      matchesWon\n      matchesLost\n      team {\n        id\n        name\n        esport {\n          id\n          slug\n          __typename\n        }\n        school {\n          id\n          name\n          logoUrl\n          slug\n          __typename\n        }\n        __typename\n      }\n      record {\n        won\n        lost\n        streak\n        winPercentage\n        matchesWonGamesWonGamesLost\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"
}
r = requests.post(url, json=body)
data = r.json()["data"]["phase"]["phaseResults"][0:10]
table_data = [[
  i + 1,
  x["team"]["name"],
  x["matchesWon"],
  x["matchesLost"],
  f"{x['gamesWon']}-{x['gamesLost']}",
  x["record"]["streak"],
  x["phaseTiebreakerPoints"]
] for i,x in enumerate(data)]
headers = ["#", "Team", "Matches Won", "Matches Lost", "Game Record", "Win Streak", "Tiebreaker Points"]

webhook_url = "put url here"
webhook_body = {
  "content": f"```\n{tabulate(table_data, headers=headers)}```"
}
requests.post(webhook_url, json=webhook_body)
result:

image

data from site for reference:

image

@blackdackota
Copy link

Alright, thank you.

@rawr37
Copy link

rawr37 commented Apr 7, 2021

can you make it so that it posts at a certain time and has different content each day (I'm doing it for a daily update thing, but everyday the content is different based on a weekly schedule (webtoon))

@Birdie0
Copy link
Author

Birdie0 commented Apr 9, 2021

@rawr37 on ifttt with filter code you can make it choose image depending on current day from predefined list, if you want data come from somewhere outside it need you to parse website by yourself and make some script to send it with cron or similar stuff like task manager on windows.

@Z0achary
Copy link

how do I ping someone using webhook message?

@Username#tag

@Birdie0
Copy link
Author

Birdie0 commented Sep 12, 2021

how do I ping someone using webhook message?

@Username#tag

<@id> reference

@WindexProjects
Copy link

how do I ping someone using webhook message?

You can use <@(Their ID)> in the content message

@RogueRaven26
Copy link

What do I put in for Event Name???

@Birdie0
Copy link
Author

Birdie0 commented Oct 10, 2021

@RogueRaven26 IFTTT's incoming/if Webhook has nothing to do with Discord Webhooks, which is used in action/then part.

@Jossefht220
Copy link

The truth is I don't know what I'm doing haha, I don't know if the parameter is right or is everything wrong, I don't know if you can give me a hand please.
2
1

@Birdie0
Copy link
Author

Birdie0 commented Dec 4, 2021

@Jossefht220 sorry, don't know what to fix here, what the programming language is that?

@Jossefht220
Copy link

@Birdie0 c# in windows forms

@Birdie0
Copy link
Author

Birdie0 commented Dec 5, 2021

@Jossefht220 hmm, you might try checking Insomnia REST client, it has code generator, including C# for resharp and httpclient libraries. If that suits you try it.

@moz9
Copy link

moz9 commented Mar 25, 2022

How do I make an image (preview tweet) or images attached to a tweet, or a video (if it's a youtube link) attached?
Here is my code:
{ "avatar_url": "https://avatar-resolver.vercel.app/twitter/{{UserName}}", "content":"", "icon_url":"", "embeds": [{ "author": { "name": "{{UserName}}", "url": "{{LinkToTweet}}", "icon_url": "https://avatar-resolver.vercel.app/twitter/{{UserName}}" }, "color": 1942002, "description":"<<<{{Text}}>>>" }] }

and here is how it looks like:
image

@Birdie0
Copy link
Author

Birdie0 commented Mar 25, 2022

Hi, there's no way to do that with custom embed, try using native one, which can show tweet images (including when there's more than one) and videos:

{
  "avatar_url": "https://avatar-resolver.vercel.app/twitter/{{UserName}}",
  "content": "{{LinkToTweet}}"
}

but that still won't work with external videos, the workaround for this is quite ugly, it's putting tweet content in message directly, so Discord would generate embeds for them:

{
  "avatar_url": "https://avatar-resolver.vercel.app/twitter/{{UserName}}",
  "content": "<<<{{Text}}>>>"
}

@moz9
Copy link

moz9 commented Mar 25, 2022

Birdie0, thanks for the reply. I tried the first option - it just inserts a link over the body of the "embeds" array, but for some reason there is no embedding on the link, that's the problem. The feeling that the webhook does not have enough permission to embed or something restricts it from embedding attachments by link, but so only through webhooks. If you try using the IFTTT: twitter to discard scheme, then on the contrary there are too many attachments, and it looks terrible.
The second method is using

{
"avatar_url": "https://avatar-resolver.vercel.app/twitter/{{UserName}}",
"content": "<<<{{Text}}><{{Text}}>><{{Text}}>>><{{Text}}>>"
}

also did not give results

@Birdie0
Copy link
Author

Birdie0 commented Mar 25, 2022

Using embeds block prevents generating automatic ones, also why you copied text it over and over?

Only solution that might work I have on my mind is using filter code to filter out links from tweet and then append them to content so both tweet and links in it get embed, but there's a problem that tweet images also included in text and they're shortened with t.co, so distinguish them is not possible. Appending them will result in duplicate embeds.

@moz9
Copy link

moz9 commented Mar 25, 2022

Birdie0, but after all, some bots somehow do this, for example Tweetcord
image

@Birdie0
Copy link
Author

Birdie0 commented Mar 25, 2022

Tweetcord uses Twitter API directly, building rich embeds is not a problem for them, IFTTT provides preselected number of ingredients which doesn't allow to reproduce same result. Services like Pipedream or Integromat/Make give direct access to API response, that comes with flexibility also with certain difficulty of configuration if you don't know what you're doing.

@moz9
Copy link

moz9 commented Mar 25, 2022

Birdie0 I see. thank you for the information and attention

@floatbeta
Copy link

It there a way to GET from a Discord webhook?

I created a form that gets filled out and sent to a Discord channel, now I need it to print back to the website, how can I do it?

My website is static html.

@Birdie0
Copy link
Author

Birdie0 commented Apr 21, 2022

@floatbeta webhook is one-way message sending for such things you likely need bot to access message from channel, also storage so you don't refetch same message over on over, and something that will handle requests from page (can be server or you can use as vercel/netlify's serverless functions as alternaive), so no credentials (including webhook url is revealed). static websites is not limit for loading dynamic data, there's bunch of js frameworks allowing to organize that.

@maulsh
Copy link

maulsh commented Jul 19, 2022

How to add fields

@Birdie0
Copy link
Author

Birdie0 commented Jul 19, 2022

@itsEci there's fields block for that, could you provide some more info about what you're trying to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment