使用 1.2 版本发送 python 中数据的适配卡

Send a adaptive card with data in python using 1.2 version

基于以下,我有一个卡片模板和数据分别为 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

在 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)

这是来自官方 bot 框架示例的更改代码 07. Using Adaptive Cards

我找不到使用 Python SDK 将数据传递到 JSON 模板的方法。但是,我能够通过以下方式解决这个问题:

  1. 在您的 .json 文件中添加标签 (${key_name}) 以添加从 python:
  2. 传入的值
{
    "text": "${name}"
}
  1. 创建一个接收字典(已解析 json)和包含要插入到 .json 文件中的 key_namevalue 对的字典的函数:
def replace(template: dict, data:dict):
        import re
        str_temp = str(template)
        for key in data:
            pattern = "${"+key+"}"
            str_temp = re.sub(pattern, str(data[key]),str_temp)        
        return eval(str_temp)

  1. 使用函数替换.json文件中的所有标签:
def create_adaptive_card_attachment(self):
        # read the .json file
        relative_path = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(relative_path, "../cards/my_card.json")
        with open(path) as in_file:
            card_template = json.load(in_file)
        template_data = {"name":"Obi-Wan"}
        card =replace(card_template,template_data)
        
        return Attachment(
            content_type="application/vnd.microsoft.card.adaptive", content=card
        )
  1. 将卡片发送给用户:
card = self.create_adaptive_card_attachment()
            response = MessageFactory.attachment(card)
            await step_context.context.send_activity(response)