Any ArcGIS Online Webhook tutorials for Python?

2191
10
09-02-2022 10:20 AM
RogerAsbury
Frequent Contributor

Just curious. Mostly what I find are webhook tutorials that use Integramat as the processing app.

 

Mostly I just find the way the webhooks for AGOL work weird compared to others I've used. It seems to send a URL. If I manually go to that URL, it brings me to a search page that has another URL at the bottom. If I go to that URL it takes me to yet another, but more specific, search page, with another URL at he bottom. Its only when I click that URL that I finally get the JSON for the change that was made that triggered the webhook. I mean, I can write something that parses through all that to get the URLs I need, but is that necessary? Is there a simpler way to just directly get the JSON?

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
10 Replies
RogerAsbury
Frequent Contributor

Looks like a similar question, though not Python specific, was asked a few weeks ago in the ArcGIS Online Developers Questions community.

Sounds like development is in the works on how webhooks in AGOL work, but no word on when that will be released. So for now, I guess my question still stands. 🙂

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
BlueBunnies
Emerging Contributor

So here is some samples of how to write code for webhooks at the Esri GitHub as well as some nice explainations:

https://github.com/Esri/webhooks-samples

This link shows some code in combination with using Arcade:

https://www.esri.com/arcgis-blog/products/api-python/developers/integrating-the-arcgis-api-for-pytho...

This following links take you to the Python API resource, where they have actual examples of how to write code for certain functions:

https://developers.arcgis.com/python/api-reference/arcgis.gis.admin.html#webhookmanager:~:text=for%2...

https://developers.arcgis.com/python/api-reference/arcgis.gis.admin.html#webhook:~:text=returns%3A%2...

Here are some code samples from users who have created their own python webhook workflows this includes workflows to grab the JSON and such:

https://programtalk.com/vs4/python/Esri/webhooks-samples/sample-workflows/slack/admin_assistant.py/

https://programtalk.com/vs4/python/Esri/webhooks-samples/sample-workflows/python-email/webhookListen...

https://programtalk.com/vs4/python/Esri/webhooks-samples/

 

I hope some of this helps!

 

RogerAsbury
Frequent Contributor

And now I'm curious if something happened over the long Labor Day holiday in the U.S., as once again, I find myself in a position where work I did last week isn't working now. This webhook is sending to some basic code, example below:

 

 

 

import azure.functions as func
import logging
import requests
import json
from pprint import pprint

def main(req: func.HttpRequest) -> str:
    jsonString = req.get_json()
    logging.error("Webhook JSON:")
    logging.error(pprint(jsonString))

 

 

 

What gets printed out is nothing. Or more specifically: None.

webhookerror.png

 

No changes from last week, when this happily printed out the json sent from the webhook. I'm finding the AGOL webhooks really flaky.

Edit - Figured I'd add this screenshot, in case anyone could see if I've screwed something up more fundamentally, here. 

webhook.png

 

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
KevinHibma
Esri Regular Contributor

@RogerAsbury  to answer your first question about getting the information from Extract Changes -- I'm not aware of any changes coming to this workflow. The webhook is purposely sent with what has been termed as a "skinny payload". Basically, a small payload without sensitive information inside. This is grounded in both performance and security reasons. I do sympathize that it takes a few steps to go back and grab what actually happened via the API.

I'm glad you and @BlueBunnies have found the github samples. Debugging Azure can be challenging sometimes, but it looks like you found the real time console. My suggestion if things aren't making sense in Azure, switch the payload URL to some place like https://webhook.site/ This is a great resource for quick tests. It takes the code and complexity out of grabbing those payloads.

0 Kudos
RogerAsbury
Frequent Contributor

I guess to be more specific: What I'm asking is if something has changed with how webhooks work between last Friday and today, because code that worked then doesn't seem to work now. I'm totally willing to accept that a change has occurred in one of the imported libraries, lord knows, that's happened before. 🙂

Also, just a tad frustrated that a process that seems so easy in one ESRI product (Survey123) is decidedly not in another, so my apologies if I come across a bit cranky. On the other hand, this isn't for anything production, I'm just trying things out to learn them. And that list of github samples was awesome.

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos
KevinHibma
Esri Regular Contributor

I'm getting webhooks sent from a couple of different Orgs/FeatureServices I have on arcgis.com.

From your screen shot, you have Active:True and accepting any changes ("*"). So everything looks good. 

0 Kudos
KevinHibma
Esri Regular Contributor

One other thing - would it be helpful if I added a Python sample for the Extract Changes workflow? Every so often I hear about this want, and I'd like to do it, but it falls off my priority list. It's possible someone has already written the code (so a matter of finding it), but either way, if it would be useful, I'd try to get it created as a sample in the mentioned repo.

KimOllivier
Honored Contributor

I am searching for a working example of extract_changes() too. I cannot understand the error messages. I did see a post from 2020 where it does not accept defaults as documented, you have to add in the URI for example.

0 Kudos
KevinHibma
Esri Regular Contributor

Kim,

I've attached a zip file with my progress on this. I'll note it's not quite complete, but offers the code you'll need to perform extract changes. (Specifically, I didn't complete the authentication to ArcGIS.com, the code assumes the service is publicly accessible). I'm trying to support both Online FS webhooks and Enterprise FS webhooks in the same script, so it needs to handle connecting to both.

You'll want to start in the agol_receiver.py -- all this does is grabs a JSON value and launches the workflow. In production, this would act as the webhook receiver and handle requests in real-time, but for this, it's just the entry way to test.

Inside extractChanges.py you'll see the basic workflow (start at the bottom). 

I hope this helps. When I get it finished up, I'll add it to the previously mentioned github repo.

0 Kudos