是否可以将AdaptiveTextBlock占位符值作为参数列表发送到Web api调用

时间:2019-01-29 09:34:36

标签: c# api web botframework

在机器人内部,我们有一个自适应卡,用户可以选择是或否。 在选择是时,提示用户输入关键字。 用户在自适应卡的文本块中输入内容后,必须捕获输入并将其作为输入参数发送到Web api。

用户输入将在AdaptiveTextInput块的占位符中给出。

    public static Attachment GetUserInputForCustomPPT()
    {
        AdaptiveCard card = new AdaptiveCard()
        {
            Id = "GetCustomPPT",
            Body = new List<AdaptiveElement>()
            {
                new AdaptiveTextBlock()
                {
                    Text = "Do you want to apply filter and customise the PPT?",
                    Wrap=true,
                   Size = AdaptiveTextSize.Small
                },
                new AdaptiveContainer()
                {
                    Id = "getCustomPPTNo",
                    SelectAction = new AdaptiveSubmitAction()
                   {
                        Id = "getCustomPPTNo",
                        Title = "No",
                        DataJson = "{ \"Type\": \"GetCustomPPT\" }",
                    }
                },
                new AdaptiveContainer()
                {
                    Id = "getCustomPPTYes",
                    Items = new List<AdaptiveElement>()
                    {
                        new AdaptiveTextBlock()
                        {
                            Text = "Please select an option",
                            Wrap=true,
                            Size = AdaptiveTextSize.Small
                        }
                    }
                },
            },
            Actions = new List<AdaptiveAction>()
            {
                new AdaptiveShowCardAction()
                {
                    Id = "GetPPTYes",
                    Title = "Yes",
                    Card = new AdaptiveCard()
                    {
                        Body = new List<AdaptiveElement>()
                        {
                            new AdaptiveTextBlock()
                            {
                                Text = "Please enter your input",
                                Wrap = true
                            },
                            new AdaptiveTextInput()
                            {
                                Id="GetUserInputKeywords",
                                Placeholder="Please enter the keyword list separated by ',' Ex:RPA,FS ",
                                MaxLength=490,
                                IsMultiline=true
                            }
                        },
                       Actions = new List<AdaptiveAction>()
                        {
                            new AdaptiveSubmitAction()
                            {
                                Id = "contactSubmit",
                                Title = "Submit",
                                DataJson = "{ \"Type\": \"GetPPT\" }"
                            },
                            new AdaptiveOpenUrlAction()
                            {
                                Id="CallApi",
                                Url=new Uri("https://xyz"+"RPA")
                                //card.Actions.Card.AdaptiveTextInput.Placeholder
                            }
                        }
                    }
                },
                 new AdaptiveShowCardAction()
                {
                    Id = "GetPPTNo",
                    Title = "No",
                    Card = new AdaptiveCard()
                    {
                        Body = new List<AdaptiveElement>()
                        {
                        },
                        Actions = new List<AdaptiveAction>()
                        {
                            new AdaptiveSubmitAction()
                            {
                                Id = "contactSubmit",
                                Title = "Submit",
                                DataJson = "{ \"Type\": \"GetPPTNo\" }"
                            }
                        }
                    }
                }
            }
        };
        // Create the attachment with adapative card. 
        Attachment attachment = new Attachment()
        {
            ContentType = AdaptiveCard.ContentType,
           Content = card
        };
        return attachment;
    }

2 个答案:

答案 0 :(得分:0)

使用路由,您可以通过模型绑定或内容值绑定在路由本身上传递多个参数,也可以在查询字符串上传递参数。在大多数情况下,这实际上效果很好。只要您通过POST操作传递单个复杂类型,或者通过查询字符串或POST缓冲区传递多个简单类型,就没有问题。但是,如果您需要像使用WCF REST或ASP.NET AJAX那样轻松地传递多个参数,那么事情就不会那么明显了。

RouteTable.Routes.MapHttpRoute(
name: "ApiName",
routeTemplate: "photos/**{action}**/{title}",
defaults: new {
    title = RouteParameter.Optional,
    controller = "PhotoApi",
    **action =** **"GetPhotos"** });

答案 1 :(得分:0)

是的,您可以从AdaptiveCard检索输入值,并将其用作对API的HTTP请求中的参数。当用户提交AdaptiveCard时,输入值将通过Value属性中的活动发送到bot。您可以使用JObject解析结果JSON字符串,并检索API调用的值。请参见下面的示例。

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{

    if (turnContext.Activity.Type == ActivityTypes.Message)
    {   
        // Check if user submitted AdaptiveCard input
        if (turnContext.Activity.Value != null) {

            // Convert String to JObject
            String value = turnContext.Activity.Value.ToString();
            JObject results = JObject.Parse(value);

            // Get type from input field
            String name = results.GetValue("Type").ToString();

            // Get Keywords from input field
            String userInputKeywords = "";
            if (name == "GetPPT") {
                    userInputKeywords = results.GetValue("GetUserInputKeywords").ToString();
            }

            // Make Http request to api with paramaters
            String myUrl = $"http://myurl.com/api/{userInputKeywords}";

            ...

            // Respond to user
            await turnContext.SendActivityAsync("Respond to user", cancellationToken: cancellationToken);
        } else {
            // Send user AdaptiveCard
            var cardAttachment = GetUserInputForCustomPPT();
            var reply = turnContext.Activity.CreateReply();
            reply.Attachments = new List<Attachment>() { cardAttachment };
            await turnContext.SendActivityAsync(reply, cancellationToken);
        }
    }
}

希望这会有所帮助!