A simple e-mail notification system for Survey123 for ArcGIS v2

34087
108
01-22-2018 03:55 PM
IsmaelChivite
Esri Notable Contributor
19 108 34.1K

Back in November 2016, I published a blog post describing how to automate e-mail notifications against a Survey123 project. The idea was simple: A Python script that would trigger an email  every time a new survey is submitted. By scheduling the script to run unattended at certain time intervals, one could easily put together a simple e-mail notification system.

I want now to revisit this topic and share a new Python script, which takes care of some limitations of the first version. Specifically, the new script:

  1. Works against ArcGIS Enterprise in addition to ArcGIS Online
  2. Can be configured to either send one email for every newly added survey, or alternatively send one email for all new records added since the script was last launched.

While the Python script can be used as is, I really share it as a starting point from which you can write the exact logic that matches your needs. You may for example tweak the script to:

  • Format the e-mail notification following specific branding guidelines.
  • Extend the notification logic so the recipient of the email varies depending on data submitted.

Below are step by step instructions so you can configure this Python script with your own survey.

Preparing your computer to run the Python script

Technically, you can host the script in any computer that is able to run Python 3.x, however, you will want to select the host carefully. You will want  the script to run regularly and unattended, so you want to pick a computer that will reliably running 24x7 and constantly connected to your network.

In addition to Python 3.x, the script requires the 'requests' Python module installed.  If your computer already has ArcGIS Pro installed, you already have Python 3.x and the 'requests' module installed. However, ArcGIS Pro is not required to run this script.

Preparing your feature service

The Python script works against your survey's feature service. The script requires that your feature service has Editor Tracking enabled. By default, all feature services created by Survey123 have Editor Tracking enabled, so there is really nothing that you need to do extra. If  your survey is configured to work against a service from ArcGIS Server, then you will want to make sure  Editor Tracking is enabled, and if not, enable it manually.

If you do not enable Editor Tracking, the  script will generate an error  text file indicating that Editor Tracking needs to be enabled and will stop execution.

Downloading and configuring the Python script

Now you can download and configure the script as follows:

  1. Download the Detect Changes and Notify Python script v2 and its associated init.json file and save them together within a folder in your computer.
  2. Edit and save the init.json file. At the very least, you will need to change the properties highlighted  in blue below. 

{
   "email":{
     "recipients":["JohnSmith@acme.com"],
      "from":"acme@acme.com",
      "subject":"New features has been added to your service",
      "text":"Hi Team,\n\nYou are receiving this e-mail because features have been added in:\n",
  "server":["smtp.acme.com", "", "", ""],
"onemailflag":1
   },
   "service":{
      "portalURL":"https://www.arcgis.com",
"fsURL":"https://services2.arcgis.com/fJJEXNgxjn0dpNsi/arcgis/rest/services/service_20dc5e3eab3f4efab8fde88f6...",
      "fsLayerNum":0,
  "serviceuser":"yourusername",
"servicepw":"yourpassword",
      "fieldstoreport":["*"],
      "viewerMapLevel":19
   },
   "filenames":{
      "lasteditfile":"lastedit.json"
   }
}

  • recipients: This is the e-mail that will be receiving notifications. You can create an email alias if you want several people to receive the email.
  • from: This is the e-mail of the person/organization that is sending the e-mail. You can also set it to a DoNotReply email address.
  • server: This setting refers to your email server connection properties which are defined as a comma separated list of strings. 
    • The first parameter is the host of your email server which can be specified by hostname or IP address. It will look something like smtp.yourCompany.com for example or smtp.gmail.com
    • The second parameter is optional and defines the port where the SMTP server is listening. Common ports are 25 and 587 but it could really be any other port depending on how your mail server is configured.
    • The third and fourth parameters are also optional and are used to set a user and password to access your email server.
To  properly get the email  server connection properties, you will want to contact your IT department and describe what you are trying to  achieve as many email servers are configured with strict security  policies that will prevent the script from successfully connecting and  using your corporate e-mail server.  Folks in your IT department should  know how to give you the right hostname and port for  your email server.  Handing over the source code of the Python script may be of help too.

If you want to configure this script using your Gmail,  You will need to configure your Gmail account with 2-step verification, and then setup a Gmail App password.   Once you have done that, use your complete Gmail e-mail address as the  user (third parameter) and the App password for the fourth parameter.   When using Gmail you do not need to specify a port number, so simply type "" in the second parameter.
    • onemailflag: If you leave this parameter as 1, then an e-mail will be sent summarizing all records added since the Python script executed for the last time. If you switch the value to 0, then the script will trigger one e-mail for every record added.
    • portalURL: You only need to change this setting if working against ArcGIS Enteprise. You will want here the full https location of your portal including the Web Adaptor.
    • fsURL: This is the URL of your survey's feature service, which could be running in ArcGIS Online or ArcGIS Enterprise.
    • fsLayerNum: This is the index of the layer in the feature service that you want to monitor. If you want to check for changes in the main layer of your survey, leave it to 0. If you want to check for changes in the layer of a repeat, change the index accordingly.
    • serviceuser and servicepw: The ArcGIS credentials of a user with access to the feature service.

After saving changes to the init.json file, it is time to give the script  a quick test:

  1. Run the script once so it can capture the current state of your feature service.
  2. Submit one record to your feature service and run the script again.

If any errors occur during execution, an error text file will be created in the same folder where you saved the Python script.

Scheduling the script

The Windows operating system includes a simple utility called Task Scheduler. It is quite easy to setup. Once you have determined  when the script will be triggered for the first  time, you can repeat its  execution at regular intervals. For example, every 5 minutes. Obviously, the computer where  you setup the task will need to be running all the time, although you can configure the task to run regardless of who is logged in.

The configuration of tasks in the scheduler is pretty much self-explanatory, but here are some specific instructions that can save you some back and forth:

  1. General: Check the option to run with highest privileges and set the task to run even if you are not logged-in.
  2. Trigger: If you want to quickly test your task, you can simply select your task in the gallery and then hit Run in the Selected Item panel on the right.  When configuring the task for real, I suggest you select the startup trigger and that you also configure the task to run indefinitely every five or ten minutes or so.
  3. Actions: You will need to be particularly careful with this one. The Program/Script setting needs to point to the Python executable (Python.exe).
    • If using your own copy of Python, refer to the installation directory of Python where you will find Python.exe
    • If using Python from ArcGIS Pro, it will be under the Pro installation directory. For example: "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"

You also need to indicate the location of your DetectChanges.py Python script as an argument. Do not forget to include the .py extension in the path. If the path to your DetectChanges.py file includes spaces, then you need to enclose the path with quotes. Lastly, set the 'Start in' property so it points to the  directory in which you are storing the DetectChanges.py file.

Beyond the basics

The technique described in this blog post is a bit rudimentary, but it  may do its job reasonably in some simple scenarios. You can manipulate the Python script to properly format the email message to be sent, and also to apply some logic to determine if a n email needs to be sent, and to who.  Since you are in 'Python land' already, you can get creative and import the arcpy.mapping module to do all sort of sophisticated things in the script. You can for example take the incoming feature added into your feature service and do a straight (buffer) or network distance (closest facility) search to determine who will be notified.

Differences between the original script and this one

There are a handful of differences between the original Python script published in November 2017 and this one.  For the most part, they are the same, but here are the key changes:

  • The most important difference is how changes in the feature layer are found. In the November 201 script the script exercises a new API introduced in ArcGIS Online to detect changes. This new API only works in ArcGIS Online (not in Enteprise) and it had to be enabled manually on the survey's feature service.  The latest version of the script, described in this  post, uses Editor Tracking. Editor Tracking is enabled by default in every feature service created by Survey123 for ArcGIS. Editor Tracking is compatible with ArcGIS Online feature services and also with ArcGIS Server feature services back to 10.1.
  • One email per feature or one per execution: The other  key difference is that in our November 201 version, it was not possible to easily configure the script to aggregate all changes in a single email. With this version, you can use the 'onemailflag' parameter in the init file to decide if you want to send one email per added record, or one email per execution of the script.

If you are already using the November 201 version of the script and want to switch to this one, I recommend that you copy the Python script and the new init file  into a separate folder. The init file is different, so pay attention to the new parameters introduced.

108 Comments
gisportaladminsTF
New Contributor II

Hi,

how can we build an additional attachment url when sending out emails?

Thanks

GIS_Fox
Occasional Contributor III

Is there a function that similarly allows the submission of a Google Calendar or Outlook event using Python? This function either separate or in tandem with email notifications is a function we have been using and testing though usually using third party vendors or coding. Having this in house and in Python would be incredible and benefit a number of active or long term projects.

JeffLegato1
Occasional Contributor

We are getting an error when we try to run this script:

C:\CameraRegDetectEdits>"c:\Program Files\Python39\python.exe" DetectEdits.py
Traceback (most recent call last):
File "C:\CameraRegDetectEdits\DetectEdits.py", line 340, in <module> main(configfilename)
File "C:\CameraRegDetectEdits\DetectEdits.py", line 81, in main maxeditdate = getMaxDate(urlLyr, editfieldsDict['creationDateField'], 'last editdate', servicetoken) / 1000. + 0.001
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

I noticed that @StephenWarnock had this same issue back in July 2018. Any help would be appreciated. 

 

******UPDATE******

Apparently, this error occurs when you have no data in the featureclass. It's working for us now. 

fkhan_sanjacintoca
New Contributor II

Does this script work if my AGOL password is tied to single-sign in? Like the way I access my computer, outlook email, and ArcGIS accounts all use the same logins via single sign on. However whenever I try to run this script I get a token generation error. I am sure that the username I entered is all lowercase because I've seen people mention that the username is case sensitive. My password has one capital letter in it. Does that create a conflict? I'm very confused trying to troubleshoot this script. Any help or insight would be greatly appreciated.

JeffLegato1
Occasional Contributor

@JamesTedrick I'm hoping you can help. We had this script running fine using a FC hosted on AGOL but when we migrated to Enterprise and I recreated the hosted FC on our Enterprise I am getting a new email every day even if no new features were added. The lastedit.json file is getting overwritten successfully the scrip is just sending an email every day with the same features identified as new. I feel like it's a user account or permissions issue but those things seem to be in order. Any tips would be appreciated. Thank you. 

------------------Update------------------

@JamesTedrick It seems to be working now. We discovered that the account running the scheduled script did not have permission to overwrite the lastedit.json file.  Sorry to bug ya'll! Thanks again for providing this functionality. I'm looking forward to this being easier (integrated) in the future. 

halaaburas
New Contributor

Thank you for this contribution. It works perfectly with Gmail without the need to add the port number. Ensure that you add the Gmail App password and NoT your Gmail Password.  

DanielWebb
Occasional Contributor

This works well on AGOL (since it uses UTC). Does anyone know how to edit the script to work with Portal on Pacific time? I can’t get it to work. Anything date/time always confuses me.

Thanks!

MansfieldGIS
New Contributor III

I'm trying to run this email notification and I'm getting this error message 

Traceback (most recent call last):
File "<string>", line 340, in <module>
File "<string>", line 29, in main
NameError: name '__file__' is not defined"

when I running  it straight  in ArcGIS Pro 3.2.0.  I have also set up a task schedul to automatically run 
(see below)  it will not run in that.  I also have another notification script that I set up 1st  and that runs perfectly.  I just can't get the 2nd one running.

This is the one that is not working.

MansfieldGIS_0-1713452331609.png

This one works.

MansfieldGIS_1-1713452383304.png

any help would be appreciated.  Thanks