Search code examples
c#botframeworkwebapi

Is it possible to send AdaptiveTextBlock placeholder value as parameter list to web API call


Within a bot, we have an adaptive card where the user has a choice to select yes or no. On selecting YES, user is prompted to enter the keywords. After the user gives input in the textblock in adaptive card, the input has to be captured and sent as input parameter to web API.

The user input will be given in Placeholder of the AdaptiveTextInput block.

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;
}

Solution

  • Yes, you can retrieve the input values from an AdaptiveCard and use them as parameters in an HTTP request to an API. When the user submits an AdaptiveCard, the input fields are sent to the bot through the activity in the Value attribute. You can parse the resulting JSON string with JObject and retrieve the values for your API call. See the example below.

    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);
            }
        }
    }
    

    Hope this helps!