Skip to content

Discord channel webhook

python
import requests

WEBHOOK_URL = ''

def post_data(url, data, headers=None):
    if headers is None:
        headers = {"Content-Type": "application/json"}
    try:
        response = requests.post(url, json=data, headers=headers)
        response.raise_for_status()  # Raise HTTPError for bad requests (4XX or 5XX)
        return response
    except requests.exceptions.HTTPError as e:
        print('Request failed. Status code:', e.response.status_code)
        raise
    except requests.exceptions.RequestException as e:
        print('Request failed due to an error:', e)
        raise

def send_discord_webhook(message, webhook_url=WEBHOOK_URL):
    data = {'content': message}
    try:
        post_data(webhook_url, data)
        print('Message successfully sent to Discord Webhook!')
    except requests.exceptions.RequestException:
        print('Message failed to send.')

Released under the MIT License.