-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathalerts.py
More file actions
104 lines (84 loc) · 3.07 KB
/
alerts.py
File metadata and controls
104 lines (84 loc) · 3.07 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import logging
import configurator
import websockets
import requests
logger = logging.getLogger("main")
cfg = configurator.load("settings.json")
async def start_receiving():
logger.info("Connecting to Pushbullet WSS stream")
async with websockets.connect(f"wss://stream.pushbullet.com/websocket/{cfg['pb_token']}") as websocket:
while True:
data = await websocket.recv()
logger.info(f"Received data! :{data}:")
print(f"Received data! :{data}:")
buffer = str(data)
if "push" in buffer and "application_name" in buffer:
await handle_data(buffer)
async def handle_data(data):
json_data = json.loads(data)
name, msg, amount = "", "", ""
if json_data["push"]["application_name"] == "MobilePay" or json_data["push"]["title"] == "MobilePay":
logger.info("Received donation!")
logger.info(f"Raw data: {json_data}")
logger.info("Splitting donation data")
body = json_data["push"]["body"]
body = body.split()
# Amount
logger.info("Getting donation amount")
amount = body[3]
amount = amount.replace(",", ".", 1)
# Name
logger.info("Getting donation name")
if body[7] == "billede":
name = body[9][1:]
else:
name = body[6][1:]
name = name.strip()
# Message
logger.info("Getting donation message")
for i in range(len(body)):
if ":" in body[i]:
msg = body[i+1:]
msg = " ".join(msg)
break
logger.info(f"Donation data: {name} - {amount} - \"{msg}\"")
elif json_data["push"]["title"].lower() == "test notification" and json_data["push"]["application_name"].lower() == "pushbullet":
logger.info("Received test notification via the Pushbullet API")
print("Received test notification via the Pushbullet API")
await test_alert()
else:
logger.info(f"Unknown data received: {json_data}")
if name.strip() == "":
name = cfg["default_name"]
if msg.strip() == "":
msg = cfg["default_msg"]
if amount.strip() == "":
amount = None
return
logger.info("Triggering donation alert")
trigger_alert(name, msg, amount)
def trigger_alert(name, msg, amount):
url = "https://streamlabs.com/api/v1.0/donations"
data = {
"access_token": cfg["sl_token"],
"name": name,
"amount": amount,
"message": msg,
"identifier": "MobilePay",
"currency": "DKK",
}
request(url, data)
async def test_alert():
print("Test alert!")
logger.info("Sending test alert")
url = "https://streamlabs.com/api/v1.0/alerts/send_test_alert"
data = {
"access_token": cfg['sl_token'],
"type": "donation",
}
request(url, data)
def request(url, data):
response = requests.post(url, data)
print(f"Alert request returned: \"{response.text}\"")
logger.info(f"Alert request returned: {response}")