<@ULVA73B9P> can I include AdaptiveCard JSON in th...
# ask-marvin
j
@Marvin can I include AdaptiveCard JSON in the body of my MS Teams notification?
m
To include an AdaptiveCard JSON in a Microsoft Teams notification, you typically need to use a connector, bot, or the Microsoft Power Automate (Flow) with an appropriate action that supports Adaptive Cards. Here's a brief overview of how it can be done: 1. Adaptive Cards in Bots or Custom Apps: If you're developing a Teams bot or an app, you can include Adaptive Cards as part of the messaging payload. This is done by sending a
message
that contains an
attachments
array with a card of type
AdaptiveCard
. 2. Using Microsoft Power Automate (Flow): You can create a flow that sends Adaptive Card notifications to Teams using the "Post an Adaptive Card to a Teams user and wait for a response" action or similar. You need to provide the JSON definition of the Adaptive Card in the action. Ensure your JSON is valid for rendering an Adaptive Card and that you send it to a channel or user authorized to receive messages from your app or flow. Remember, you can't send Adaptive Cards directly through simple webhook messages without additional external handling or a supporting configuration (like a bot or app service) that listens and processes those cards. If you're planning to integrate Prefect with Teams to send these notifications, using Prefect 2.x or 3.x, you might want to explore sending notifications from tasks or flows using external service integrations like webhooks or custom Python logic. If you need further guidance on integrating Prefect with Microsoft Teams, let me know which version of Prefect you're using so I can provide more specific help!
j
@Marvin I'm asking if within my automation's "send a notification" action using a Microsoft Teams Webhook block, can I specify the AdaptiveCard with JSON rather than using a jinja2 template
m
To include an AdaptiveCard JSON directly within a notification using a Microsoft Teams Webhook block, you typically need to ensure that the webhook payload can interpret the JSON structure of the AdaptiveCard. In many automation tools, support for formatting options such as Jinja2 or direct JSON might depend on the tool's capabilities and how it processes webhook payloads. Here's how you can generally approach this: 1. Direct JSON in Webhook: If the webhook action allows you to input a raw JSON payload, you can place your AdaptiveCard JSON directly in the body of your webhook request. Make sure that the JSON is formatted correctly according to the AdaptiveCard schema. 2. Template Processing: If your tool supports template processing (like Jinja2) but you wish to avoid it, you'll need to check if there's an option to disable templating or interpret the input as raw JSON. In some cases, you might use a direct JSON block instead of a template block. 3. Validate JSON: Ensure your AdaptiveCard JSON is valid. You can use online tools or the Adaptive Cards Designer to validate and preview your card. Since you're using a Prefect block for sending notifications, the ability to specify raw JSON might depend on how the Prefect Microsoft Teams Webhook block is implemented. If you're using a specific Prefect version, let me know, and I can check the documentation for you, or check Prefect community repositories for solutions or examples involving JSON payloads instead of Jinja2 templates.
j
@Marvin let's try this again. if I specify the body as:
Copy code
# Hello from Prefect!
the teams webhook will send this payload:
Copy code
[
  {
    "contentType": "application/vnd.microsoft.card.adaptive",
    "contentUrl": null,
    "content": {
      "$schema": "<http://adaptivecards.io/schemas/adaptive-card.json>",
      "type": "AdaptiveCard",
      "version": "1.4",
      "body": [
        {
          "type": "Image",
          "url": "<https://github.com/caronc/apprise/raw/master/apprise/assets/themes/default/apprise-failure-32x32.png>",
          "height": "32px",
          "altText": "failure"
        },
        {
          "type": "TextBlock",
          "text": "# Hello from Prefect!",
          "style": "default",
          "wrap": true,
          "id": "body"
        }
      ],
      "msteams": {
        "width": "full"
      }
    }
  }
]
I want to know if I can override this and send my own payload via the body form, or if I will have to skip the teams webhook and construct my own request
m
If the Microsoft Teams Webhook block in Prefect automatically wraps your message in a predefined JSON structure for an AdaptiveCard, it may not allow you to directly override it within the block itself. This behavior often depends on how the webhook functionality is implemented in Prefect. Here's what you can explore: 1. Override Within Prefect: Check if there are any advanced settings or a custom configuration option in the Microsoft Teams Webhook block that allows you to input raw JSON instead of automatically wrapping it. Documentation or community forums might provide details if this feature exists. 2. Use a Different Method: If the Microsoft Teams Webhook block doesn't support sending raw JSON payloads, you might need to consider constructing your own HTTP request. You can use a Prefect
Task
with Python's
requests
library to send a fully customized payload to the Teams webhook URL. 3. Custom Request in Prefect: Here is a brief idea of how you can construct a custom request using a Prefect task:
Copy code
python
   import requests
   from prefect import task, Flow

   @task
   def send_custom_teams_notification(webhook_url, custom_payload):
       headers = {"Content-Type": "application/json"}
       response = <http://requests.post|requests.post>(webhook_url, json=custom_payload, headers=headers)
       response.raise_for_status()
       return response.json()

   # Define your custom AdaptiveCard JSON payload
   custom_payload = {
       "type": "message/card",
       "attachments": [
           {
               "contentType": "application/vnd.microsoft.card.adaptive",
               "content": {
                   # Your custom card content here
               }
           }
       ]
   }

   with Flow("custom-teams-notification-flow") as flow:
       send_custom_teams_notification("<your_webhook_url>", custom_payload)

   flow.run()
3. Consult Prefect Resources: If you're using Prefect 2.x or 3.x, consider consulting Prefect's documentation or the Prefect community on GitHub or Slack for insights about the webhook block's capabilities regarding customizing payloads. If you're using a specific version of Prefect and need more tailored guidance or want me to look into the Prefect documentation or GitHub issues, please let me know your Prefect version!