在机器人中,我们有一个自适应卡,用户可以选择是或否。选择YES后,系统将提示用户输入关键字。用户在适配卡的文本块中输入后,必须捕获该输入并将其作为输入参数发送到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;
}
发布于 2019-02-18 23:39:13
它实际上只是一个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"
}
接下来,我们在BotServices.cs
中初始化BotService
类的一个新实例,该实例从.bot文件中获取上述信息。外部服务是使用BotConfiguration
类配置的。
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
文件中的单例。
// Initialize Bot Connected Services clients.
var connectedServices = new BotServices(botConfig);
services.AddSingleton(sp => connectedServices);
services.AddSingleton(sp => botConfig);
在您的Bot.cs
类中注入在.bot
文件中配置的服务:
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
,如下所示:
"type": "Action.Submit"
现在您可以进行Luis api调用,这才是您真正需要的。这个调用可以在您的类中的任何地方完成,它注入Luis服务:
var recognizerResult = await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);
此代码片段来自MS Docs here
https://stackoverflow.com/questions/54748358
复制相似问题