DEV Community

Cover image for Discord Webhook
Choonho Son
Choonho Son

Posted on

Discord Webhook

Discord webhooks are a powerful way to send messages and updates from external sources into your Discord channels. Whether you're integrating with a CI/CD pipeline, receiving alerts from a monitoring system, or simply automating messages, webhooks are an efficient solution. In this blog post, we'll walk you through the process of creating a webhook in Discord and how to send messages to it.

Step 1. Create Webhook

1.1 Open Your Discord Server
First, navigate to the Discord server where you want to create the webhook. You must have the appropriate permissions to manage webhooks in the channel.

1.2 Access Channel Settings
Go to the specific channel where you want the webhook to send messages. Click on the gear icon (⚙️) next to the channel name to open the channel settings.

Image description

1.3 Navigate to Integrations
In the channel settings, select the "Integrations" tab from the sidebar. Here, you’ll see an option to create a new webhook.

Image description

1.4 Create a Webhook
Click the "Create Webhook" button. You’ll be prompted to set up your webhook:

Image description

Name: Choose a name for your webhook. This will be displayed as the sender of the messages.
Channel: Select the channel where the messages will be sent. By default, it’s the channel you accessed the settings from.
Avatar: Optionally, you can set an avatar for your webhook.

Image description

1.5 Copy Webhook URL
After setting up your webhook, click the "Copy Webhook URL" button. This URL is crucial as it will be used to send messages to this webhook.

Image description

Step 2: Sending Messages to the Webhook

Now that you have your webhook URL, you can send messages to it using a simple HTTP POST request. Here's a basic example using Python and the requests library.

import requests

webhook_url = 'YOUR_WEBHOOK_URL'

embed = {
    "title": "Sample Embed",
    "description": "This is an example of an embedded message.",
    "color": 16711680,  # Red color in RGB
    "fields": [
        {"name": "Field 1", "value": "This is field 1", "inline": True},
        {"name": "Field 2", "value": "This is field 2", "inline": True},
    ]
}

data = {
    "content": "This message contains an embed.",
    "embeds": [embed]
}

response = requests.post(webhook_url, json=data)

if response.status_code == 204:
    print("Message sent successfully!")
else:
    print(f"Failed to send message: {response.status_code}")
Enter fullscreen mode Exit fullscreen mode

Output looks like

Image description

References

https://discord.com/developers/docs/resources/channel#embed-object

Top comments (0)