[使用1.2版本在python中发送带有数据的自适应卡

问题描述 投票:0回答:1

基于以下内容,我有一个卡模板和数据分别作为json。

https://adaptivecards.io/samples/FlightItinerary.html

但是我不确定如何通过python为下面的卡发送数据。我找不到用于python的模板和数据的文档示例。如何通过python将数据发送到此卡

    # code for adaptive card
    reply = MessageFactory.list([])
    reply.attachment_layout = AttachmentLayoutTypes.carousel
    reply.attachments.append(CardFactory.adaptive_card(LIST_JOB_CARD_CONTENT))
    await step_context.context.send_activity(reply)
LIST_JOB_CARD_CONTENT ={
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "Job Number",
"size": "Small",
"weight": "Bolder",
"color": "Accent"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "Job Status",
"fontType": "Default",
"size": "Small",
"color": "Accent",
"weight": "Bolder",
"horizontalAlignment": "Center"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "Design Reference",
"weight": "Bolder",
"color": "Accent",
"size": "Small",
"horizontalAlignment": "Right"
}
]
}
],
"style": "default",
"spacing": "Small",
"horizontalAlignment": "Center",
"height": "stretch",
"minHeight": "5px"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "${$root.JobNumber}",
"size": "Medium",
"color": "Good",
"weight": "Bolder"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "${$root.jobStatus}",
"weight": "Bolder",
"color": "Attention",
"size": "Medium",
"horizontalAlignment": "Center"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "${$root.DesignReference}",
"size": "Medium",
"weight": "Bolder",
"color": "Good",
"horizontalAlignment": "Center"
}
]
}
],
"style": "default",
"spacing": "Small",
"horizontalAlignment": "Center",
"height": "stretch"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "250px",
"items": [
{
"type": "TextBlock",
"text": "Customer Name",
"size": "Medium",
"weight": "Bolder",
"color": "Accent",
"horizontalAlignment": "Center",
"spacing": "Medium"
}
],
"backgroundImage": {
"horizontalAlignment": "Center"
},
"spacing": "Small"
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "Promotion",
"horizontalAlignment": "Center",
"size": "Medium",
"weight": "Bolder",
"color": "Accent"
}
]
}
],
"id": "CustomerName"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "250px",
"items": [
{
"type": "TextBlock",
"horizontalAlignment": "Center",
"text": "${$root.CustomerName}",
"wrap": True,
"size": "Medium",
"fontType": "Monospace",
"color": "Good",
"weight": "Lighter"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "${$root.Promotion}",
"horizontalAlignment": "Center",
"fontType": "Monospace",
"size": "Medium",
"color": "Good"
}
]
}
]
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "50px",
"items": [
{
"type": "TextBlock",
"text": "Brand->",
"size": "Small",
"weight": "Bolder",
"color": "Accent"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "${$root.Brand}",
"id": "brand",
"wrap": True,
"size": "Medium",
"color": "Warning",
"horizontalAlignment": "Right"
}
]
}
]
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "100px",
"items": [
{
"type": "TextBlock",
"text": "RangeName->",
"fontType": "Default",
"size": "Small",
"weight": "Bolder",
"color": "Accent"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "${$root.RangeName}",
"size": "Medium",
"color": "Warning",
"horizontalAlignment": "Right"
}
]
}
]
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "60px",
"items": [
{
"type": "TextBlock",
"text": "Variety->",
"size": "Small",
"weight": "Bolder",
"color": "Accent"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "${$root.Variety}",
"horizontalAlignment": "Right",
"size": "Medium",
"color": "Warning"
}
]
}
]
}
]
}



  [1]: https://i.stack.imgur.com/YkjO4.png
python azure-bot-service adaptive-cards
1个回答
0
投票

在json文件(即FlightItenerary.json)中创建您的自适应卡,然后使用类似以下的内容:

import json
import os
import random

from botbuilder.core import ActivityHandler, TurnContext, CardFactory
from botbuilder.schema import ChannelAccount, Attachment, Activity, ActivityTypes

CARDS = [
    "resources/FlightItineraryCard.json"
]


class AdaptiveCardsBot(ActivityHandler):

    async def on_message_activity(self, turn_context: TurnContext):
        message = Activity(
            text="Here is an Adaptive Card:",
            type=ActivityTypes.message,
            attachments=[self._create_adaptive_card_attachment()],
        )

        await turn_context.send_activity(message)

    def _create_adaptive_card_attachment(self) -> Attachment:
        card_path = os.path.join(os.getcwd(), CARDS[0])
        with open(card_path, "rb") as in_file:
            card_data = json.load(in_file)

        return CardFactory.adaptive_card(card_data)

这是来自官方机器人框架示例07. Using Adaptive Cards的更改后的代码,>

© www.soinside.com 2019 - 2024. All rights reserved.