Sending JSON as a Post Request to a Microsoft Teams Webhook?

4844
3
04-14-2021 04:32 PM
AdamRepsher_BentEar
Frequent Contributor

Hi Everyone!

Fun evening - spending it with GeoEvent Server v10.8. 😁

I am struggling with a few things.  I have a developer who figured out how to post a message to a MS Teams channel through a python script.  He is sending this message HTTP Post :

 

{
    "contentType": "application/vnd.microsoft.card.hero",
    "content": {
      "title": "Seattle Center Monorail",
      "text": "The Seattle Center Monorail is an elevated train line between Seattle Center (near the Space Needle) and downtown Seattle. It was built for the 1962 World's Fair. Its original two trains, completed in 1961, are still in service.",
     "buttons": [
       {
          "type": "openUrl",
          "title": "Official website",
          "value": "https://www.seattlemonorail.com"
        }
      ]
    }
 }

 

I was thinking (yea - my bad) that I could just compact this down to one line and use it as a template to calculate my own data into this format...  and then calculate into a single string field!

SO - I compacted it and slapped two single quotes around it to see if I could throw it into an HTTP Text output...

 

'{"contentType":"application/vnd.microsoft.card.hero","content":{"title":"Seattle Center Monorail","text":"The Seattle Center Monorail is an elevated train line between Seattle Center (near the Space Needle) and downtown Seattle. It was built for the 1962 World's Fair. Its original two trains, completed in 1961, are still in service.","buttons":[{"type":"openUrl","title":"Official website","value":"https://www.seattlemonorail.com"}]}}'

 

I put it into a field calculator and asked it to put it all into a STRING formatted field:

Processor "Fill Text Field" : Field Calculator validation failed: Unable to parse character at position 334: ':'

Yea - so I think this relates to another lesson that @RJSunderman sent me last week on literal quotes and ambiguity of opening and closing quotes.  I was doing some more in-depth reading about it just before starting this - and my mind just liquified when docs.oracle.com said:

Warning:
The rules for using quotes within message format patterns unfortunately have shown to be somewhat confusing.

Really?

Well - I began to think about what I was trying to do.  Just check if I could send a String in a HTTP Text....
output...
To an incoming webhook...

Wait.  I set up an HTTP Push JSON to an External Website.  :see_no_evil_monkey:

I think it's time to see if I am barking up the wrong tree.  WHAT DO YOU THINK?  Am I completely wasting my time here because what I am trying to accomplish is not in the cards with OOTB GeoEvent Server?

Thank you for reading this far - and for your honesty!

--Adam

0 Kudos
3 Replies
RJSunderman
Esri Regular Contributor

Hey Adam --

Serializing a JSON Object (e.g. the collection and structure of key:value pairs in-between the outermost curl-braces) as a String can be done. It's not easy. You might take a look at the community thread How to switch positions on coordinates which illustrates a series of Field Calculator processors, each using a replaceAll( ) function with regular expression pattern matching, to perform some manipulation on a received String. The goal in that thread is to take the received data string and turn it into a JSON string representation of a polygon geometry.

So it's not serializing JSON with all of its embedded double-quotes, square brackets, curl-braces, and commas that's a problem. I would recommend taking a step back to think about what you're trying to do. It could be very difficult to extract values from event record attributes for title, text, type, title (potential duplicate attribute name!), and value and insert them into a properly formatted JSON string so that you can send the String as a JSON Object to an external receiver using a Push JSON to an External Website outbound connector.

That higher level challenge aside, the problem you're running into, I think, is that there's an embedded single quote in your data:

json.png

If you remove that embedded single-quote you can, as you suggested, wrap the whole serialized JSON string in a pair of single quotes and copy/paste it into a Field Calculator processor's expression. The bigger challenge is going to be designing a GeoEvent Service that accepts data, extracts values from that data, and computes derivative values to place into a hierarchical JSON structure of this complexity.

-- RJ

 

AdamRepsher_BentEar
Frequent Contributor

So @RJSunderman ,

I have been thinking about this and do want to make my life simpler.😊

My task is to find a way to send alerts to a specific webhook.  But before I do that, I must put my data in a format that the webhook will accept.  I have the JSON schema and contentType (what I am interpreting as the Post body MIME Type). Let's just make the GeoEvent Definition match what the webhook wants - and then send it through a Push JSON to an External Website output...

Referencing the JSON in my original post - I create a Definition, starting with the JSON Object Name (I am interpreting "content" as the Object Name):

Definition.PNG

 

Even if I do have this wrong...  Well - I'll wait until the end.

I have an test CSV file and input created with a number of String fields - so I throw that into a Service with a Field Mapper Processor:

 FieldMapper.PNG

 

At this point, I create the output...

Output.PNG

 

 

I publish, and select my path from Field Mapper to the Output so that I can see any data, and run the Simulator.  I push one event that goes through the Input - but not past the Field Mapper.

What am I missing?  Even if my formulation of the Definition is incorrect for the webhook, shouldn't I see something anyway?  (I would like to figure out the correct Definition too. I may be missing something that has to do with Many instead of One in the buttons portion of the schema - and now that I look at it again, I may be missing it completely with objects)

Thanks,

--Adam

 

0 Kudos
RJSunderman
Esri Regular Contributor

Hey Adam --

The Field Mapper processor was designed to flatten a schema to make the event record's data structure compatible with the ArcGIS REST Services API used when sending processed event data as JSON to a feature service's addFeatures or updateFeatures endpoint.

You cannot use Field Mapper ... or any of the out-of-the-box processors ... to write data to a hierarchical structure. It appears, from your illustration, that event data being ingested is already adapted using flattened data structure (e.g. the cardinality of every event record attribute is '1' and the data type is DateDoubleLongString, (etc.) ... not Group).

I think you'll want to consider developing a custom outbound adapter which is able to take a flat data structure and adapt it into a hierarchical data structure expected by the Web Hook you want to receive data you've processed through a GeoEvent Service.

0 Kudos