-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathALTnotifications.py
271 lines (225 loc) · 8.8 KB
/
ALTnotifications.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import random
import ubinascii
import urequests
class GridEmail:
"""
A class used to represent a Grid-Email object
...
Attributes
----------
sender_address : str
The Email address of the sender
sendgrid_auth_token : str
The account's authentication token from SendGrid
Methods
-------
send_email(self, receiver_address, subject='', body='')
Sends an email
verf_code(self, receiver_address, code_len=8)
Sends an email with a verification code to destination
"""
def __init__(self, auth_token, sender_address):
"""
Parameters
----------
sender_address : str
The Email address of the sender
auth_token : str
The account's authentication token from SendGrid
"""
self.sender_address = sender_address
self.sendgrid_auth_token = auth_token
def send_email(self, receiver_address, subject='', body=''):
"""
Sends an Email message
Parameters
----------
receiver_address : str
The Email address to receive the message
subject : str
The subject of the Email message (default is empty string)
body : str
The body of the Email message (default is empty string)
"""
headers = {'Authorization': self.sendgrid_auth_token, }
json_data = {'personalizations': [{'to': [{'email': receiver_address, }, ], }, ],
'from': {'email': self.sender_address, }, 'subject': subject,
'content': [{'type': 'text/plain', 'value': body, }, ], }
try:
response = urequests.post('https://api.sendgrid.com/v3/mail/send', headers=headers, json=json_data)
except Exception as e:
print('Failed Sending Email: ' + str(e))
return
print('Email Sent With Status Code: ' + str(response.status_code))
print('Response: ' + response.text)
def verf_code(self, receiver_address, code_len=8):
"""
Sends a verification code in Email
Parameters
----------
receiver_address : str
The Email address to receive the verification code
code_len : int
Number of digits in the verification code (default is 8)
Returns
-------
str
The generated verification code
"""
max_number = 10 ** code_len - 1
code = str(random.randint(0, max_number))
code = '0' * (code_len - len(code)) + code
self.send_email(receiver_address, 'Verification Code', 'Use verification code ' + code + ' for authentication.')
return code
class TwilioSMS:
"""
A class used to represent a Twilio-SMS object
...
Attributes
----------
twilio_account_sid : str
The account's SID from Twilio
twilio_auth_token : str
The account's authentication token from Twilio
twilio_service_sid : str
The account's service SID from Twilio
Methods
-------
send_sms(self, to_number, body='')
Sends an SMS message
verf_code(self, receiver_address, code_len=8)
Sends an SMS with a verification code to destination
"""
def __init__(self, account_sid, auth_token, service_sid):
"""
Parameters
----------
account_sid : str
The account's SID from Twilio
auth_token : str
The account's authentication token from Twilio
service_sid : str
The account's service SID from Twilio
"""
self.twilio_account_sid = account_sid
self.twilio_auth_token = ubinascii.b2a_base64('{sid}:{token}'.format(sid=account_sid, token=auth_token)).strip()
self.twilio_service_sid = service_sid
def send_sms(self, to_number, body=''):
"""
Sends an SMS
Parameters
----------
to_number : str
The number to receive the message
body : str
The contents of the message (default is empty string)
"""
data = 'Body={body}&To={to_number}&MessagingServiceSid={mssid}'.format(body=body,
to_number=to_number.replace('+', '%2B'),
mssid=self.twilio_service_sid)
try:
response = urequests.post(
'https://api.twilio.com/2010-04-01/Accounts/' + self.twilio_account_sid + '/Messages.json', data=data,
headers={'Authorization': b'Basic ' + self.twilio_auth_token,
'Content-Type': 'application/x-www-form-urlencoded'})
except Exception as e:
print('Failed Sending SMS: ' + str(e))
return
print('SMS Sent With Status Code: ' + str(response.status_code))
print('Response: ' + response.text)
def verf_code(self, to_number, code_len=8):
"""
Sends a verification code in an SMS
Parameters
----------
to_number : str
The number to receive the verification code in an SMS
code_len : int
Number of digits in the verification code (default is 8)
Returns
-------
str
The generated verification code
"""
max_number = 10 ** code_len - 1
code = str(random.randint(0, max_number))
code = '0' * (code_len - len(code)) + code
self.send_sms(to_number, 'Use verification code ' + code + ' for authentication.')
return code
class TwilioWhatsApp:
"""
A class used to represent a Twilio-WhatsApp object
...
Attributes
----------
twilio_account_sid : str
The account's SID from Twilio
twilio_auth_token : str
The account's authentication token from Twilio
twilio_from_number : str
The phone number of the sender
Methods
-------
send_whatsapp_message(self, to_number, body='')
Sends a WhatsApp message
verf_code(self, receiver_address, code_len=8)
Sends a WhatsApp message with a verification code to destination
"""
def __init__(self, account_sid, auth_token, from_number):
"""
Parameters
----------
account_sid : str
The account's SID from Twilio
auth_token : str
The account's authentication token from Twilio
from_number : str
The phone number of the sender
"""
self.twilio_account_sid = account_sid
self.twilio_auth_token = ubinascii.b2a_base64('{sid}:{token}'.format(sid=account_sid, token=auth_token)).strip()
self.twilio_from_number = from_number
def send_whatsapp_message(self, to_number, body=''):
"""
Sends a WhatsApp message
Parameters
----------
to_number : str
The number to receive the message
body : str
The contents of the message (default is empty string)
"""
data = 'Body={body}&To=whatsapp:{to_number}&From=whatsapp:{from_number}'.format(body=body,
to_number=to_number.replace('+',
'%2B'),
from_number=self.twilio_from_number.replace(
'+', '%2B'))
try:
response = urequests.post(
'https://api.twilio.com/2010-04-01/Accounts/' + self.twilio_account_sid + '/Messages.json', data=data,
headers={'Authorization': b'Basic ' + self.twilio_auth_token,
'Content-Type': 'application/x-www-form-urlencoded'})
except Exception as e:
print('Failed Sending WhatsApp Message: ' + str(e))
return
print('WhatsApp Message Sent With Status Code: ' + str(response.status_code))
print('Response: ' + response.text)
def verf_code(self, to_number, code_len=8):
"""
Sends a verification code in a WhatsApp message
Parameters
----------
to_number : str
The number to receive the verification code in a WhatsApp message
code_len : int
Number of digits in the verification code (default is 8)
Returns
-------
str
The generated verification code
"""
max_number = 10 ** code_len - 1
code = str(random.randint(0, max_number))
code = '0' * (code_len - len(code)) + code
self.send_whatsapp_message(to_number, 'Use verification code ' + code + ' for authentication.')
return code