Search code examples
botframeworkbot-framework-composer

how can I compose a bot to iterate trough a xml json object?


I am using the composer to publish a bot to fetch data from an azure storage table. In short, the bot composer needs to construct a bot to iterate through an XML deserialized JSON object returned by the azure storage rest API.

In my code generated by the composer, the bot does a "set property" step immediately following the successful return of the REST API (storage table query). Given the deserialized object returned by the storage REST API, how should the "set property" statement be constructed so the bot can print our the individual data field,

Another way to phrase the question: how can I use the composer to construct the bot to iterate through a returned deserialized object (coded in XML JSON format)?

Where can I find a document that can shed some light on this matter? Is there any place I can find a good example? Can it be done via composer?

Thanks in advance.


Solution

  • Yes, it can be done. If the API returns XML, make sure you configure your api call to ask for content type application/xml.

    Then you can use use the xPath built in function. Make note that it will return an array if results in more than value matches the expression, in which you can use the foreach function to iterate over it with. I needed to run the nightly build of Composer (with bot-builder 4.12.0) to get it to work for me. See here for some more info: https://github.com/microsoft/botbuilder-js/pull/3093

    Here's an example that worked for me:

    
        "actions": [
        {
          "$kind": "Microsoft.SendActivity",
          "$designer": {
            "id": "rGv7XC"
          },
          "activity": "${SendActivity_rGv7XC()}"
        },
        {
          "$kind": "Microsoft.HttpRequest",
          "$designer": {
            "id": "TDA1wO"
          },
          "method": "GET",
          "url": "http://www.geoplugin.net/xml.gp?ip=157.54.54.128",
          "resultProperty": "dialog.api_response",
          "contentType": "application/xml"
        },
        {
          "$kind": "Microsoft.SetProperty",
          "$designer": {
            "id": "ipNhfY"
          },
          "property": "dialog.timezone",
          "value": "=xPath(dialog.api_response.content,'/geoPlugin/geoplugin_timezone/text()')"
        },
        {
          "$kind": "Microsoft.SendActivity",
          "$designer": {
            "id": "DxohEx"
          },
          "activity": "${SendActivity_DxohEx()}"
        }
        ]
    
    

    You can (if needed/you wish) use the json and jPath built in functions to convert xml to json and then query with. Something like:

    
        ${json(user.testXml)} and then
        ${jPath(user.testJson , "automobiles")}