-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications.py
63 lines (56 loc) · 2.19 KB
/
notifications.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
from typing import TYPE_CHECKING, Any, List
from twilio.rest import Client
import __init__
from data.Fleet import Fleet
import os
from consts import StatusCode
CLIENT = Client(os.getenv("TWILIO_SID"), os.getenv("TWILIO_AUTH"))
'''
Sends the message to the phone
'''
def send_message(text, np):
try:
CLIENT.messages.create(to=np.get_owner_phone(),
from_=os.getenv("TWILIO_PH"),
body=text)
except Exception as e:
print(f"\nAn error occured: {e}")
return False
print("Message was sent successfully!")
return True
'''
Takes any data given to it and formats it into the proper message
to send via text.
'''
def format_message(status_code: StatusCode, data: Any, np):
if status_code == StatusCode.ENEMY:
# Edge case where you are truly fucked and receiving pincer attacks
if len(data) == 1:
single_enemy: Fleet = data[0]
if send_message(f"(NP) ALERT: {single_enemy.strength} ships have entered scanning range from {single_enemy.get_owner_name()}", np):
return (200, "Message sent!")
else:
return (400, "Error")
elif len(data) > 1:
initial: str = "(NP) ALERT: Multiple fleets have entered scanning range. Details are as follows\n"
for e in data:
initial += f"{e.strength} ships - {e.get_owner_name()}\n"
initial += "Good luck out there. I'll be surprised if anyone ever gets this text. Let me know."
if send_message(initial, np):
return (200, "Message sent!")
else:
return (400, "Error")
elif status_code == StatusCode.FLEET_SHIPS:
pass
elif status_code == StatusCode.FLEET_WATCH:
pass
elif status_code == StatusCode.DAILY:
initial: str = "(NP) DAILY: Here's your daily Neptune's digest:\n"
initial += f"Enemy Fleets: {len(data[0])}\n"
initial += f"Moving Fleets (Inclusive): {len(data[1])}"
if send_message(initial, np):
return (200, "Message sent!")
else:
return (400, "Error")
else:
raise Exception(f'Unknown status code: {status_code}')