Asp.net web api 从自适应文本块捕获用户输入后,是否有任何可能的方法应用luis?

Asp.net web api 从自适应文本块捕获用户输入后,是否有任何可能的方法应用luis?,asp.net-web-api,botframework,azure-language-understanding,Asp.net Web Api,Botframework,Azure Language Understanding,在机器人中,我们有一个自适应卡,用户可以选择是或否。选择是时,会提示用户输入关键字。用户在自适应卡的文本块中输入后,必须捕获输入并将其作为输入参数发送到web api。但是,在给出输入后,我们必须应用luis,因为输入文本可能有同义词。在下面的代码中,关键字变量指的是用户提供的输入文本,必须应用LUIS private async Task CustomisePPT(IDialogContext context, IAwaitable<object> result)

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

    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;
    }
private async Task CustomisePPT(IDialogContext上下文,IAwaitable结果)
{          
尝试
{                
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();
字符串关键字=split.Trim();
关键词=关键词.子字符串(1,关键词.长度-5);
使用(HttpClient=new HttpClient())
{
//api将用户消息作为查询参数
字符串RequestURI=”https://xyz“+***关键字***”;
HttpResponseMessageResponseMsg=await client.GetAsync(请求URI);
//TODO:处理失败案例
if(ResponseMSG.IsSuccessStatusCode)
{
var apisresponse=await responsemsg.Content.ReadAsStringAsync();
}
}
}
捕获(例外情况除外)
{
}
//抛出新的NotImplementedException();
等待任务。完成任务;
}

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

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

{
"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"
接下来,在
ConfigureServices
方法中使用以下代码在
Startup.cs
文件中将LUIS应用程序注册为单例

// Initialize Bot Connected Services clients.
var connectedServices = new BotServices(botConfig);
services.AddSingleton(sp => connectedServices);
services.AddSingleton(sp => botConfig);
.bot
文件中配置的服务注入到
bot.cs
类中:

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
的操作数组中。按如下方式提交:

"type": "Action.Submit"
现在您可以进行luisapi调用,这正是您真正需要的。此调用可以在类中的任何位置执行,这将注入Luis服务:

 var recognizerResult = await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);

此代码段来自MS文档,它实际上只是一个api调用,应该进行配置

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

{
"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"
接下来,在
ConfigureServices
方法中使用以下代码在
Startup.cs
文件中将LUIS应用程序注册为单例

// Initialize Bot Connected Services clients.
var connectedServices = new BotServices(botConfig);
services.AddSingleton(sp => connectedServices);
services.AddSingleton(sp => botConfig);
.bot
文件中配置的服务注入到
bot.cs
类中:

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
的操作数组中。按如下方式提交:

"type": "Action.Submit"
现在您可以进行luisapi调用,这正是您真正需要的。此调用可以在类中的任何位置执行,这将注入Luis服务:

 var recognizerResult = await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);

此代码段来自MS文档

@Taher的答案将帮助您集成LUIS。这一个将帮助您使用自适应卡

自适应卡发送的提交结果与常规用户文本略有不同。当用户在聊天中键入内容并发送正常消息时,它会在
Context.Activity.Text
中结束。当用户在自适应卡上填写输入时,它将进入
Context.Activity.Value
,这是一个对象,其中键名是卡中的
id
,值是自适应卡中的字段值

例如,json:

{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "text": "Test Adaptive Card"
        },
        {
            "type": "ColumnSet",
            "columns": [
                {
                    "type": "Column",
                    "items": [
                        {
                            "type": "TextBlock",
                            "text": "Text:"
                        }
                    ],
                    "width": 20
                },
                {
                    "type": "Column",
                    "items": [
                        {
                            "type": "Input.Text",
                            "id": "userText",
                            "placeholder": "Enter Some Text"
                        }
                    ],
                    "width": 80
                }
            ]
        }
    ],
    "actions": [
        {
            "type": "Action.Submit",
            "title": "Submit"
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0"
}
。。创建一张如下所示的卡:

如果用户在文本框中输入“Testing 123”并点击Submit,
Context.Activity
将显示如下内容:

{ type: 'message',
  value: { userText: 'Testing Testing 123' },
  from: { id: 'xxxxxxxx-05d4-478a-9daa-9b18c79bb66b', name: 'User' },
  locale: '',
  channelData: { postback: true },
  channelId: 'emulator',
  conversation: { id: 'xxxxxxxx-182b-11e9-be61-091ac0e3a4ac|livechat' },
  id: 'xxxxxxxx-182b-11e9-ad8e-63b45e3ebfa7',
  localTimestamp: 2019-01-14T18:39:21.000Z,
  recipient: { id: '1', name: 'Bot', role: 'bot' },
  timestamp: 2019-01-14T18:39:21.773Z,
  serviceUrl: 'http://localhost:58453' }
用户提交可以在
Context.Activity.Value.userText
中看到

请注意,自适应卡提交是作为回发发送的,这意味着提交数据不会作为对话的一部分出现在聊天窗口中,而是保留在自适应卡上

将自适应卡与

你的问题与此不太相关,但既然你最终可能会尝试这样做,我认为在我的回答中包含这一点可能很重要

从本质上讲,自适应卡的工作方式与提示不同。有提示时,将显示提示并等待用户输入,然后继续。但是对于自适应卡(即使它包含一个输入框和一个提交按钮),自适应卡中没有代码会导致瀑布式对话框在继续对话框之前等待用户输入

因此,如果您使用的是接受用户输入的自适应卡,您通常希望在瀑布式对话框的上下文之外处理用户提交的任何内容

也就是说,如果您想将自适应卡用作瀑布式对话框的一部分,那么有一种解决方法。基本上,你:

  • 显示自适应卡
  • 显示文本提示
  • 将用户的自适应卡输入转换为文本提示输入
  • 在瀑布式对话框类中(步骤1和2):

    private async Task DisplayCardAsync(waterwallstepcontext-stepContext,CancellationToken-CancellationToken)
    {
    //创建自适应卡
    var cardPath=Path.Combine(“.”,“AdaptiveCard.json”);
    var cardJson=File.ReadAllText(cardPath);
    var cardAttachment=新附件()
    {
    ContentType=“application/vnd.microsoft.card.adaptive”,
    Content=JsonConvert.DeserializeObject(cardJson),