Skip to content

Instantly share code, notes, and snippets.

@Bilka2
Last active April 26, 2024 08:29
Show Gist options
  • Save Bilka2/5dd2ca2b6e9f3573e0c2defe5d3031b2 to your computer and use it in GitHub Desktop.
Save Bilka2/5dd2ca2b6e9f3573e0c2defe5d3031b2 to your computer and use it in GitHub Desktop.
Simple discord webhook with python
import requests # dependency
url = "<your url>" # webhook url, from here: https://i.imgur.com/f9XnAew.png
# for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
data = {
"content" : "message content",
"username" : "custom username"
}
# leave this out if you dont want an embed
# for all params, see https://discordapp.com/developers/docs/resources/channel#embed-object
data["embeds"] = [
{
"description" : "text in embed",
"title" : "embed title"
}
]
result = requests.post(url, json = data)
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
else:
print(f"Payload delivered successfully, code {result.status_code}.")
# result: https://i.imgur.com/DRqXQzA.png
@BluCobalt
Copy link

As a noob in http requests and just python in general, this is really awesome, thank you

@muhammad-k
Copy link

Just want to thank you, I ended up modifying this script for my own webhook!

@reydus
Copy link

reydus commented Apr 14, 2020

Noobs assemble! Same as the people above, thanks alot for this!!

@MaheshReddy
Copy link

Thanks man, this was super useful and simple to use.

@fadedmax
Copy link

fadedmax commented Aug 7, 2020

thanks for making, super simple, easy to use. :)

@4teraflops
Copy link

thanks!

@restockflippaz
Copy link

tysm

@laundmo
Copy link

laundmo commented Jan 1, 2021

this does multiple things in a weird way that it doesn't need to, like assigning to a dict after creating it and manually dumping JSON instead of letting requests handle it.

I recommend this simpler variant:

import requests

url = "<your webhook url>"

embed = {
    "description": "text in embed",
    "title": "embed title"
    }

data = {
    "content": "message content",
    "username": "custom username",
    "embeds": [
        embed
        ],
}

headers = {
    "Content-Type": "application/json"
}

result = requests.post(url, json=data, headers=headers)
if 200 <= result.status_code < 300:
    print(f"Webhook sent {result.status_code}")
else:
    print(f"Not sent with {result.status_code}, response:\n{result.json()}")

@z0ned-out
Copy link

z0ned-out commented Jan 5, 2021

i am new to this and this helped a lot, but i just found out that you don't really need to define the headers or pass them as a parameter if you are passing the data into JSON, like laundmo did just above. the code defined below would just work fine as the one above.

`import requests

url = "put webhook url"

embed = {
    "description": "text in embed",
    "title": "embed title"
    }

data = {
    "content": "message content",
    "username": "custom username",
    "embeds": [
        embed
        ],
}


result = requests.post(url, json=data)
if 200 <= result.status_code < 300:
    print(f"Webhook sent {result.status_code}")
else:
    print(f"Not sent with {result.status_code}, response:\n{result.json()}")

@laundmo
Copy link

laundmo commented Jan 6, 2021

@z0ned-out you're right of course, requests will set the header automatically

@Bilka2
Copy link
Author

Bilka2 commented Jan 6, 2021

Hey, thank you for the recommendations @laundmo and @z0ned-out. I have integrated them into the code and also updated the linked example images for the newest discord layout.

Note that f-strings are not used in the example to preserve compatibility with versions below Python 3.6. With f-strings the last line could be print(f"Payload delivered successfully, code {result.status_code}.").

@laundmo
Copy link

laundmo commented Jan 6, 2021

@Bilka2 Python 3.5 has already reached its End of Life, there is no reason to support it. f-strings are way more performant and readable than .format and as such should ideally be used.

@Bilka2
Copy link
Author

Bilka2 commented Jan 6, 2021

My desire to support pre-3.6 is a good enough reason to keep the code how it is.

@Senoel
Copy link

Senoel commented Jan 20, 2021

Is there a way to attach embed images?

@dilutedev
Copy link

Hey guys, is there a way to send texts that are actually links?
like in html e.g(Text)
I am trying to send shoe sizes as a message to my chanel, and I want to be able to click on those sizes and itll take me to the respective page of each size

@laundmo
Copy link

laundmo commented Feb 16, 2021

@andyias you can use markdown links in the webhook content

[the link](https://example.com)

this is something unique to webhooks, and won't work for normal discord messages.

@Senoel i recommend taking a look at https://leovoel.github.io/embed-visualizer/ (make sure to click on "webhook mode")

you can also take a look at https://birdie0.github.io/discord-webhooks-guide/structure/file.html for sending files without an embed

@Iinksafe
Copy link

very useful

@Deagleus
Copy link

Deagleus commented Aug 6, 2021

very useful, since im a noob with python :D

@15u43i
Copy link

15u43i commented Apr 27, 2022

Neat, thanks!

@Hypurrnating
Copy link

how would one attach a file?
do we just use .read() for the contents? and where does the file name go?

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