Skip to content

Commit

Permalink
Merge pull request #5 from offbyone/doc-tools-python
Browse files Browse the repository at this point in the history
Add examples on using Python for discord webhooks
  • Loading branch information
Birdie0 authored Feb 3, 2025
2 parents 69d6743 + 9defac2 commit cebc551
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This guide includes:
* [Insomnia](tools/insomnia.md)
* [curl](tools/curl.md)
* [HTTPie](tools/httpie.md)
* [Python](tools/python.md)
* Examples
* Other things :)

Expand Down
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* [Insomnia](tools/insomnia.md)
* [curl](tools/curl.md)
* [HTTPie](tools/httpie.md)
* [Python](tools/python.md)

# Examples

Expand Down
1 change: 1 addition & 0 deletions docs/structure/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ payload_json={"embeds":[{"image":{"url":"attachment://pizza.jpg"}}]}
* [HTTPie](../tools/httpie.md#sending-attachments)
* [Insomnia](../tools/insomnia.md#sending-attachments)
* [Postman](../tools/postman.md#sending-attachments)
* [Python](../tools/python.md#sending-attachments)
46 changes: 46 additions & 0 deletions docs/tools/python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Python

[Python](https://python.org) has many HTTP libraries that are able to generate webhook requests. While there's no way to list all of them, here are some examples you can try.

## httpx

The [httpx](https://www.python-httpx.org/) client library can be used to submit both simple and more complex webhooks.

### Send a payload as json

```python
import httpx

response = httpx.post(
"https://discord.com/api/webhooks/123/w3bh00k_t0k3n",
json={
"username": "Captain Hook",
"content": "Ahoy matey! I be a webhook message!",
# you can add embeds here, too
}
)
# raise an exception if the operation failed.
response.raise_for_status()
```

### Sending Attachments

```python
import json
import httpx

with open("image.png", "rb") as io_object:
response = httpx.post(
"https://discord.com/api/webhooks/123/w3bh00k_t0k3n",
data={"payload_json": json.dumps({
"embeds": [{
"title": "Hello, world!",
"description": "This is a message with an image",
"image": {"url": "attachment://filename"}
}]
})},
files={"filetag": ("filename", io_object, "image/png")}
)
# raise an exception if the operation failed.
response.raise_for_status()
```

0 comments on commit cebc551

Please sign in to comment.