-
Notifications
You must be signed in to change notification settings - Fork 0
/
textnotifier.py
57 lines (48 loc) · 2.17 KB
/
textnotifier.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
from datetime import datetime
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
from datamodels import APIConfig
from notifier import Notifier
class TextNotifier(Notifier):
def __init__(self, api_config: APIConfig, phone_number, collection_address, collection_name, silent):
self.twilio_client = Client(api_config.twilio_sid, api_config.twilio_auth_token)
self.twilio_number = api_config.twilio_phone_number
self.phone_number = phone_number
self.collection_address = collection_address
self.collection_name = collection_name
self.sentAt = None
self.silent = silent
def minutes_between(self, d1, d2):
"""Get the minutes between two datetime objects"""
return (d2 - d1).total_seconds() / 60
def get_text_msg_body(self, raw_data):
polygonscan_link = 'https://polygonscan.com/address/{}'.format(raw_data['wallet'])
collection = self.collection_name if self.collection_name is not None else self.collection_address
return """
Hey! We noticed {} transfer(s) for collection {} from your wallet recently (block {} - {}). You may want to investigate for potential suspicious activity: {}
""".format(
raw_data['transfers'],
collection,
raw_data['start_block'],
raw_data['end_block'],
polygonscan_link
)
def notify(self, raw_data):
print("Sending text notification to {}!".format(self.phone_number))
print("Msg to send is {}".format(self.get_text_msg_body(raw_data)))
if self.silent:
return
if self.sentAt is not None and self.minutes_between(self.sentAt, datetime.now()) < 5:
print("Skipping... text already sent")
return
try:
message = self.twilio_client.messages.create(
to=self.phone_number,
from_=self.twilio_number,
body=self.get_text_msg_body(raw_data)
)
self.sentAt = datetime.now()
print("Text notification sent")
print(message)
except TwilioRestException as err:
print(err)