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:
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:
Below are step by step instructions so you can configure this Python script with your own survey.
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.
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.
Now you can download and configure the script as follows:
{
"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"
}
}
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.
After saving changes to the init.json file, it is time to give the script a quick test:
If any errors occur during execution, an error text file will be created in the same folder where you saved the Python 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:
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.
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.
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.