With ArcGIS GeoEvent it is very easy to setup a notification when a new feature is added to an ArcGIS Online Hosted Feature Service. Instructions can be found here. If you do not have GeoEvent you can still accomplish this using Python. Below is an example on how to do this.
When editor tracking is enabled, new features are recorded in a field called 'CreationDate'. The script queries this field within the service and compares the time to the current time (minus one hour). If the time is greater than the current time, it will send an e-mail with a specified field's values for the new features.
Windows Task Scheduler can be setup to execute this script at a given interval (i.e. every hour). This will report any new features within that last hour.
The below example is written for the Citizen Problem Report application. It utilizes 2 fields (probtype & status) when querying the service. These variables can be commented out, or updated to reference different fields, if working with a different service:
The SUBJECT and TEXT variables in the sendEmail function can be updated to whatever you would like to include in the e-mail:
import requests, json, datetime, time, smtplib
from datetime import timedelta
from email.mime.text import MIMEText
# Disable warnings
requests.packages.urllib3.disable_warnings()
# Variables
username = 'jskinner_CountySandbox' # AGOL Username
password = '*******' # AGOL Password
URL = 'https://services.arcgis.com/dlFJXQQtlWFB4qUk/arcgis/rest/services/CitizenProblems/FeatureServer/0' # Feature Service URL
dateField = 'CreationDate' # Date field to query
hoursValue = 1 # Number of hours to check when a feature was added
fromEmail = 'no-reply@esri.com' # Email sender
toEmail = ['user1@esri.com', 'user2@esri.com'] # Email receiver(s)
smtpServer = 'smtp.esri.com' # SMTP Server Name
portNumber = 25 # SMTP Server port
# Function to send email
def sendEmail():
SUBJECT = 'Problem Reported'
TEXT = "{0} was created with status {1}".format(typeProb, status)
smtpObj = smtplib.SMTP(host=smtpServer, port=portNumber)
msg = MIMEText(TEXT)
msg['Subject'] = SUBJECT
msg['From'] = fromEmail
msg['To'] = ", ".join(toEmail)
smtpObj.sendmail(fromEmail, toEmail, msg.as_string())
print("Successfully sent email")
smtpObj.quit()
# Generate AGOL token
try:
print('Generating Token')
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
r = requests.post(tokenURL, data=params, verify=False)
response = json.loads(r.content)
token = response['token']
except:
token = ''
# Return largest ObjectID
whereClause = '1=1'
params = {'where': whereClause, 'returnIdsOnly': 'true', 'token': token, 'f': 'json'}
r = requests.post(URL + '/query', data = params, verify = False)
response = json.loads(r.content)
try:
response['objectIds'].sort()
except Exception as e:
print("Error: {0}".format(e))
count = len(response['objectIds'])
# Query service and check if CreationDate time is within the last hour
if count < 1000:
params = {'f': 'pjson', 'where': "1=1", 'outFields' : '*', 'returnGeometry' : 'false', 'token' : token}
r = requests.post(URL + '/query', data=params, verify=False)
response = json.loads(r.content)
for feat in response['features']:
typeProb = feat['attributes']['probtype']
status = feat['attributes']['status']
createDate = feat['attributes'][dateField]
createDate = int(str(createDate)[0:-3])
t = datetime.datetime.now() - timedelta(hours=hoursValue)
t = time.mktime(t.timetuple())
if createDate > t:
sendEmail()
else:
y = minOID
x = minOID + 1000
ids = response['objectIds']
newIteration = (math.ceil(iteration/1000.0) * 1000)
while y < newIteration:
if x > int(newIteration):
x = newIteration
where = OID + '>' + str(y) + ' AND ' + OID + '<=' + str(x)
print('Querying features with ObjectIDs from ' + str(y) + ' to ' + str(x))
params = {'f': 'pjson', 'where': where, 'outFields' : '*', 'returnGeometry' : 'false', 'token' : token}
r = requests.post(URL + '/query', data=params, verify=False)
response = json.loads(r.content)
for feat in response['features']:
typeProb = feat['attributes']['probtype']
status = feat['attributes']['status']
createDate = feat['attributes'][dateField]
createDate = int(str(createDate)[0:-3])
t = datetime.datetime.now() - timedelta(hours=hoursValue)
t = time.mktime(t.timetuple())
if createDate > t:
sendEmail()
x += 1000
y += 1000