如何从LUIS中的自适应卡中获取值? C#

时间:2018-08-24 09:30:26

标签: c# submit botframework luis adaptive-cards

我目前正在研究在VSTS中创建项目的LUIS机器人。 现在,您只需将名称(如“ Create Projekt abcd”)写入机器人,它就会为您创建项目。 我想通过为输入添加自适应卡来使其看起来更好,但是当我按下“提交”按钮时,它只是对Bot代码说了一个错误。 我做了一些研究,但问题似乎是LUIS bot不知道如何处理作为消息返回的对象。

自适应卡:`

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AdaptiveCards;
using AdaptiveCards.Rendering;
using AdaptiveCards.Rendering.Html;
using Microsoft.Bot.Builder.FormFlow;

namespace LuisBot.Dialogs
{
public class ProjektInputCard
{


    public static Attachment GetCard(String pName)
    {
        String projektname = pName;
        if (projektname.Equals("(Name not found)"))
        {
            projektname = "";
        }

        AdaptiveCard Card = new AdaptiveCard() 
        {
            Body = new List<AdaptiveElement>()
            {
                new AdaptiveContainer()
                {
                    Items = new List<AdaptiveElement>()
                    {
                        new AdaptiveTextBlock()
                        {
                            Text = "Projekterstellung",
                            Weight = AdaptiveTextWeight.Bolder,
                            Size = AdaptiveTextSize.Large
                        },
                        new AdaptiveTextBlock()
                        {
                            Text = "Projektname:",
                            Weight = AdaptiveTextWeight.Bolder,
                            Size = AdaptiveTextSize.Default
                        },
                        new AdaptiveTextInput()
                        {
                            Type = "Input.Text",
                            Id = "ID_projekt",
                            Value = projektname
                        },
                        new AdaptiveTextBlock()
                        {
                            Text = "Beschreibung:",
                            Weight = AdaptiveTextWeight.Bolder,
                            Size = AdaptiveTextSize.Default
                        },

                        new AdaptiveTextInput()
                        {
                            Type = "Input.Text",
                            Id = "ID_description",
                            Value = "",
                            IsMultiline = true
                        }
                    }


                }



            }


        };

        Card.Actions = new List<AdaptiveAction>()
        {
            new AdaptiveSubmitAction()
            {
                Type = "Action.Submit",
                Title = "Erstellen"
            }
        };

        Attachment Attach = new Attachment() 
        {
            ContentType = AdaptiveCard.ContentType,
            Content = Card
        };

        return Attach;
    }
}
}`

调用卡片的方法:`

private async Task Test(IDialogContext context)
     {

        var createprompt = context.MakeMessage();
    createprompt.Attachments.Add(ProjektInputCard.GetCard(GetProjectName()));
        await context.PostAsync(createprompt);

        context.Wait(MessageReceivedAsync);
    }`

MessageReceived方法:

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var message = await result;
        InputValues data;
        if (message.Value != null)
        {
            // Got an Action Submit
            dynamic value = message.Value;
            string submitType = value.Type.ToString();
            if (value != null)
            {
                data = Newtonsoft.Json.JsonConvert.DeserializeObject<InputValues>(submitType);
                _projectname = data.Name;
                _description = data.Description;
                await this.ShowLuisResult(context);

            }

        }
    }

1 个答案:

答案 0 :(得分:1)

Ceepert, 与其尝试在同一个类中尝试全部操作(我想这就是我在代码中看到的),您要做的是将自适应卡零件和luis零件分离到单独的对话框中。您的初始对话框将是常规的IDialog <>实现。

从输入中收集数据,使用自适应卡中的数据作为消息的Text属性创建一条新消息,并调用上下文。将新消息发送到luis对话框。 从您的代码尚不清楚,Luis将使用AdaptiveCard的哪个输入来确定用户的意图,因此我在示例中假定为'_projectname'

如果“文本”之外还有其他数据,则可以将其作为参数传递给Luis对话框构造函数。

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var message = await result;

    if (message.Value != null)
    {
        //reroute the user back to your card with an additional message to 
        //put response in the provided fields.
        return;
    }

    InputValues data;
    if (message.Value != null)
    {
        // Got an Action Submit
        dynamic value = message.Value;
        string submitType = value.Type.ToString();
        if (value != null)
        {
            data = Newtonsoft.Json.JsonConvert.DeserializeObject<InputValues>(submitType);
            _projectname = data.Name;
            _description = data.Description;

            IMessageActivity msg = Activity.CreateMessageActivity();
            msg.TextFormat = "text";
            msg.Text = _projectname;

            await context.Forward(new MyLuisDialog(), ResumeAfterLuisDialog, msg, CancellationToken.None);

        }

    }
}