Dynamically generating adaptive cards

66 Views Asked by At

I've been getting my butt handed to me by MS Teams and adaptive cards. The problem I'm having is that I need to dynamically generate the form of an adaptive card that will post to MS Teams using an incoming webhook. I can post a simple card, and even one with nested objects with simple styling, but when it comes to the formatting it all just looks like a long list without any differentiation between the items. For example, I may generate a card where there is an object with an unknown number of fields with unknown names that also contains an unknown number of nested objects/arrays, but when it posts to Teams it's just one long list without any formatting....all items in every object appear identically. How can I get the function below to format the output to Teams like traditional json, or at least in a way that separates the nested and parent objects without multiple cards?

export function contextToFormattedAdCard(objToParse: any): Array<Record<string, any>> {
  const attachments: Array<Record<string, any>> = [
    {
      "contentType":"application/vnd.microsoft.card.adaptive",
      "contentUrl":null,
      "content":{
        "$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
        "type":"AdaptiveCard",
        "version":"1.2",
        "body":[

        ]
      }
    }
  ];

  let newCard = {
    "type":"Message",
    "version":"1.2",
    "body":[]
  }

  function processObject(objToFormat: Record<string, any>): any {
    for (const key in objToFormat) {
      const value = objToFormat[key];
      const keyValuePair = `${key} : ${value}`;

      if (typeof value === 'object') {

        const nestedObj =  {
          type: "Container",
          items: [
            {
              contentType: "application/vnd.microsoft.card.adaptive",
              content: {
                $schema: "http://adaptivecards.io/schemas/adaptive-card.json",
                type: "AdaptiveCard",
                version: "1.2",
                body: contextToFormattedAdCard(value),
              },
            },
          ],
        };
        attachments[0].content.body.push(nestedObj);
      } else {

        attachments[0].content.body.push({
          type: "TextBlock",
          text: keyValuePair,
          wrap: true,
        });
      }
    }
  }

  processObject(objToParse);
  return attachments;
}

I was expecting the output to put out an object organized similar to JSON.stringify(value, null, 2) or something fairly comparable.

0

There are 0 best solutions below