-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathdiscord_webhook.py
27 lines (22 loc) · 999 Bytes
/
discord_webhook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import requests
def send_to_discord(message: str, webhook_url: str) -> bool:
"""
Sends a message to a Discord channel using a webhook URL.
Args:
message (str): The message to send to the Discord channel.
webhook_url (str): The Discord webhook URL for the channel to send the message to.
Returns:
bool: True if the message was successfully sent, False otherwise.
"""
try:
# Define the JSON payload to send to the webhook URL.
payload = {"content": message}
# Make a POST request to the webhook URL with the JSON payload.
response = requests.post(webhook_url, json=payload)
# Check the response status code and return True if it was successful.
response.raise_for_status()
return True
except Exception as e:
# If there was an error sending the message, print the error message and return False.
print(f"Error sending message to Discord channel: {e}")
return False