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

18065
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?

67 Replies
AlexP_
by
Occasional Contributor III

Edward,

Thank you for your quickly response. Your statement "Are you asking for a return of all records where the last edit date field or the status field has not changed in 24-48 hours?  If so the date field would be easy to do it on" is what I am asking. How can I do it? Is there a script or other? Please advise.

0 Kudos
EdwardHankins
New Contributor II

The script that is at the top of this page would be the one you want to use.  You would have to change some things to match your field names and your REST Service URL of course.  

Also the current script is searching for dates a little different,  you would need to change the current setup

  1. import urllib2, json, urllib, datetime, time, smtplib  
  2. from datetime import timedelta  
  3.   
  4. oidList = []  
  5.   
  6. URL = 'http://services.arcgis.com/Fz6JUji5ArUSDM/arcgis/rest/services/Airports/FeatureServer/0/query'  
  7. params = {'f': 'pjson', 'where': "1=1", 'outfields' : 'OBJECTID, created_date', 'returnGeometry' : 'false'}  
  8. req = urllib2.Request(URL, urllib.urlencode(params))  
  9. response = urllib2.urlopen(req)  
  10. data = json.load(response)  
  11. for feat in data['features']:  
  12.     createDate = feat['attributes']['created_date']  
  13.     createDate = int(str(createDate)[0:-3])  
  14.     t = datetime.datetime.now() - timedelta(hours=1)  
  15.     t = time.mktime(t.timetuple())  
  16.     if createDate > t:  
  17.         oidList.append(feat['attributes']['OBJECTID']) 

you want to change lines 6 to your URL,  also change line 7 to , this will return all of the attribute fields,  also you can use the where part to query only certain features (i will tell you more about this at the bottom)

  1. params = {'f': 'pjson', 'where': "1=1", 'outfields' : '*', 'returnGeometry' : 'false'}  

and then you want to change createDate to your LastEditedDate field,  since that is what you are comparing on.  

I would imagine yours would be something similar to 

  1. for feat in data['features']:  
  2.     LastDate = feat['attributes']['YOUR_LASTEDITED_DATEFIELD_NAME']  
  3.     LastDate = int(str(createDate)[0:-3])  
  4.     t = datetime.datetime.now() - timedelta(hours=48)   #you want 48 hours since you are looking for 2 days
  5.     t = time.mktime(t.timetuple())  
  6.     if LastDate < t:  # if the last edit date is less (older) than today - 48 hours (ie 2 days ago)
  7.         oidList.append(feat['attributes']) # i prefer this as it will append all attributes,  not just the OBJECTID like the original 

Once you have dumped the returns into the oidList you can access them by calling oidList['FIELDNAMEYOUWANT'] 

** alternatively you ignore the date compare in python and could use a SQL query in the params to only return records that havent been edited in the last 2 days  

  1. params = {'f': 'pjson', 'where': "1=1", 'outfields' : '*', 'returnGeometry' : 'false'}  

you would change the where part to something like this  (this wont work because i am not sure the exact syntax for querying datefields in rest services because they use unix epoch time) 

'where': "LAST_EDIT_DATE < TODAY()-2"  # again this wont work, but it should give you an idea of what to look for.

0 Kudos
AlexP_
by
Occasional Contributor III

Hello Edward.

Thank you for your response. Please clarify. What are features and attributes? Ex. Attributes = STATUS field? and features = ??? 

Do I need to replace LastDate = int(str(createDate)[0:-3])  ? I am not sure what this is..

If the status field is completed or closed, would it stop run 24 hours? 

Thank you for your assistance!

0 Kudos
EdwardHankins
New Contributor II

attributes are your fields,  but when i am referencing them in this case I am talking about the parts of your data object.  data is made up of geometry and attributes,  and likewise the attributes are made up features.  

for feat in data['features']   <<- features here is to get at your attributes,  ie you dont want geometry, just the features

oidList.append(feat['attributes'])   <--  each feature has attributes,  append these attributes in the list

LastDate = int(str([YOUR LAST DATE FIELD IN YOUR TABLE])[0:3]

 

^^ This is just making an INT (that is in UNIX EPOCH TIME) of your LAST DATE FIELD in YOUR RECORDS.  For somereason you need to remove the trailing 000s that occur when using EPOCH TIME.  This part is solely to get your time into a format that you can use datetime.datetime on to turn into a valid date to compare to your local tables.  

*** addressing your screenshot below,  you would have to send me your entire code, maybe post it here or make a zip file from it.  That error could be caused by any number of things. 

0 Kudos
AlexP_
by
Occasional Contributor III
import urllib2, json, urllib, datetime, time, smtplib  
from datetime import timedelta  
  
oidList = []  
  
URL = 'https://utility.arcgis.com/usrsvcs/servers/xxxxxxxxx/rest/services/xxxxx/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=24)  
    t = time.mktime(t.timetuple())  
    if createDate > t:  
        oidList.append(feat['attributes']['OBJECTID'])  
  
FROM = 'xxxxxxxx'  
TO = ['xxxxxxxxxxxx']  
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='xxxxxxxx', port=xxx)  
smtpObj.sendmail(FROM, TO, message)  
print "Successfully sent email"  
smtpObj.quit()  
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

It has been run successful but the email I have recieved is blank.

0 Kudos
EdwardHankins
New Contributor II

Ok so now you have built your oidList.  It should contain a list of all the OBJECTIDs and their created_date.  

You can check that by typing oidList in your python window and seeing what appears.  You now have to go line by line through the list and build your email.  

for me it was like this:

For you it may be different.  but think of it like this,  now you have a list (oiddict) that holds your objectid and your date.  now you open a for loop and go through each line and make your email message (TEXT in my case)  by appending it to a new list (in my case its called newList)  

Then format an email msg,  and then append the newList to it.  then fire off the email.  

i can probably share with you my script but i will have to clean it up and remove all my local server information and email accounts.  

0 Kudos
AlexP_
by
Occasional Contributor III

I am not sure that I am following. That would be great if you can share it to me. Much appericated. 

0 Kudos
EdwardHankins
New Contributor II

here you go.  i zipped the .py script and uploaded it here:

https://ufile.io/cf0ha 

0 Kudos
AlexP_
by
Occasional Contributor III

Thank you for the file. I revised it and tried to run it but I got invalid syntax. Screenshot below.

0 Kudos
EdwardHankins
New Contributor II

Send me the file with your changes and I will look at it.  I am off for the weekend so it wont be till next week.  

0 Kudos