I discovered a handy workflow for calling and passing arguments to Python scripts using Survey123’s webhook capability, Integromat, and Google Cloud Functions. The possibilities are endless, but some use cases include:
- Making scripts available and convenient to users without a Python environment on their machine
- Calling a script each time a survey is submitted or updated (e.g., perform a field calculation)
- Update records in a separate feature layer based on survey responses
In this example, I’ve made a simple web form and Python script that checks an ArcGIS Online org for new items and sends a raw list object of these items via email.
Step 1: Create a Google Cloud Function to run your script
- Go to https://console.cloud.google.com/, log in with your Google account, and follow prompts to begin a Google Cloud Platform trial.
- You’ll be provided with a default project titled “My First Project.” From the dashboard of this project, in the Resources pane, click Cloud Functions. You can also go directly to functions at https://console.cloud.google.com/functions
- At the top of the page, click Create Function. Name your function and accept defaults except Runtime, which you’ll change to Python 3.7.
- Click the requirements.txt tab, above the code editor pane. This is where you’ll enter any Python modules required by your script that aren’t included in the standard Python distribution. For this example, we will need to add only the arcgis module.
- Now we add our Python code! Click the main.py tab and paste your code. Be sure to wrap everything in a function, pass ‘request’ as an argument to your function, and assign variables used by the function with request.args.get(‘example’), matching the field names from the Survey123 form you’ll create in the next section. See my example below.
- Enter your function name under ‘Function to Execute’ and click Create. It will take a few minutes for your function to deploy.
Step 2: Create and publish Survey123
Now, let’s create a form to call the Python script.
- Open Survey123 Connect and create a new survey with fields for each argument you’ll be passing to the script. These field names must match what you provided to the request.args.get() method in the script – in our case, “startDate” and “toEmail”.
- Publish your survey.
Step 3: Set up a scenario in Integromat and connect a webhook to your Survey123 form
You’ll need to set up two modules in Integromat: The Survey123 module to watch for a new survey submission, and the HTTP module to make a request that calls the script on Google Cloud Functions and passes arguments to it.
- Add the Survey123 module to your scenario and connect it to your new survey as described here.
- Next, add the HTTP module but leave it alone for now. Connect the two modules:
- Return to your Google Cloud Function, click the Trigger tab, and copy the URL for your function:
- Paste your URL into the HTTP module in Integromat and add a question mark at the end, followed by your first field/argument name and an equals sign, as shown below.
- Now grab your first argument from the dynamic fields window by expanding Attributes and clicking the field name:
- Repeat for additional arguments, separating with an ampersand (&). The final URL in our example looks like this:
Step 4: Call your script with the survey
- Turn on your Integromat scenario (or click Run Once)
- Open your survey in a web browser, fill in your arguments, and submit. You just called your script!