AGOL email trigger for added features?

6930
10
Jump to solution
01-07-2015 06:46 AM
ThomasHoman
Occasional Contributor III

Hello all,

After creating my first data editing app in AGOL, I have a question. Is there some method of email notification that features were added?

I don't want to have to be tied to AGOL all the time creating what effectively becomes a manual process looking for updates. Opening a MXD that has the feature service added to the map is still a manual process in my book. An email notification would serve as a triggering event to check the AGOL data and ingest the features recently added.

Thanks for any insight/guidance

Tom

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

I helped a customer create a script that was run by Windows Task Scheduler every couple hours.  The script checked the pressure of valves in their hosted service.  If the pressure was over 500 PSI, an e-mail would be sent.  The script is posted below:

import urllib, urllib2, json, smtplib

username = 'agol'
password = 'agol'

tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']


valveURL = 'http://services2.arcgis.com/DyqsuMihRCOO7S/arcgis/rest/services/DEMO1/FeatureServer/4/query'
params = {'where': 'PRESSURE > 500', 'returnIdsOnly': 'true', 'f': 'pjson', 'token': token}
req = urllib2.Request(valveURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
valveList = data['objectIds']


if len(valveList) > 0:
    FROM = 'user@agol.com'
    TO = ['user@agol.com']
    SUBJECT = 'Valve Inspections'
    TEXT = "Valves with OBJECTIDs " + str(valveList) + " need inspection."

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

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

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

View solution in original post

10 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Thomas,

Take a look at the Operations Dashboard.  You can easily configure widgets to show information about the features and their associated attributes.  For example, you could create a query widget to show which features were added on a given date/time.

0 Kudos
ThomasHoman
Occasional Contributor III

Hi Jake,

That's an option but really it's closely related to my trying to avoid having another application open to monitor that I'm trying to avoid. The app is probably going to see very low traffic rates (1-2 entries per month after the first 2 weeks) so having a dashboard up is a non-starter for me.

The app is to collect boundary polygons for notification purposes when we change road names in the County. Once the first responder and utility types get added I don't anticipate the public as being too interested so activity will essentially become close to nil. At that point I would be checking for new data on a monthly basis.

Regards,

Tom

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I helped a customer create a script that was run by Windows Task Scheduler every couple hours.  The script checked the pressure of valves in their hosted service.  If the pressure was over 500 PSI, an e-mail would be sent.  The script is posted below:

import urllib, urllib2, json, smtplib

username = 'agol'
password = 'agol'

tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']


valveURL = 'http://services2.arcgis.com/DyqsuMihRCOO7S/arcgis/rest/services/DEMO1/FeatureServer/4/query'
params = {'where': 'PRESSURE > 500', 'returnIdsOnly': 'true', 'f': 'pjson', 'token': token}
req = urllib2.Request(valveURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
valveList = data['objectIds']


if len(valveList) > 0:
    FROM = 'user@agol.com'
    TO = ['user@agol.com']
    SUBJECT = 'Valve Inspections'
    TEXT = "Valves with OBJECTIDs " + str(valveList) + " need inspection."

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

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

    smtpObj = smtplib.SMTP(host='mail.agol.com', port=25)
    smtpObj.sendmail(FROM, TO, message)
    print "Successfully sent email"
    smtpObj.quit()
ThomasHoman
Occasional Contributor III

Hi Jake,

Thanks for the script. With some minor editing, I should be able to move forward.

Tom

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Tom,

If you get a chance, please mark this question as answered to help other users in the community.

0 Kudos
KushendraShah2
New Contributor II

Hi Jake, 

Can we integrate this into the portal federated with the server? I need to get an alert message in an email as the new feature is created in a feature layer in the database. Can you help me with that?

Thanks,

Kush

0 Kudos
JakeSkinner
Esri Esteemed Contributor
KushendraShah2
New Contributor II

Hi Jake,

Thanks for the script. It works on my end. However, I have an issue regarding automated email notification. I set the script to execute in window task scheduler, and If I just put my email address, I get notified. But, if I put multiple email recipients (i.e coworkers email address including myself), I don't receive the notification. Do you have an idea to solve this issue?

Thanks,

Kush

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Kushendra Shah‌ take a look at the e-mail portion of the script in the below document.  I believe I updated it a few months ago so it will works with multiple email recipients. 

# If new features exist, send email
if len(oidList) > 0:
    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()