在C#中的Bot Framework v4中更新已发布的自适应卡 [英] Update already posted Adaptive Card in Bot Framework v4 in C#

查看:50
本文介绍了在C#中的Bot Framework v4中更新已发布的自适应卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#中的Bot Framework v4开发Bot.我现在想要的是,在使用操作将自适应卡发送给用户之后,可以在触发操作后更新该卡.例如,该按钮将在单击后消失.

在我从SharePoint库中使用MS Graph搜索之后,将生成此卡片.

主要目标是,我可以使用精炼机通过JSON中的新卡片来更新卡片.确定"按钮是一个提交动作,可以在OnMessageActivityAsync方法中捕获该动作.输入值在活动值中,因此可以创建过滤方法.我的问题是,我无法更新已经发送给用户的卡.

在我将带有结果的第一张卡片发送给用户之前,我将活动写入状态,这样我就可以访问OnMessageActivityAsync方法,但是我不确定这是否是正确的方法.

 受保护的重写异步任务OnMessageActivityAsync(ITurnContext< IMessageActivity> turnContext,CancellationToken cancelledToken){Logger.LogInformation(正在运行带有消息活动的对话框.");var txt = turnContext.Activity.Text;动态val = turnContext.Activity.Value;//检查活动是否来自提交动作如果(string.IsNullOrEmpty(txt)&&val!= null){等待turnContext.SendActivityAsync(MessageFactory.Text($"Refiner Language:{val.id_language}")));等待turnContext.SendActivityAsync(MessageFactory.Text($"Refiner MachType:{val.id_type}")));var r = new StreamReader("Cards/helloCard2.json");字符串json = r.ReadToEnd();AdaptiveCard卡= AdaptiveCard.FromJson(json).Card;var docSearchState =等待StateAccessor.GetAsync(turnContext);活动activity = docSearchState.Activity;activity.Attachments = new List< Attachment>(){新的Attachment(){名称=卡",ContentType = AdaptiveCard.ContentType,内容=卡,}};等待turnContext.UpdateActivityAsync(activity);} 

此代码给我以下错误消息:

 失败:Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter [0]捕获到异常:从JsonReader读取JArray时出错.当前JsonReader项不是数组:StartObject.路径"DocumentSearchState.Activity.attachments.$ values [0] .content.body". 

有人有解决这个问题的好主意吗?

解决方案

看看 Bot.Builder.Community.AdaptiveCards

  • AdaptiveCard提示
  • I'm working on a Bot with the Bot Framework v4 in C#. What I want now is, that after sending an Adaptive Card to the user, with Actions, that I can update this card after the Action is fired. For example, that the button will disappear after the click.

    Link to NodeJS Solution

    here I have a Solution in NodeJS, but I'm not able to transfer it to C#. Can anybody help?

    More Project details: I have an Adaptive Card that looks like this:

    This Card will be generated, after I searched with the MS Graph from a SharePoint Library.

    The Main Goal is, that i can use the Refiners to update the Card with a new Card from JSON. The "OK" button is a Submit Action which can be catched in the OnMessageActivityAsync Method. The Input Values are in the Activity Value so can create a Filter Method. my Problem is, that I can't update the Card that is already send to the User.

    Before I sended the first Card with the Results to the User I write the Activity into a State, so I can Access OnMessageActivityAsync Method, but I'm not sure if this is the right approach.

    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            Logger.LogInformation("Running dialog with Message Activity.");
    
            var txt = turnContext.Activity.Text;
            dynamic val = turnContext.Activity.Value;
            // Check if the activity came from a submit action
            if (string.IsNullOrEmpty(txt) && val != null)
            {
                await turnContext.SendActivityAsync(MessageFactory.Text($"Refiner Language: {val.id_language}"));
                await turnContext.SendActivityAsync(MessageFactory.Text($"Refiner MachType: {val.id_type}"));
    
                var r = new StreamReader("Cards/helloCard2.json");
                string json = r.ReadToEnd();
                AdaptiveCard card = AdaptiveCard.FromJson(json).Card;
    
                var docSearchState = await StateAccessor.GetAsync(turnContext);
                Activity activity = docSearchState.Activity;
                activity.Attachments = new List<Attachment>() {
                    new Attachment()
                    {
                        Name = "Card",
                        ContentType = AdaptiveCard.ContentType,
                        Content = card,
                    }
                };
    
                await turnContext.UpdateActivityAsync(activity);
            }
    

    This Code gives me the following Error Message:

    fail: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      Exception caught : Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path 'DocumentSearchState.Activity.attachments.$values[0].content.body'.
    

    Has anybody a good idea to solve this?

    解决方案

    Have a look at this answer to get an idea of how to update Adaptive Cards.

    You can either write code to remove the submit action from an Adaptive Card dynamically, or you can have two different versions of your card: one with the submit action and one without.

    If you want this whole process to be made easier with prebuilt code that you can install in NuGet packages, feel free to voice your support for these ideas on GitHub:

    这篇关于在C#中的Bot Framework v4中更新已发布的自适应卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆