WebexPythonSDK icon indicating copy to clipboard operation
WebexPythonSDK copied to clipboard

Attaching cards without AdaptiveCard(body, actions), but via JSON?

Open FlyingThunder opened this issue 3 years ago • 2 comments

I couldnt find anything on it, but with requests, i can make a POST to the message API, and just attach the JSON of a card as the data, which IMO is a better solution than building the card in the code (for big and more complex cards at least). This looks like this:

   cardBody = json.dumps({
  "roomId": f"{message.roomId}",
  "markdown": "[Tell us about yourself](https://www.example.com/form/book-vacation). We just need a few more details to get you booked for the trip of a lifetime!",
  "attachments": [
    ******BIG JSON STRUCTURE FOR THE CARD HERE********]})
    message_url = "https://webexapis.com/v1/messages"

    cardHeader = {
        'Authorization': f'Bearer {teams_token}',
        'Content-Type': 'application/json'
    }


    response = requests.request("POST", message_url, headers=cardHeader, data=cardBody)

Is this possible with the webexteamssdk? If yes, how? i tried api.messages.create(room.id, text="Fallback", attachments=[card_json]), where card_json is this card i got from a cisco example:

      "contentType": "application/vnd.microsoft.card.adaptive",
      "content": {
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "type": "AdaptiveCard",
        "version": "1.0",
        "body": [
          {
            "type": "ColumnSet",
            "columns": [
              {
                "type": "Column",
                "width": 2,
                "items": [
                  {
                    "type": "TextBlock",
                    "text": "Tell us about yourself",
                    "weight": "bolder",
                    "size": "medium"
                  },
                  {
                    "type": "TextBlock",
                    "text": "We just need a few more details to get you booked for the trip of a lifetime!",
                    "isSubtle": "True",
                    "wrap": "True"
                  },
                  {
                    "type": "TextBlock",
                    "text": "Für internen Nutzen.",
                    "isSubtle": "True",
                    "wrap": "True",
                    "size": "small"
                  },
                  {
                    "type": "TextBlock",
                    "text": "Feld 1",
                    "wrap": "True"
                  },
                  {
                    "type": "Input.Text",
                    "id": "Name",
                    "placeholder": "Beispieltext 1"
                  },
                  {
                    "type": "TextBlock",
                    "text": "Feld 2",
                    "wrap": "True"
                  },
                  {
                    "type": "Input.Text",
                    "id" : "Url",
                    "placeholder": "Beispieltext 2"
                  },
                  {
                    "type": "TextBlock",
                    "text": "Feld 3",
                    "wrap": "True"
                  },
                  {
                    "type": "Input.Text",
                    "id": "Email",
                    "placeholder": "Beispieltext 3",
                    "style": "email"
                  },
                  {
                    "type": "TextBlock",
                    "text": "Feld 4"
                  },
                  {
                    "type": "Input.Text",
                    "id": "Tel",
                    "placeholder": "Beispieltext 4",
                    "style": "tel"
                  }
                ]
              },
              {
                "type": "Column",
                "width": 1,
                "items": [
                  {
                    "type": "Image",
                    "url": "https://media.aubi-plus.com/files/institution/f48996e-avodaq-ag-logo.jpg",
                    "size": "auto"
                  }
                ]
              }
            ]
          }
        ],
        "actions": [
          {
            "type": "Action.Submit",
            "title": "Submit"
          }
        ]
      }
    }```

But in the end, it always just sends `Fallback`, i dont know why it fails though - no error message visible.

FlyingThunder avatar Sep 06 '21 10:09 FlyingThunder

Hi @FlyingThunder - I was able to take the example JSON you provided and replicate the issue on my side. Seems like there might be a soft syntax failure somewhere that's not generating an error. When I changed all of the "True" values in the JSON to true (lower case, no quotes) it worked just fine.

Here's the card JSON that worked for me:

{
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "type": "AdaptiveCard",
  "version": "1.0",
  "body": [
    {
      "type": "ColumnSet",
      "columns": [
        {
          "type": "Column",
          "width": 2,
          "items": [
            {
              "type": "TextBlock",
              "text": "Tell us about yourself",
              "weight": "bolder",
              "size": "medium"
            },
            {
              "type": "TextBlock",
              "text": "We just need a few more details to get you booked for the trip of a lifetime!",
              "isSubtle": true,
              "wrap": true
            },
            {
              "type": "TextBlock",
              "text": "Für internen Nutzen.",
              "isSubtle": true,
              "wrap": true,
              "size": "small"
            },
            {
              "type": "TextBlock",
              "text": "Feld 1",
              "wrap": true
            },
            {
              "type": "Input.Text",
              "id": "Name",
              "placeholder": "Beispieltext 1"
            },
            {
              "type": "TextBlock",
              "text": "Feld 2",
              "wrap": true
            },
            {
              "type": "Input.Text",
              "id" : "Url",
              "placeholder": "Beispieltext 2"
            },
            {
              "type": "TextBlock",
              "text": "Feld 3",
              "wrap": true
            },
            {
              "type": "Input.Text",
              "id": "Email",
              "placeholder": "Beispieltext 3",
              "style": "email"
            },
            {
              "type": "TextBlock",
              "text": "Feld 4"
            },
            {
              "type": "Input.Text",
              "id": "Tel",
              "placeholder": "Beispieltext 4",
              "style": "tel"
            }
          ]
        },
        {
          "type": "Column",
          "width": 1,
          "items": [
            {
              "type": "Image",
              "url": "https://media.aubi-plus.com/files/institution/f48996e-avodaq-ag-logo.jpg",
              "size": "auto"
            }
          ]
        }
      ]
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "title": "Submit"
    }
  ]
}

I then threw that into a local file called card.json and used the following:

api = WebexTeamsAPI()

card_json = json.load(open('card.json', 'r'))

mycard = {"contentType": "application/vnd.microsoft.card.adaptive", "content": card_json}

api.messages.create(room.id, text="Card", attachments=[mycard])

0x2142 avatar Sep 17 '21 21:09 0x2142

This works, but for my cards, I've been using the https://github.com/CiscoSE/pyadaptivecards library. I find that this saves me more lines of code, and easier to read as well.

awolder avatar Oct 05 '21 09:10 awolder

Closing this issue; it appears to have been a JSON formatting issue.

cmlccie avatar Feb 19 '24 18:02 cmlccie