-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.py
66 lines (57 loc) · 2.14 KB
/
alerts.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
import json, urllib, datetime, time, smtplib, requests
from datetime import timedelta
from twilio.rest import TwilioRestClient
from email.mime.text import MIMEText
accountSID = [your info]
authToken = [your info]
twilioCli = TwilioRestClient(accountSID, authToken)
myTwilioNumber = [your twilio number]
myCellPhone = [your cell number]
oidList = []
import arcrest
from arcrest.security import AGOLTokenSecurityHandler
username = [your username]
password = [your password]
sh = AGOLTokenSecurityHandler(username=username, password=password)
URL = [url to your feature layer]
fl = arcrest.agol.FeatureLayer(url=URL,securityHandler = sh, initialize = True)
# the creation date field requires tracking enabled
params = {'f': 'pjson', 'where': "1=1", 'outfields': 'OBJECTID, CreationDate', 'returnGeometry': 'false'}
sql= '1=1'
resFeats = fl.query(where=sql, out_fields="*")
for feat in resFeats:
createDate = feat.get_value('CreationDate')
createDate = int(str(createDate)[0:-3])
t = datetime.datetime.now() - timedelta(hours=1)
t = time.mktime(t.timetuple())
if createDate > t:
oidList.append(feat.get_value('OBJECTID'))
# To send email, press 1
SUBJECT = ''
TEXT = ''
FROM = '[email protected]'
TO = '[email protected]'
# Exit the program if there are no updates to share
if len(oidList) == 0:
print("no new records")
exit()
# Prepare message components if only 1 feature added
elif len(oidList) == 1:
SUBJECT = 'New Feature Added'
TEXT = str(len(oidList)) + " feature was added to [name of your layer]."
# Prepare message components if more than 1 feature added
elif len(oidList) > 1:
SUBJECT = 'New Features Added'
TEXT = str(len(oidList)) + " features were added to [name of your layer]."
# Send SMS message with update
message = twilioCli.messages.create(body=TEXT, from_=myTwilioNumber, to=myCellPhone)
# Create email message and parts
msg = MIMEText(TEXT)
msg['From'] = FROM
msg['To'] = TO
msg['Subject'] = SUBJECT
# Connect to email server and send email
with smtplib.SMTP([your smtp server], [your port]) as smtpObj:
smtpObj.starttls()
smtpObj.login([your email],[your password])
smtpObj.send_message(msg)