在Python版Bot Framework中添加Adaptive Card的实现疑问
Got it, let's walk through exactly how to attach that Adaptive Card to your bot's response using Bot Builder Python. You're right that await context.send_activity() is where you'll make the change—instead of sending a plain text string, you'll send an Activity object that includes your card as an attachment.
Step 1: Import Required Classes
First, make sure you have these imports at the top of your file:
import json from botbuilder.core import Activity, Attachment
Step 2: Prepare Your Adaptive Card
You can use your existing JSON directly as a Python dictionary (I've included your full card below for convenience):
# Your Adaptive Card JSON (converted to a Python dictionary) adaptive_card = { "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", "version": "1.0", "body": [ { "type": "Container", "items": [ { "type": "TextBlock", "text": "Publish Adaptive Card schema", "weight": "bolder", "size": "medium" }, { "type": "ColumnSet", "columns": [ { "type": "Column", "width": "auto", "items": [ { "type": "Image", "url": "https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg", "size": "small", "style": "person" } ] }, { "type": "Column", "width": "stretch", "items": [ { "type": "TextBlock", "text": "Matt Hidinger", "weight": "bolder", "wrap": true }, { "type": "TextBlock", "spacing": "none", "text": "Created {{DATE(2017-02-14T06:08:39Z, SHORT)}}", "isSubtle": true, "wrap": true } ] } ] } ] }, { "type": "Container", "items": [ { "type": "TextBlock", "text": "Now that we have defined the main rules and features of the format, we need to produce a schema and publish it to GitHub. The schema will be the starting point of our reference documentation.", "wrap": true }, { "type": "FactSet", "facts": [ { "title": "Board:", "value": "Adaptive Card" }, { "title": "List:", "value": "Backlog" }, { "title": "Assigned to:", "value": "Matt Hidinger" }, { "title": "Due date:", "value": "Not set" } ] } ] } ], "actions": [ { "type": "Action.ShowCard", "title": "Set due date", "card": { "type": "AdaptiveCard", "body": [ { "type": "Input.Date", "id": "dueDate" } ], "actions": [ { "type": "Action.Submit", "title": "OK" } ] } }, { "type": "Action.ShowCard", "title": "Comment", "card": { "type": "AdaptiveCard", "body": [ { "type": "Input.Text", "id": "comment", "isMultiline": true, "placeholder": "Enter your comment" } ], "actions": [ { "type": "Action.Submit", "title": "OK" } ] } } ] }
Step 3: Convert the Card to an Attachment
Wrap your card in an Attachment object with the correct content type—this is critical for the bot framework to recognize it as an Adaptive Card:
card_attachment = Attachment( content_type="application/vnd.microsoft.card.adaptive", content=adaptive_card )
Step 4: Create and Send the Activity
Instead of sending a plain string, create an Activity that includes both fallback text (for channels that don't support Adaptive Cards) and your attachment:
# Build the response activity response_activity = Activity( text="Check out this Adaptive Card!", # Fallback text for non-supporting channels attachments=[card_attachment] ) # Send the activity await context.send_activity(response_activity)
Key Notes:
- Content Type: Always use
application/vnd.microsoft.card.adaptive—this tells the bot framework and channels exactly what kind of card you're sending. - Fallback Text: The
textfield in the Activity will be displayed if the channel doesn't support Adaptive Cards (like SMS), so make sure it's descriptive for those users. - Testing: Use the Bot Framework Emulator to test your card—it renders Adaptive Cards properly so you can verify the layout and functionality before deploying.
内容的提问来源于stack exchange,提问作者Alejandro Peña

