-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationManager.py
56 lines (43 loc) · 1.85 KB
/
NotificationManager.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
from twilio.rest import Client
from threading import Thread
from datetime import datetime
class TwilioNotifier:
def __init__(self, interval=10):
self.interval = interval
self.lastSent = datetime.strptime('2017-05-04', "%Y-%m-%d")
def send(self, conf, msg):
t = Thread(target=self._send, args=(conf, msg,))
t.start()
def _send(self, conf, msg):
if conf["enable_notifications"]:
delta = datetime.now() - self.lastSent
if delta.seconds>int(conf["interval"])*60:
self.lastSent = datetime.now()
client = Client(conf["twilio_sid"], conf["twilio_auth"])
client.messages.create(to=conf["phone"], from_=conf["twilio_from"], body=msg)
def addNewNumber(self, conf, userName, number):
client = Client(conf["twilio_sid"], conf["twilio_auth"])
outgoing_caller_ids = client.outgoing_caller_ids.list(limit=20)
callNumber = "+972"+number[1:]
for record in outgoing_caller_ids:
if callNumber in record.phone_number:
return callNumber
validation_request = client.validation_requests \
.create(
friendly_name=userName,
phone_number=callNumber
)
return callNumber
def updateNumer(self, conf, phoneNumnber, newName):
client = Client(conf["twilio_sid"], conf["twilio_auth"])
outgoing_caller_ids = client.outgoing_caller_ids.list(limit=20)
callNumber = "+972" + phoneNumnber[1:]
sid = ""
for record in outgoing_caller_ids:
if callNumber in record.phone_number:
sid = record.sid
if len(sid)>0:
outgoing_caller_id = client \
.outgoing_caller_ids(sid) \
.update(friendly_name=newName)
print(outgoing_caller_id.friendly_name)