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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否可以将AdaptiveTextBlock占位符值作为参数列表发送到web api调用相关的知识,希望对你有一定的参考价值。

在机器人中,我们有一个自适应卡,用户可以选择是或否。选择“是”时,系统会提示用户输入关键字。在用户在自适应卡中的文本块中给出输入之后,必须捕获输入并将其作为输入参数发送到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;
    }
答案

使用路由,您可以在路由本身上传递多个参数,也可以通过模型绑定或内容值绑定传递查询字符串上的参数。对于大多数常见情况,这实际上非常有效。只要您通过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"** });
另一答案

是的,您可以从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);
        }
    }
}

希望这可以帮助!

以上是关于是否可以将AdaptiveTextBlock占位符值作为参数列表发送到web api调用的主要内容,如果未能解决你的问题,请参考以下文章

是否可以制作没有模糊的 Next.js Image 占位符?

是否可以在 strings.xml 中为运行时值添加占位符?

是否可以在图像内制作占位符并稍后替换它?

将占位符文本添加到 Jquery Tokeninput

对应用于 Glide 的占位符做些啥?

如何将 R 会话中的值替换为 SQL 绑定变量占位符?