ArcGIS Online Hosted Feature Service Notification on new record/feature added

4170
37
02-11-2015 09:26 AM
Status: Open
New Contributor II

Add the ability to trigger a notification (to a specified e-mail address) when a new record is created in a hosted feature service though ArcGIS Online. This would be most beneficial when leveraging the GeoForm application for collecting information from the public. A bonus ability would be to initiate notification to different e-mails depending on a specific attribute. (For example, the GeoForm has a drop down menu (generated from a domain) for "Problem type", with the options "Pothole",  "Tree Issue", etc. If the "Pothole" problem is chosen, then the email notification would go to the street department, if "Tree Issue" is chosen then the notification would go to the Urban Forester).

37 Comments
Yes! Because what's the point of geoform from anonymous users, when you're not aware of the new entry?
 

If you have the GeoEvent Extension for ArcGIS Server, you can achieve this.  The steps are outlined in the help for the Citizen Problem Reporter application:

Real-time Problem Notifications - Citizen Problem Reporter | ArcGIS for Local Government

For those without the ability to access an Arc Server, this would be SUPER helpful! 

I agree there is a need for this functionality without having to purchase the Geoevent extension. In my office we have accomplished this goal with a python script and a scheduled windows task. That being said, I had a conversation with someone at Esri and they acknowledged the need for this to be a part of the core functionality. One Idea I have, is to utilize Sever Object Interceptors. If I understand the way Portal/ArcGIS Online works, you could in theory create a SOI to live on any service on the GIS server behind the portal, and set it up so that the SOI is configurable where the user can input a list of emails, define what the SOI is listening for, and what message should be sent. Me team and I hope to begin R&Ding this soon.

Hi,

Would it be possible to know what functions you used in Python to write your script? We would love to be able to have an email notification as well after new entries are added. 

Thanks in advance any help will be greatly appreciated!

This is how we do it:

# Import modules
import arcpy, time, sys, string, os, traceback, datetime, shutil, httplib, urllib, json, getpass, arcserver
import xml.dom.minidom as DOM
import xml.etree.ElementTree as ET
import smtplib
from email.MIMEText import MIMEText

# Setting the arc py environment
ENV= arcpy.env
# End Arcpy Environment

# Setting the overwrite of existing features
ENV.overwriteOutput = True
# End Overwrite Setting

# Variables
DateTime = datetime.datetime.now()
PreviousDateTime = readTextFile(txt_file, DateTime)
writelog(logFile, str(PreviousDateTime) + "\n")
D1 = "( LAST_EDITED_DATE >= '" + str(PreviousDateTime) + "' )"
FeatureClass = "\\feaureclasspath\\FeatureClass"
FeatureClassLayer = "FeatureClassLayer"

FeatureClassFields = ["Name", "Email", "Address", "PhoneNumber"]
# End Variable Section

try:
   FeatureClassLayer = arcpy.MakeFeatureLayer_management(FeatureClass, FeatureClassLayer )

   FeatureClassLayer  = arcpy.SelectLayerByAttribute_management(FeatureClassLayer , "NEW_SELECTION", D1)

   FeatureClassLayerCount  = arcpy.GetCount_management(FeatureClassLayer)

   if str(FeatureClassLayerCount) != str(0):

        cursor = arcpy.da.SearchCursor(FeatureClassLayer, FeatureClassFields)

        for row in cursor:

            Name = GetValue(row, 0)
            Email = GetValue(row, 1)
            Address = GetValue(row, 2)
            PhoneNumber = GetValue(row, 3)

            if EmailMessage == "":
               EmailMessage += "The following drainage complaints have been made: " + "\n"
            EmailMessage += "\n"
            EmailMessage += "Name: " + Name + "\n"
            EmailMessage += "Date: " + Date + "\n"
            EmailMessage += "Email: " + Email + "\n"
            EmailMessage += "Address: " + Address + "\n"
            EmailMessage += "Phone Number: " + PhoneNumber + "\n"

        del cursor
        del row

        writelog(logFile, "Process: Send Email" + "\n")
         # Send Email
         # This is the email notification piece [%]
         #email error notification
         smtpserver = 'email server'
         AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
         smtpuser = '' # for SMTP AUTH, set SMTP username here
         smtppass = '' # for SMTP AUTH, set SMTP password here

         RECIPIENTS = ['email@email.com', 'email1@email1.com']
         SENDER = 'email2@email2.com'
          #msg = arcpy.GetMessages()***I think I need to look at the message variable
         #msg = arcpy.GetMessage(0)# Brian Corrected this it is arcpy.GetMessage()
         #msg += arcpy.GetMessage(2)# Brian Corrected this it is arcpy.GetMessage()
         #msg += arcpy.GetMessage(3)# Brian Corrected this it is arcpy.GetMessage()
         msg = MIMEText(EmailMessage) #***i pointed this mime thing at the message
         msg['Subject'] = 'Edited Features briefing'
         # Following headers are useful to show the email correctly
         # in your recipient's email box, and to avoid being marked
         # as spam. They are NOT essential to the sendmail call later
         msg['From'] = "ArcGIS"
         msg['Reply-to'] = "Name "
         msg['To'] = "Email@Email.com"

except:
 print("Error:")

The concept is the same for dealing with feature services. We currently use a library from github called Portalpy in order to interact with the feature services. We are in the process of rewriting the process with the ArcGIS Python API. Please let me know if that helps or if you have any other questions. Thank you for everything and have a delightful day.

Sincerely,

Joe Guzi

Wow thank you so much, it's very appreciated!! I won't be able to look at it in more details for a little while but I have any questions I'll let you know. 

Cheers

Chrystel

It would be best if it was a supported webhook, then you could easily set up different notification emails by Problem Type with Microsoft Flow, Integromat, or another. We currently also use a scheduled python script, but webhooks would be much easier and allow more functionality.

FYI: Webhook is a work in progress in online feature service. Most likely it will be available later this year. 

Khaled Hassen

Online Feature Service Dev Lead

For anyone interested, here's the code I use to check for updates, send notifications, and update the feature. It uses the ArcGIS API for python (Python 3). Just turn on a scheduled task to run it how often you want.

It currently only sends internal notification, but if you understand what's going on, it would be easy to apply an external notification as well, so when a problem is marked as completed the email is then sent to the reportee with the email that they entered and then the email status is updated to sent or whatever you decide to use. Or, if there's no email you could change the emailstatus to "Unable to Send" or something like that. Your query would look like:

queryString = "status = 'Completed' AND emailstatus = 'WHATEVER_VALUE_YOU_PICKED_FOR_DEFAULT' "

EDIT: Depending on your version of "Pandas", you may have to use len(query_result1) instead of len(query_result1.features)

#!/usr/bin/env python3

"""
Python Script that will be used to notify staff of new additions
to the Citizen Problems feature in ArcGIS Online using the ArcGIS API for Python.

Notification is sent to the appropriate person when a new feature is found and the
feature is updated to Received
"""
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import smtplib
import datetime

def sendInternalNotification(category, problemType, fullName, submittedOn, details😞
   switcher = {
      "Animal": "EMAIL@DOMAIN.COM",
      "Blight": "EMAIL@DOMAIN.COM",
      "Health": "EMAIL@DOMAIN.COM",
      "Park/Tree": "EMAIL@DOMAIN.COM",
      "Road": "EMAIL@DOMAIN.COM",
      "Snow/Ice": "EMAIL@DOMAIN.COM",
      "Trash": "EMAIL@DOMAIN.COM",
      "Utility": "EMAIL@DOMAIN.COM",
      "Land Use": "EMAIL@DOMAIN.COM"
   }
   fromaddr = "donotreply@DOMAIN.COM"
   toaddr = switcher.get(category, "DEFAULTEMAIL@DOMAIN.COM") #second argument is in case a category is not in the switcher
   sep = "\n"
   msg = (
      f"From: {fromaddr}{sep}"
      f"To: {toaddr}{sep}"
      f"Subject: New {category} - {problemType}"
      f"{sep}"
      f"A new complaint has been made: {sep}"
      f"Category: {category}{sep}"
      f"Type of Problem: {problemType}{sep}"
      f"Name: {fullName}{sep}"
      f"Date Submitted: {datetime.datetime.fromtimestamp(int(submittedOn)//1000)}{sep}"
      f"Details: {details}{sep}"
   )

   server = smtplib.SMTP('YOUR.EMAIL.SERVER')
   server.set_debuglevel(1)
   server.sendmail(fromaddr, toaddr, msg)
   server.quit()

def main():
   #Login and get the Feature Layer
   gis = GIS("https://YOUR_ORGANIZATION.maps.arcgis.com", "USERNAME", "PASSWORD")
   #Create query string
   queryString = "status = 'Submitted'"

   #Query layer and send mail
   query_result1 = layer.query(where=queryString)
   if len(query_result1.features) > 0:
      featureUpdates = []
      for feature in query_result1.features:
         sendInternalNotification(feature.attributes["category"], feature.attributes["probtype"], feature.attributes["pocfullname"],          feature.attributes["CreationDate"], feature.attributes["details"])
         #Update feature status to Received
         feature.attributes["status"] = 'Received'
         featureUpdates.append(feature)

      layer.edit_features(updates=featureUpdates)

if __name__ == "__main__":
   main()