首页 > 解决方案 > Search Message Extension 的 Select Item 事件上的自适应卡片

问题描述

在搜索消息扩展中选择搜索结果时,我正在尝试返回自适应卡片。以下是我退卡的课程:

import { IEntity } from '../model/IEntity';
import { Attachment, CardFactory } from "botbuilder";

export class AccountResultCard {

    public static getCard(account: IEntity): Attachment {

        // Get map
        let logo = `https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg`;
        let url = `https://pbs.twimg.com/docs/test.aspx?GoPage=AccountCard.aspx?AccountID='${account.ID}'&_Division_='${account.Division}'`;
        const accountDetail = account.Code ? account.Code + ' - '  + account.Description : '';
        const accountcontextinfo1 = account.ContextInfo[1] ? account.ContextInfo[1] : '';
        const accountcontextinfo2 = account.ContextInfo[3] ? account.ContextInfo[3] + ' '+ account.ContextInfo[2] : '';

        
        const card =  CardFactory.adaptiveCard(
            {
                "type": "AdaptiveCard",
                "body": [
                    {
                        "type": "TextBlock",
                        "size": "Large",
                        "weight": "Bolder",
                        "text": "Title"
                    },
                    {
                        "type": "TextBlock",
                        "size": "Medium",
                        "weight": "Bolder",
                        "text": accountDetail
                    },
                    {
                        "type": "ColumnSet",
                        "columns": [
                            {
                                "type": "Column",
                                "items": [
                                    {
                                        "type": "TextBlock",
                                        "text": accountcontextinfo1,
                                        "wrap": true
                                    },
                                    {
                                        "type": "TextBlock",
                                        "spacing": "None",
                                        "text": accountcontextinfo2,
                                        "wrap": true
                                    },
                                    {
                                        "type": "TextBlock",
                                        "spacing": "None",
                                        "text": "Missing state and country",
                                        "wrap": true
                                    }
                                ],
                                "width": "stretch"
                            },
                            {
                                "type": "Column",
                                "items": [
                                    {
                                        "type": "Image",
                                        "style": "Person",
                                        "url": logo,
                                        "size": "Small"
                                    }
                                ],
                                "width": "auto"
                            }
                        ]
                    }
                ],
                "actions": [
                    {
                        "type": "Action.OpenUrl",
                        "title": "Open in Aurora",
                        "url": url,
                        "width": "Small",
                        "style": "default"
                    }
                ],
                "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
                "version": "1.2"
            }
        );

        return card;
    }
}

以下是我的选择项目事件:

 public async handleTeamsMessagingExtensionSelectItem(
    context: TurnContext,
    obj: any
  ): Promise<any> {
   const item = this.response.find(e => e.ID == obj.name);
    return {
      composeExtension: {
        type: "result",
        attachmentLayout: "list",
        attachments: [AccountResultCard.getCard(item)],
      },
    };
  }

但它永远不会返回任何东西。我从 get card 中删除了所有内容,只发送了一个带有硬编码值的文本块。那也没用。你们能建议我哪里做错了吗?

标签: botframeworkmicrosoft-teamsadaptive-cardsimessage-extension

解决方案


通过将代码更改为,我能够在 SelectItem 事件中发送自适应卡:

return {
  composeExtension: {
    type: "result",
    attachmentLayout: "list",
    attachments: [{preview: CardFactory.heroCard("title", ""),
      ...yourAdaptiveCard}],
  },
};

这已经回答here


推荐阅读