Package: discord-webhook
Python Discord webhookEasily send Discord webhooks with Python (also hasasync support)InstallInstall via pip:pip install discord-webhookExamplesBasic WebhookCreate Multiple Instances / Use multiple URLsGet Webhook by IDSend Webhook to threadManage Being Rate LimitedEmbedded ContentEdit Webhook MessageDelete Webhook MessageSend FilesRemove Embeds and FilesAllowed MentionsUse ProxiesTimeoutAsync SupportBasic Webhookfromdiscord_webhookimportDiscordWebhookwebhook=DiscordWebhook(url="your webhook url",content="Webhook Message")response=webhook.execute()Create multiple instancesIf you want to use multiple URLs you need to create multiple instances.fromdiscord_webhookimportDiscordWebhook# you can provide any kwargs except urlwebhook1,webhook2=DiscordWebhook.create_batch(urls=["first url","second url"],content="Webhook Message")response1=webhook1.execute()response2=webhook2.execute()Get Webhook by IDYou can access a webhook that has already been sent by providing the ID.fromdiscord_webhookimportDiscordWebhookwebhook=DiscordWebhook(url="your webhook url",id="your webhook message id")# now you could delete or edit the webhook# ...Send Webhook to threadYou can send a message to an existing thread by settingthread_idor create a new thread in a forum channel by using athread_name.fromdiscord_webhookimportDiscordWebhook# send to an existing threadwebhook=DiscordWebhook(url="your webhook url",thread_id="the thread id")webhook.execute()# create a new thread in a forum channelwebhook=DiscordWebhook(url="your webhook url",thread_name="some-thread-name")webhook.execute()Manage being Rate Limitedfromdiscord_webhookimportDiscordWebhook# if rate_limit_retry is True then in the event that you are being rate# limited by Discord your webhook will automatically be sent once the# rate limit has been liftedwebhook=DiscordWebhook(url="your webhook url",rate_limit_retry=True,content="Webhook Message")response=webhook.execute()Webhook with Embedded Contentfromdiscord_webhookimportDiscordWebhook,DiscordEmbedwebhook=DiscordWebhook(url="your webhook url")# create embed object for webhook# you can set the color as a decimal (color=242424) or hex (color="03b2f8") numberembed=DiscordEmbed(title="Your Title",description="Lorem ipsum dolor sit",color="03b2f8")# add embed object to webhookwebhook.add_embed(embed)response=webhook.execute()fromdiscord_webhookimportDiscordWebhook,DiscordEmbedwebhook=DiscordWebhook(url="your webhook url")# create embed object for webhookembed=DiscordEmbed(title="Your Title",description="Lorem ipsum dolor sit",color="03b2f8")# set authorembed.set_author(name="Author Name",url="author url",icon_url="author icon url")# set imageembed.set_image(url="your image url")# set thumbnailembed.set_thumbnail(url="your thumbnail url")# set footerembed.set_footer(text="Embed Footer Text",icon_url="URL of icon")# set timestamp (default is now) accepted types are int, float and datetimeembed.set_timestamp()# add fields to embedembed.add_embed_field(name="Field 1",value="Lorem ipsum")embed.add_embed_field(name="Field 2",value="dolor sit")# add embed object to webhookwebhook.add_embed(embed)response=webhook.execute()This is another example with embedded contentfromdiscord_webhookimportDiscordWebhook,DiscordEmbedwebhook=DiscordWebhook(url="your webhook url",username="New Webhook Username")embed=DiscordEmbed(title="Embed Title",description="Your Embed Description",color="03b2f8")embed.set_author(name="Author Name",url="https://github.com/lovvskillz",icon_url="https://avatars0.githubusercontent.com/u/14542790")embed.set_footer(text="Embed Footer Text")embed.set_timestamp()embed.add_embed_field(name="Field 1",value="Lorem ipsum")embed.add_embed_field(name="Field 2",value="dolor sit")embed.add_embed_field(name="Field 3",value="amet consetetur")embed.add_embed_field(name="Field 4",value="sadipscing elitr")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 settinginline=Falseas follows:fromdiscord_webhookimportDiscordWebhook,DiscordEmbedwebhook=DiscordWebhook(url="your webhook url",username="New Webhook Username")embed=DiscordEmbed(title="Embed Title",description="Your Embed Description",color="03b2f8")embed.set_author(name="Author Name",url="https://github.com/lovvskillz",icon_url="https://avatars0.githubusercontent.com/u/14542790",)embed.set_footer(text="Embed Footer Text")embed.set_timestamp()# Set `inline=False` for the embed field to occupy the whole lineembed.add_embed_field(name="Field 1",value="Lorem ipsum",inline=False)embed.add_embed_field(name="Field 2",value="dolor sit",inline=False)embed.add_embed_field(name="Field 3",value="amet consetetur")embed.add_embed_field(name="Field 4",value="sadipscing elitr")webhook.add_embed(embed)response=webhook.execute()Edit Webhook Messagesfromdiscord_webhookimportDiscordWebhookfromtimeimportsleepwebhook=DiscordWebhook(url="your webhook url",content="Webhook content before edit")webhook.execute()webhook.content="After Edit"sleep(10)webhook.edit()Delete Webhook Messagesfromdiscord_webhookimportDiscordWebhookfromtimeimportsleepwebhook=DiscordWebhook(url="your webhook url",content="Webhook Content")webhook.execute()sleep(10)webhook.delete()Send Filesfromdiscord_webhookimportDiscordWebhookwebhook=DiscordWebhook(url="your webhook url",username="Webhook with files")# send two imageswithopen("path/to/first/image.jpg","rb")asf:webhook.add_file(file=f.read(),filename="example.jpg")withopen("path/to/second/image.jpg","rb")asf:webhook.add_file(file=f.read(),filename="example2.jpg")response=webhook.execute()You can use uploaded attachments in Embeds:fromdiscord_webhookimportDiscordWebhook,DiscordEmbedwebhook=DiscordWebhook(url="your webhook url")withopen("path/to/image.jpg","rb")asf:webhook.add_file(file=f.read(),filename="example.jpg")embed=DiscordEmbed(title="Embed Title",description="Your Embed Description",color="03b2f8")embed.set_thumbnail(url="attachment://example.jpg")webhook.add_embed(embed)response=webhook.execute()Remove Embeds and Filesfromdiscord_webhookimportDiscordWebhook,DiscordEmbedwebhook=DiscordWebhook(url="your webhook url")withopen("path/to/image.jpg","rb")asf:webhook.add_file(file=f.read(),filename="example.jpg")embed=DiscordEmbed(title="Embed Title",description="Your Embed Description",color="03b2f8")embed.set_thumbnail(url="attachment://example.jpg")webhook.add_embed(embed)response=webhook.execute(remove_embeds=True)# webhook.embeds will be empty after webhook is executed# You could also manually call the function webhook.remove_embeds().remove_file()removes the given filefromdiscord_webhookimportDiscordWebhookwebhook=DiscordWebhook(url="your webhook url",username="Webhook with files")# send two imageswithopen("path/to/first/image.jpg","rb")asf:webhook.add_file(file=f.read(),filename="example.jpg")withopen("path/to/second/image.jpg","rb")asf:webhook.add_file(file=f.read(),filename="example2.jpg")# remove "example.jpg"webhook.remove_file("example.jpg")# only "example2.jpg" is sent to the webhookresponse=webhook.execute()Allowed MentionsLook into theDiscord Docsfor examples and for more explanationThis example would only ping user123and124but not everyone else.fromdiscord_webhookimportDiscordWebhookcontent="@everyone say hello to our new friends <@123> and <@124>"allowed_mentions={"parse":["everyone"],"users":["123","124"]}webhook=DiscordWebhook(url="your webhook url",content=content,allowed_mentions=allowed_mentions)response=webhook.execute()Use Proxiesfromdiscord_webhookimportDiscordWebhookproxies={"http":"http://10.10.1.10:3128","https":"http://10.10.1.10:1080",}webhook=DiscordWebhook(url="your webhook url",content="Webhook Message",proxies=proxies)response=webhook.execute()orfromdiscord_webhookimportDiscordWebhookproxies={"http":"http://10.10.1.10:3128","https":"http://10.10.1.10:1080",}webhook=DiscordWebhook(url="your webhook url",content="Webhook Message")webhook.set_proxies(proxies)response=webhook.execute()Timeoutfromrequests.exceptionsimportTimeoutfromdiscord_webhookimportDiscordWebhook,DiscordEmbed# We will set ridiculously low timeout threshold for testing purposeswebhook=DiscordWebhook(url="your webhook url",timeout=0.1)# You can also set timeout later using# webhook.timeout = 0.1embed=DiscordEmbed(title="Embed Title",description="Your Embed Description",color="03b2f8")webhook.add_embed(embed)# Handle timeout exceptiontry:response=webhook.execute()exceptTimeoutaserr:print(f"Oops! Connection to Discord timed out:{err}")Async supportIn order to use the async version, you need to install the package using:pip install discord-webhook[async]Example usage:importasynciofromdiscord_webhookimportAsyncDiscordWebhookasyncdefsend_webhook(message):webhook=AsyncDiscordWebhook(url="your webhook url",content=message)awaitwebhook.execute()asyncdefmain():awaitasyncio.gather(send_webhook("Async webhook message 1"),send_webhook("Async webhook message 2"),)# sends both messages asynchronouslyasyncio.run(main())Use CLIusage: discord_webhook [-h] -u URL [URL ...] -c CONTENT [--username USERNAME]
                       [--avatar_url AVATAR_URL]

Trigger discord webhook(s).

optional arguments:
  -h, --help            show this help message and exit
  -u URL [URL ...], --url URL [URL ...]
                        Webhook(s) url(s)
  -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 webhookDevelopmentDev SetupThis project usesPoetryfor dependency management and packaging.Install Poetry and add Poetry toPath.Debian / Ubuntu / Maccurl -sSL https://install.python-poetry.org | python3 -Windowsopen powershell and run:(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -Install dependencies:poetry installInstall the defined pre-commit hooks:poetry run pre-commit installActivate the virtualenv:poetry shell