-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.py
55 lines (45 loc) · 1.75 KB
/
job.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
import pickle
import requests
import time
from decouple import config
from utils import twilio_authenticate, get_natural_timedelta
def send_message():
client = twilio_authenticate()
tried = 0
with open('db.bin', 'rb') as f:
db = pickle.load(f)
def send():
nonlocal tried
running_time = get_natural_timedelta()
# the user will get the time for which the service is running
body = f"{db['message']}\nservice running for {running_time}"
try:
message = client.messages.create(
to=db['phone'],
from_=config('TWILIO_PHONE'),
body=body,
status_callback=config('STATUS_CALLBACK'))
sid = message.sid
status = client.messages.get(sid).fetch().status
time.sleep(20)
if status not in ['sent', 'delivered'] and tried < 5:
tried += 1
with open('error.log', 'a+') as f:
f.write(f'{ time.ctime() } { status }\
SMS did not send successfully\n')
# saving the error log in "error.log" file in root directory
send()
time.sleep(20)
# the instant SMS status is likely to be "queued", therefore
# trying 20 seconds after sending message, and doing it
# for maximum five times
status = client.messages.get(sid).fetch().status
except requests.exceptions.ConnectionError:
# retry to send message if there is problem connecting internet
print("Connection error occured!")
time.sleep(5)
print("retrying")
send()
send()
if __name__ == '__main__':
send_message()