-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_test.py
More file actions
48 lines (40 loc) · 1.49 KB
/
Copy pathwebhook_test.py
File metadata and controls
48 lines (40 loc) · 1.49 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import http
import json
import requests
import os
from discord_webhook import DiscordWebhook, DiscordEmbed
import dotenv
dotenv.load_dotenv()
HOOK_URL=os.getenv("DISCORD_WEBHOOK_URL")
def send_discord_message(webhook_url, content, embed_title=None, embed_description=None):
"""
Send a message to a Discord channel via webhook.
:param webhook_url: The Discord webhook URL (string)
:param content: Plain text message (string)
:param embed_title: Optional embed title (string)
:param embed_description: Optional embed description (string)
"""
try:
# Create webhook object
webhook = DiscordWebhook(url=webhook_url, content=content)
# If embed data is provided, add it
if embed_title or embed_description:
embed = DiscordEmbed(title=embed_title or "", description=embed_description or "", color=0x00ff00)
webhook.add_embed(embed)
# Execute webhook
response = webhook.execute()
# Check response
if response.status_code in (200, 204):
print("✅ Message sent successfully!")
else:
print(f"⚠️ Failed to send message. HTTP {response.status_code}: {response.text}")
except Exception as e:
print(f"❌ Error: {e}")
# Example usage
if __name__ == "__main__":
send_discord_message(
webhook_url=HOOK_URL,
content="Hello from Python! 🚀",
embed_title="Sample Embed",
embed_description="This is an example embed message."
)