Send discord webhooks using python
Project description
RedWebhook
Install
Install using pip:
- pip install RedWebhook
- pip install -U RedWebhook
- python -m pip install -U RedWebhook
Examples
- Basic Webhook
- Manage Discord Rate Limit
- Multiple Webhook Urls
- Embedded Webhook
- Edit Webhook Message
- Delete Webhook Message
- Send Files
- Remove Embeds and Files
- Allowed Mentions
- Use Proxies
- Timeout
basic webhook
from RedWebhook import RedWebhook
webhook = RedWebhook(url="", content="Webhook Message")
response = webhook.execute()
manage discord rate limit
from RedWebhook import RedWebhook
webhook = RedWebhook(
url='<your_webhook_url_here>',
rate_limit_retry=True,
content='Example'
)
response = webhook.execute()
multiple webhook urls
from RedWebhook import RedWebhook
webhooks = ['<webhook_url_1>', '<webhook_url_2>']
webhook = RedWebhook(
url=webhooks,
content='Webhook'
)
response = webhook.execute()
embedded webhook
from RedWebhook import RedWebhook, DiscordEmbed
webhook = RedWebhook(url='<your_webhook_url_here>')
# create embed objects for webhook
embed = DiscordEmbed(
title='<your_title>',
description='<your_description>',
color='<your_color>'
)
# set author
embed.set_author(
name='<author>',
url='<author_url>',
icon_url='<author_icon_url>'
)
# set image
embed.set_image(
url='<your_image_url>',
)
# set thumbnail
embed.set_thumbnail(
url='<your_thumbnail_url>',
)
# set footer
embed.set_thumbnail(
text='<your_footer>'
)
# set timestamp (default is now)
embed.set_timestamp()
# add fields to the embed
embed.add_embed_field(
name='<field_1>',
value='<field_description>'
)
embed.add_embed_field(
name='<field_2>',
value='<field_description>'
)
# add embed object to webhook
webhook.add_embed(embed)
response = webhook.execute()
By default, the embed fields are placed side by side. We can arrange them in a new line by setting inline=False as follows:
from RedWebhook import RedWebhook, DiscordEmbed
webhook = RedWebhook(url='<your_webhook_url_here>')
embed = DiscordEmbed(
title='<your_title>',
description='<your_description>',
color='<your_color>'
)
embed.set_footer(
text='<your_footer>'
)
embed.set_timestamp()
# set `inline=False` for the embed field to come in the whole line
embed.add_embed_field(
name='<field_1>',
value='<field_description>',
inline=False
)
embed.add_embed_field(
name='<field_2>',
value='<field_description>',
inline=False
)
embed.add_embed_field(
name='<field_3>',
value='<field_description>'
)
embed.add_embed_field(
name='<field_3>',
value='<field_description>'
)
edit webhook message
from RedWebhook import RedWebhook
from time import sleep
webhook = RedWebhook(
url='<your_webhook_url_here>',
content='Before edit'
)
sent_webhook = webhook.execute()
webhook.content = 'After edit'
sleep(10)
sent_webhook = webhook.edit(sent_webhook)
delete webhook messages
from RedWebhook import RedWebhook
from time import sleep
webhook = RedWebhook(
url='<your_webhook_url_here>',
content='Before edit'
)
sent_webhook = webhook.execute()
sleep(10)
webhook.delete(sent_webhook)
send files
from RedWebhook import RedWebhook
webhook = RedWebhook(
url='<your_webhook_url_here>',
username='RedWebhook with files'
)
with open('path/to/first/image.jpg', 'rb') as f:
webhook.add_file(file=f.read(), filename='image1.jpg')
with open("path/to/second/image.jpg", "rb") as f:
webhook.add_file(file=f.read(), filename='image2.jpg')
response = webhook.execute()
You can also upload embedded attachments:
from RedWebhook import RedWebhook, DiscordEmbed
webhook = RedWebhook(
url='<your_webhook_url_here>'
)
with open('path/to/image.jpg', 'rb') as f:
webhook.add_file(file=f.read(), filename='attach.jpg')
embed = DiscordEmbed(
title='<your_embed_title>',
description='<your_embed_description>',
color=0x2f3136
)
embed.set_thumbnail(url='attachment://attach.jpg')
webhook.add_embed(embed)
response = webhook.execute()
remove embeds and files
from RedWebhook import RedWebhook, DiscordEmbed
webhook = RedWebhook(
url='<your_webhook_url_here>'
)
with open('path/to/image.jpg', 'rb') as f:
webhook.add_file(file=f.read(), filename='attach.jpg')
embed = DiscordEmbed(
title='<your_embed_title>',
description='<your_embed_description>',
color=0x2f3136
)
embed.set_thumbnail(url='attachment://attach.jpg')
webhook.add_embed(embed)
response = webhook.execute(remove_embeds=True, remove_files=True)
# webhook.files and webhook.embeds will be empty after webhook is executed
# You could also manually call the functions webhook.remove_files() and webhook.remove_embeds()
.remove_file() removes the given file:
from RedWebhook import RedWebhook
webhook = RedWebhook(
url='<your_webhook_url_here>',
username='Webhook files'
)
# send two images
with open('path/to/first/image.jpg', 'rb') as f:
webhook.add_file(file=f.read(), filename='image1.jpg')
with open("path/to/second/image.jpg", "rb") as f:
webhook.add_file(file=f.read(), filename='image2.jpg')
# remove 'image1.jpg'
webhook.remove_file('image1.jpg')
# only 'image2.jpg' is sent to the webhook
response = webhook.execute()
allowed mentions
Look into the Discord Documentation for examples and explanation
This example will only ping user_id_1 and user_id_2, no one else
from RedWebhook import RedWebhook
content = '@everyone say hello to our friends <@user_id_1> and <@user_id_2>'
allowed_mentions = {
'users': ['user_id_1', 'user_id_2]
}
webhook = RedWebhook(
url='<your_webhook_url_here>',
content=content,
allowed_mentions=allowed_mentions
)
response = webhook.execute()
use proxies
from RedWebhook import RedWebhook
proxies = {
'http': 'http://11.11.1.11:6969',
'https': 'https://11.11.1.11:0420'
}
webhook = RedWebhook(
url='<your_webhook_url_here>',
content='Webhook',
proxies=proxies
)
response = webhook.execute()
or
from RedWebhook import RedWebhook
proxies = {
'http': 'http://11.11.1.11:6969',
'https': 'https://11.11.1.11:0420'
}
webhook = RedWebhook(
url='<your_webhook_url_here>',
content='Webhook'
)
webhook.set_proxies(proxies)
response = webhook.execute()
use cli
usage: RedWebhook [-h] -u URL [URL ...] -c CONTENT [--username USERNAME] [--avatar_url AVATAR_URL]
Trigger Red Webhooks
optional arguments:
-h, --help show this help message and exit
-u URL [URL ...], --url URL [URL ...] webhook url/urls
-c CONTENT, --content CONTENT message content
--username USERNAME override the default username of the webhook
--avatar_url AVATAR_URL override the default avatar of the webhook
timeout
from requests.exceptions import Timeout
from RedWebhook import RedWebhook, DiscordEmbed
# we will set ridiculously low timeout threshold for testing purposes
webhook = RedWebhook(
url='<your_webhook_url_here>',
timeout=0.2,
)
# you can also set timeout later using
# webhook.timeout = 0.2
embed = DiscordEmbed(
title='<embed_title>',
description='<embed_description>',
color=0x2f3136
)
webhook.add_embed(embed)
# handle timeout exceptions
try:
response = webhook.execute()
except Timeout as err:
print(f'Beep Boop, Connection to Discord timed out: \n{err}')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file RedWebhook-0.1.0.tar.gz.
File metadata
- Download URL: RedWebhook-0.1.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d13b00d18e70bad867a6b94c5b5835001c2d359aea46ec95ab151f1497f3041f
|
|
| MD5 |
819c1a37c0a6e45fdee732be801e223a
|
|
| BLAKE2b-256 |
443ad8839851fd893dfc2def1735ff157ad6cf7013520a3ce65c1d6b677d740b
|
File details
Details for the file RedWebhook-0.1.0-py3-none-any.whl.
File metadata
- Download URL: RedWebhook-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18d59aa126ba1006e2f6722fdfc9bb11dc89b4443b6d0b994bd21cb718758f37
|
|
| MD5 |
c614d2c88a20f64a2bda5c88291e0f47
|
|
| BLAKE2b-256 |
1caac4ecde2fe3ccd31ad0efe36f1a0b4ea0607d62cf5a49db260b442a98ab5f
|