-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.py
158 lines (122 loc) · 4.09 KB
/
defs.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
import Credentials
import requests
import json
import mimetypes
# create base twilio client
twilio_client = Client(Credentials.twilio_get_sid(), Credentials.twilio_get_auth())
dummy_list = []
# main message class
class Message:
message_type = "" # sms or mms
content = "" # message body
sid = "" # ID of message
number = "" # number of who sent message
# this is for internal use only
beta_uri = ""
# respond with sms
def send_sms(self, content):
twilio_client.messages \
.create(
body=content,
from_=Credentials.twilio_get_number(),
to=self.number
)
# respond with mms
def send_mms(self, content, url):
twilio_client.messages \
.create(
body=content,
from_=Credentials.twilio_get_number(),
media_url=[url],
to=self.number
)
# delete message
def mark_as_read(self):
try:
twilio_client.messages(self.sid).delete()
except TwilioRestException:
if self.sid not in dummy_list:
dummy_list.append(self.sid)
# twilio_client.messages(self.sid).update(body="")
# download message
def MMS_mv(self, file_name):
string1 = f"https://api.twilio.com{self.beta_uri}"
string1 = f"{string1[:-5]}/Media{string1[-5:]}"
r = requests.get(string1, auth=(Credentials.twilio_get_sid(), Credentials.twilio_get_auth()))
r = json.loads(r.text)
r = r["media_list"][0]
sid = r["sid"]
mime_type = r["content_type"]
# print(r["content_type"])
media_url = f"{string1[:-5]}/{sid}"
r2 = requests.get(media_url)
with open(f'{file_name}{mimetypes.guess_extension(mime_type, strict=True)}', 'wb') as handler:
handler.write(r2.content)
def MMS_raw_data(self):
string1 = f"https://api.twilio.com{self.beta_uri}"
string1 = f"{string1[:-5]}/Media{string1[-5:]}"
r = requests.get(string1, auth=(Credentials.twilio_get_sid(), Credentials.twilio_get_auth()))
r = json.loads(r.text)
r = r["media_list"][0]
sid = r["sid"]
mime_type = r["content_type"]
media_url = f"{string1[:-5]}/{sid}"
r2 = requests.get(media_url)
return r2.content, mime_type
class Client:
number = ""
sid = ""
auth = ""
def __init__(self, number, sid, auth):
self.number = number
self.sid = sid
self.auth = auth
def get_unread_messages(self, list_count=20):
response = []
messages = twilio_client.messages.list(
to=self.number,
limit=list_count
)
for message in messages:
if message.sid not in dummy_list:
msg = Message()
msg.content = message.body
msg.number = message.from_
msg.sid = message.sid
msg.beta_uri = message.uri
if message.num_media != "0":
msg.message_type = "mms"
else:
msg.message_type = "sms"
response.append(msg)
else:
dummy_list.remove(message.sid)
self.mark_as_read(message.sid)
return response
def send_sms(self, content, to):
twilio_client.messages \
.create(
body=content,
from_=self.number,
to=to
)
def send_mms(self, content, to, url):
twilio_client.messages \
.create(
body=content,
from_=self.number,
media_url=[url],
to=to
)
def mark_as_read(self, sid):
try:
twilio_client.messages(sid).delete()
except TwilioRestException:
if sid not in dummy_list:
dummy_list.append(sid)
def mark_all_read(self):
messages = self.get_unread_messages(50)
for message in messages:
message.mark_as_read()