使用Visual C#在BOT框架中渲染的自适应卡SDK V4

发布于 2025-01-17 22:06:35 字数 2870 浏览 1 评论 0原文

您好,我已经使用机器人框架 SDK v4 中的核心机器人模板开发了一个聊天机器人,并将其发布在我的 Azure 门户中,并且我能够在网络聊天中打开它。我正在尝试向我的自适应卡添加样式,并且我已检查 Link ,但我无法弄清楚如何渲染我的卡并为其设置 hostconfig.json 。

这是我的 bot.cs 文件

  '''


  using Microsoft.Bot.Builder;
  using Microsoft.Bot.Builder.Dialogs;
  using Microsoft.Bot.Schema;
  using Microsoft.Extensions.Logging;
  using Newtonsoft.Json;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Threading;
  using System.Threading.Tasks;

   namespace asign.Bots
  {
   public class DialogAndWelcomeBot<T> : DialogBot<T>
     where T : Dialog
  {
      public DialogAndWelcomeBot(ConversationState conversationState, UserState 
    userState, T dialog, ILogger<DialogBot<T>> logger)
         : base(conversationState, userState, dialog, logger)
     {
     }

     protected override async Task OnMembersAddedAsync(IList<ChannelAccount> 
   membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, 
  CancellationToken 
cancellationToken)
    {
        foreach (var member in membersAdded)
        {
            // Greet anyone that was not the target (recipient) of this message.
            // To learn more about Adaptive Cards, see https://aka.ms/msbot- 
   adaptivecards for more details.
            if (member.Id != turnContext.Activity.Recipient.Id)
            {
                var welcomeCard = CreateAdaptiveCardAttachment();
                var response = MessageFactory.Attachment(welcomeCard, ssml: "Welcome 
   to Bot Framework!");
                await turnContext.SendActivityAsync(response, cancellationToken);
                await Dialog.RunAsync(turnContext, 
   ConversationState.CreateProperty<DialogState>("DialogState"), cancellationToken);
            }
        }
    }

    // Load attachment from embedded resource.
    private Attachment CreateAdaptiveCardAttachment()
    {
        var cardResourcePath = 
   GetType().Assembly.GetManifestResourceNames().First(name => 
   name.EndsWith("welcomeCard.json"));

        using (var stream = 
  GetType().Assembly.GetManifestResourceStream(cardResourcePath))
        {
            using (var reader = new StreamReader(stream))
            {
                var adaptiveCard = reader.ReadToEnd();
                return new Attachment()
                {
                    ContentType = "application/vnd.microsoft.card.adaptive",
                    Content = JsonConvert.DeserializeObject(adaptiveCard),
                };
            }
        }
      }
    }
  }

   '''

Please help me understand how to add a rendering method to above code and setup hostconfig. I would really appriciate the solution.

Hi I have developed a chatbot using core bot template in bot framework SDK v4 and I have published it in my azure portal, and Im able to open it in webchat. I am trying to add styles to my adaptive card and I have checked Link , but I am not able to figure out how to render my card and set up the hostconfig.json to it.

this is my bot.cs file

  '''


  using Microsoft.Bot.Builder;
  using Microsoft.Bot.Builder.Dialogs;
  using Microsoft.Bot.Schema;
  using Microsoft.Extensions.Logging;
  using Newtonsoft.Json;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Threading;
  using System.Threading.Tasks;

   namespace asign.Bots
  {
   public class DialogAndWelcomeBot<T> : DialogBot<T>
     where T : Dialog
  {
      public DialogAndWelcomeBot(ConversationState conversationState, UserState 
    userState, T dialog, ILogger<DialogBot<T>> logger)
         : base(conversationState, userState, dialog, logger)
     {
     }

     protected override async Task OnMembersAddedAsync(IList<ChannelAccount> 
   membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, 
  CancellationToken 
cancellationToken)
    {
        foreach (var member in membersAdded)
        {
            // Greet anyone that was not the target (recipient) of this message.
            // To learn more about Adaptive Cards, see https://aka.ms/msbot- 
   adaptivecards for more details.
            if (member.Id != turnContext.Activity.Recipient.Id)
            {
                var welcomeCard = CreateAdaptiveCardAttachment();
                var response = MessageFactory.Attachment(welcomeCard, ssml: "Welcome 
   to Bot Framework!");
                await turnContext.SendActivityAsync(response, cancellationToken);
                await Dialog.RunAsync(turnContext, 
   ConversationState.CreateProperty<DialogState>("DialogState"), cancellationToken);
            }
        }
    }

    // Load attachment from embedded resource.
    private Attachment CreateAdaptiveCardAttachment()
    {
        var cardResourcePath = 
   GetType().Assembly.GetManifestResourceNames().First(name => 
   name.EndsWith("welcomeCard.json"));

        using (var stream = 
  GetType().Assembly.GetManifestResourceStream(cardResourcePath))
        {
            using (var reader = new StreamReader(stream))
            {
                var adaptiveCard = reader.ReadToEnd();
                return new Attachment()
                {
                    ContentType = "application/vnd.microsoft.card.adaptive",
                    Content = JsonConvert.DeserializeObject(adaptiveCard),
                };
            }
        }
      }
    }
  }

   '''

Please help me understand how to add a rendering method to above code and setup hostconfig. I would really appriciate the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文