Generate/send email when a feature is added to a hosted service

18330
67
Jump to solution
02-24-2015 09:47 AM
ChandreyeeLahiri
New Contributor III

Is it possible to have an email sent (to a list of recipients) whenever a point is added to a hosted File GDB on a public web-app?

 

The idea is to originate a permit application process from a web-map/app: applicants can add a point for the location they need a permit for, enter their information and apply for a permit (of particular kind). An email alerts officials that a permit has been applied for. The rest of the approval process flow from there and the hosted data is used to collect applicant information (including location for permit) as well as flag the status of the application as it makes its way through various stages of the approval process.

 

Any ideas on how to have edits trigger emails?

1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Chandreyee,

If you are tracking edits within the hosted feature service, you could have Windows Task Scheduler check the service at a given time interval (i.e. once an hour) and check for any new features that were added within the last hour.

Below is an example on how to do this.  The service is tracking edits, where new features are recorded in a field called 'created_date'.  The script queries the service and checks this field and compares it to the current time (minus one hour).  If the time is greater than the current time, it will send an e-mail of the OBJECTIDs of the new features.

import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta

oidList = []

URL = 'http://services.arcgis.com/Fz6JUji5ArUSDM/arcgis/rest/services/Airports/FeatureServer/0/query'
params = {'f': 'pjson', 'where': "1=1", 'outfields' : 'OBJECTID, created_date', 'returnGeometry' : 'false'}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
for feat in data['features']:
    createDate = feat['attributes']['created_date']
    createDate = int(str(createDate)[0:-3])
    t = datetime.datetime.now() - timedelta(hours=1)
    t = time.mktime(t.timetuple())
    if createDate > t:
        oidList.append(feat['attributes']['OBJECTID'])

FROM = 'sender@hotmail.com'
TO = ['receiver@yahoo.com']
SUBJECT = 'New Feature Added'
TEXT = "Features with OBJECTIDs " + str(oidList) + " were added."

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

smtpObj = smtplib.SMTP(host='smtp.hotmail.com', port=25)
smtpObj.sendmail(FROM, TO, message)
print "Successfully sent email"
smtpObj.quit()

View solution in original post

67 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Chandreyee,

If you are tracking edits within the hosted feature service, you could have Windows Task Scheduler check the service at a given time interval (i.e. once an hour) and check for any new features that were added within the last hour.

Below is an example on how to do this.  The service is tracking edits, where new features are recorded in a field called 'created_date'.  The script queries the service and checks this field and compares it to the current time (minus one hour).  If the time is greater than the current time, it will send an e-mail of the OBJECTIDs of the new features.

import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta

oidList = []

URL = 'http://services.arcgis.com/Fz6JUji5ArUSDM/arcgis/rest/services/Airports/FeatureServer/0/query'
params = {'f': 'pjson', 'where': "1=1", 'outfields' : 'OBJECTID, created_date', 'returnGeometry' : 'false'}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
for feat in data['features']:
    createDate = feat['attributes']['created_date']
    createDate = int(str(createDate)[0:-3])
    t = datetime.datetime.now() - timedelta(hours=1)
    t = time.mktime(t.timetuple())
    if createDate > t:
        oidList.append(feat['attributes']['OBJECTID'])

FROM = 'sender@hotmail.com'
TO = ['receiver@yahoo.com']
SUBJECT = 'New Feature Added'
TEXT = "Features with OBJECTIDs " + str(oidList) + " were added."

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

smtpObj = smtplib.SMTP(host='smtp.hotmail.com', port=25)
smtpObj.sendmail(FROM, TO, message)
print "Successfully sent email"
smtpObj.quit()
ChandreyeeLahiri
New Contributor III

This is excellent Jake!

Only I would need to tweak it to check for changes in certain fields + changes since the last time it was checked....which might just be daily (so changes in the last 24 hours instead of 1).

Could you tell my the syntax for setting up those queries (or point me to resources)?

Also, I'm not sure where to run this code from. I'm a bit familiar with Python but this doesn't look like it. A few pointers please on how to run this?

Thanks much!

0 Kudos
RyanNosek
Occasional Contributor II

Jake -

Thank you for posting this as an example! Not sure why your response was not marked as the "correct answer", but nonetheless the OP's question and your response helped me tremendously.

Just wanted to add that if someone were to implement your code as-is and set up a scheduled task to run every hour, the script would generate an email every hour regardless if there were new features added or not.  Or maybe I've set something up wrong with the scheduled task?  Regardless, to fix the problem I encountered - I simply added an if-else statement that checked to see if the list was empty, if so, do nothing, if not, send the email. 

This is an extremely useful piece of code and saved me tons of dev time and head-banging, and have implemented it for a few of our data collection projects that use our locally hosted ArcGIS Server secured feature services (not esri hosted feature services). E.g. Very useful for services involved in our Crowdsource Reporter apps to generate emails to relevant Programs when new features are added. Thanks again for your post, Jake.

- Ryan

RemiFoicik
New Contributor II

Hi Jake, 

I'm really interested by using this script for many of our features services but our datas aren't on Arcgis Online, They are on a 10.4 Portal. 

I'm really new in python but i'm asking if this is possible to do the same on non-public feature service. And if this is possible, have you some tips please ? I don't really know when I have to place the token generated etc.. 

thx in advance, 

Best Regards 

Rémi 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Remi,

This should work with Portal for ArcGIS as well.  You will just need to update the URL (line 6) to the feature service you are monitoring.

0 Kudos
RemiFoicik
New Contributor II

First, Thanks you for your answer. 

I'm actually trying with the service shared with everyone. 
Unfortunately, I get this when I'm trying to run the script  

0 Kudos
RyanNosek
Occasional Contributor II

Check your service/feature calss, do you have this field ("created_date") in your dataset? Usually this field is created when you enable editor tracking on your sde gdb.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Does your feature service have editor tracking enabled?  If so, does the field 'created_date' exist?

0 Kudos
RemiFoicik
New Contributor II

Unfortunately Yes 😕


0 Kudos