-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_messenger.py
65 lines (54 loc) · 2.33 KB
/
discord_messenger.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
import os
from utils import take_screenshot_of_app, load_yaml_config
# Load webhooks from config.yaml
def load_webhooks():
config = load_yaml_config()
return config['webhooks']
# Send message to Discord
def send_message_to_discord(message, noimage, win, debug):
screenshot_path = None
if not noimage:
# Take a screenshot and return the temporary file path
screenshot_path = take_screenshot_of_app("Trade Automation Toolbox", win)
message_ids = []
webhooks = load_webhooks() # Load webhooks from config.yaml
for webhook in webhooks:
url = webhook["url"]
thread_id = webhook.get("thread_id")
if thread_id:
url += f"?thread_id={thread_id}"
payload = {"content": message}
response = None # Initialize response variable
try:
if screenshot_path and os.path.isfile(screenshot_path):
with open(screenshot_path, "rb") as image_file:
files = {"file": (os.path.basename(screenshot_path), image_file)}
response = requests.post(url, data=payload, files=files)
else:
response = requests.post(url, data=payload)
# Check if response is successful
if response.status_code not in [200, 204]:
print(f"Failed to send message to webhook {url}. Status code: {response.status_code}")
print(f"Response: {response.text}")
message_ids.append(None)
else:
try:
response_data = response.json()
message_id = response_data.get('id')
message_ids.append(message_id)
except ValueError:
message_ids.append(None)
except Exception as e:
print(f"Error while sending message to {url}: {e}")
message_ids.append(None)
# Clean up the temporary screenshot file after all webhooks have been processed
if screenshot_path and os.path.exists(screenshot_path):
os.remove(screenshot_path)
return message_ids
# Delete messages from Discord
def delete_messages(message_ids):
webhooks = load_webhooks() # Load webhooks from config.yaml
for msg_id in message_ids:
url = f"{webhooks[0]['url']}/messages/{msg_id}"
requests.delete(url)