c# - 从 Form Flow 获取数据并将其输入到 api 调用中

标签 c# botframework azure-language-understanding formflow

我无法从使用表单流创建的表单中获取变量值。我需要用户提供这四个过滤器,以便我可以将它们插入我的 API 调用并返回一个 JSON 对象。

在此代码中,过滤器是在 URL 中手动指定的,仅用于说明目的。

当我运行这段代码时,API 调用首先执行,然后表单出现在 Bot 框架模拟器上。

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

这里是Enquiry.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();
        }
    }
}

最佳答案

一旦用户完成/填写了表单,就会触发 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
    }

关于c# - 从 Form Flow 获取数据并将其输入到 api 调用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50113025/

相关文章:

c# - 在 dll 中导出并从 C# 加载的 C++ 函数

c# - 当您没有类的源代码时,是否可以对对象进行 .NET 二进制序列化?

Botframework 网络聊天的 token 生成

botframework - 如何使用 microsoft bot-builder 向 Skype for Business 发送消息给特定用户?

c# - 在极少数情况下,重定向到 ACS 并返回后 Cookie 为空

c# - 什么 C# 模板引擎在 HTML 和控制代码之间有清晰的分离?

azure - Bot 框架 Composer 错误 : Unable to get a value for KnowledgeBaseId from state

azure-language-understanding - LUIS 资源键在语言理解门户中被禁用

c# - Luis 意图处理程序向机器人框架抛出异常

node.js - 无法使用 TriggerAction 与 Microsoft Bot 框架 (node.js) 中的 LUIS 意图匹配