首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在从自适应文本块捕获用户输入后,有没有可能应用luis?

在从自适应文本块捕获用户输入后,有没有可能应用luis?
EN

Stack Overflow用户
提问于 2019-02-18 21:24:59
回答 1查看 194关注 0票数 0

在机器人中,我们有一个自适应卡,用户可以选择是或否。选择YES后,系统将提示用户输入关键字。用户在适配卡的文本块中输入后,必须捕获该输入并将其作为输入参数发送到web api。然而,在给定输入之后,我们将不得不应用luis,因为输入文本可能具有同义词。在下面的代码中,关键字变量指的是用户给出的输入文本,必须对其应用LUIS。

代码语言:javascript
复制
    private async Task CustomisePPT(IDialogContext context, IAwaitable<object> result)
    {          
        try
        {                
            var replyMessage = context.MakeMessage();
            var newMessage = context.Activity.AsMessageActivity();
            var userMessage = newMessage.Value;
            var take=userMessage.ToString().Substring(userMessage.ToString().IndexOf("GetUserInputKeywords"));
            var split = take.ToString().Substring("GetUserInputKeywords".Length+2);
            string keywords = split.Trim();
            keywords = keywords.Substring(1, keywords.Length - 5);

            using (HttpClient client = new HttpClient())
            {
                // api takes the user message as a query paramater
                string RequestURI = "https://xyz" + ***keywords***;
                HttpResponseMessage responsemMsg = await client.GetAsync(RequestURI);
                // TODO: handle fail case

                if (responsemMsg.IsSuccessStatusCode)
                {
                    var apiResponse = await responsemMsg.Content.ReadAsStringAsync();
                }
            }
        }
        catch (Exception ex)
        {

        }
        //throw new NotImplementedException();
        await Task.CompletedTask;
    }
EN

回答 1

Stack Overflow用户

发布于 2019-02-18 23:39:13

它实际上只是一个api调用,应该进行配置。

首先,您应该将Luis配置添加到.bot文件中。

代码语言:javascript
复制
{
"name": "LuisBot",
"description": "",
"services": [
    {
        "type": "endpoint",
        "name": "development",
        "endpoint": "http://localhost:3978/api/messages",
        "appId": "",
        "appPassword": "",
        "id": "166"
    },
    {
        "type": "luis",
        "name": "LuisBot",
        "appId": "<luis appid>",
        "version": "0.1",
        "authoringKey": "<luis authoring key>",
        "subscriptionKey": "<luis subscription key>",
        "region": "<luis region>",
        "id": "158"
    }
],
"padlock": "",
"version": "2.0"

}

接下来,我们在BotServices.cs中初始化BotService类的一个新实例,该实例从.bot文件中获取上述信息。外部服务是使用BotConfiguration类配置的。

代码语言:javascript
复制
public class BotServices
{
    // Initializes a new instance of the BotServices class
    public BotServices(BotConfiguration botConfiguration)
    {
        foreach (var service in botConfiguration.Services)
        {
            switch (service.Type)
            {
                case ServiceTypes.Luis:
                {
                    var luis = (LuisService)service;
                    if (luis == null)
                    {
                        throw new InvalidOperationException("The LUIS service is not configured correctly in your '.bot' file.");
                    }

                    var app = new LuisApplication(luis.AppId, luis.AuthoringKey, luis.GetEndpoint());
                    var recognizer = new LuisRecognizer(app);
                    this.LuisServices.Add(luis.Name, recognizer);
                    break;
                    }
                }
            }
        }

    // Gets the set of LUIS Services used. LuisServices is represented as a dictionary.  
    public Dictionary<string, LuisRecognizer> LuisServices { get; } = new Dictionary<string, LuisRecognizer>();
}

接下来,在ConfigureServices方法中使用以下代码将LUIS应用程序注册为Startup.cs文件中的单例。

代码语言:javascript
复制
// Initialize Bot Connected Services clients.
var connectedServices = new BotServices(botConfig);
services.AddSingleton(sp => connectedServices);
services.AddSingleton(sp => botConfig);

在您的Bot.cs类中注入在.bot文件中配置的服务:

代码语言:javascript
复制
public class LuisBot : IBot
{
    // Services configured from the ".bot" file.
    private readonly BotServices _services;

    // Initializes a new instance of the LuisBot class.
    public LuisBot(BotServices services)
    {
        _services = services ?? throw new System.ArgumentNullException(nameof(services));
        if (!_services.LuisServices.ContainsKey(LuisKey))
        {
            throw new System.ArgumentException($"Invalid configuration....");
        }
    }
}

可以在adaptivecard的操作数组中将adaptivecard的输入捕获为Action.Submit,如下所示:

代码语言:javascript
复制
"type": "Action.Submit"

现在您可以进行Luis api调用,这才是您真正需要的。这个调用可以在您的类中的任何地方完成,它注入Luis服务:

代码语言:javascript
复制
 var recognizerResult = await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);

此代码片段来自MS Docs here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54748358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档