Получение данных из Form Flow и ввод их в вызов API - PullRequest
0 голосов
/ 01 мая 2018

Я застрял при получении значений переменных из формы, которую я создал, используя Form Flow. Мне нужны эти четыре фильтра от пользователя, чтобы я мог вставить их в мой вызов API и вернуть объект JSON.

В этом коде фильтры указываются вручную в URL только для пояснения.

Когда я запускаю этот код, сначала выполняется API-вызов, затем форма поступает в эмулятор Bot-framework.

[LuisIntent("ProductSearch")]
public async Task ProductSearch(IDialogContext context, LuisResult result)
{
    if (result.TryFindEntity("SearchKeyword", out EntityRecommendation SearchKeywordEntity))
    {
        await context.PostAsync($"Searching for '{SearchKeywordEntity.Entity}'...");

        Enquiry enqform = new Enquiry();
        FormDialog<Enquiry> Enquiryform = new FormDialog<Enquiry>(enqform, Enquiry.BuildEnquiryForm, FormOptions.PromptInStart);
        context.Call(Enquiryform, EnquiryFormSubmitted);

        ApiCall api = new ApiCall();
        string json = ApiCall.GET($"http://127.0.0.1:5000/search?search_keyword=water&av_filter=Asia&in_filter=Chemicals&aa_filter=Home Care&pg_filter=Polymers");
        await context.PostAsync(json);
    }
}

Вот запрос.cs

using Microsoft.Bot.Builder.FormFlow;
using System;

namespace BASF_Bot_Application2.Dialogs
{
    [Serializable]
    public class Enquiry
    {
        [Prompt("Would you like to apply some filters to get more specific results? {||}")]
        public bool ApplyFilters { get; set; }

        [Prompt("Where do yo want the product? {||}")]
        public Availability AvailabilityRequired { get; set; }
        public enum Availability
        {
            Global, Africa, Asia, Australia, Europe, North_America, South_America
        }

        [Prompt("What industry would you prefer? {||}")]
        public Industries IndustriesRequired { get; set; }
        public enum Industries
        {
            Agriculture, Automotive & Transportation, Chemicals, Construction, Electronics & Electrics, Energy & Resources, Furniture & Wood, Home Care and I&I Cleaning, Nutrition, Packaging & Print, Paint & Coatings Industry, Personal Care & Hygiene, Plastics & Rubber, Pulp & Paper, Textile, Leather & Footwear
        }

        [Prompt("Where is the Area of Application? {||}")]
        public Areas_of_Application Areas_of_ApplicationRequired { get; set; }
        public enum Areas_of_Application
        {
            Ag Chem Additives, Cleaning and caring, Construction, Electronics, Food and Beverage, Formulation Technologies, Health, Home and Garden, Home Care, Industrial and Institutional Cleaning, Information technology, Manufacturing, Measuring and control technol., Packaging, Paper and Printing
        }

        public static IForm<Enquiry> BuildEnquiryForm()
        {
            return new FormBuilder<Enquiry>()
                .Field("ApplyFilters")
                .Field("AvailabilityRequired")
                .Field("Industries")
                .Field("Areas_of_Application")
                .Build();
        }
    }
}

1 Ответ

0 голосов
/ 01 мая 2018

Как только форма заполнена / заполнена пользователем, запускается EnquiryFormSubmitted, так что именно здесь вы получите значения, введенные пользователем, и именно здесь вы должны сделать свой вызов API.

private async Task EnquiryFormSubmitted(IDialogContext context, IAwaitable<Enquiry> result)
    {            
        var enquiry = await result;
        //All the user entered details are available in the enquiry object.
        ApiCall api = new ApiCall();
        string parameters = $"search_keyword=water&av_filter={enquiry.Availability}&in_filter={enquiry.Industries}&aa_filter={enquiry.Areas_of_Application}&pg_filter=Polymers";
        string json = ApiCall.GET($"http://127.0.0.1:5000/search?" + parameters);
        await context.PostAsync(json);
        //Whatever more logic is required
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...