通过使用Dispatch cli,我可以同时运行Luis和QnA。 我正在尝试实施自适应卡,它也可以正常工作,我 可以轻松添加/查看任何类型。当我尝试获得问题时 自适应卡上的响应提交按钮单击。我的机器人回溯到 switch语句,其中未跟踪意图时 默认情况被触发。在评论默认我 收到此错误
System.Collections.Generic.KeyNotFoundException:给定的密钥 字典中没有“ luisResult”。
at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at Microsoft.BotBuilderSamples.DispatchBot.DispatchToTopIntentAsync(ITurnContext`1
turnContext,字符串意图,RecognizerResult识别器结果, CancellationToken CancellationToken)中 F:\ nikhil \ Alfi \ mts-qna + luis \ Bots \ DispatchBot.cs:第76行
at Microsoft.BotBuilderSamples.DispatchBot.OnMessageActivityAsync(ITurnContext`1
turnContext,CancellationToken cancelToken) F:\ nikhil \ Alfi \ mts-qna + luis \ Bots \ DispatchBot.cs:第45行
at Microsoft.Bot.Builder.BotFrameworkAdapter.TenantIdWorkaroundForTeamsMiddleware.OnTurnAsync(ITurnContext
turnContext,NextDelegate接下来,CancellationToken取消(Token) 在 d:\ a \ 1 \ s \ libraries \ Microsoft.Bot.Builder \ BotFrameworkAdapter.cs:line 964
at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext
turnContext,BotCallbackHandler回调,CancellationToken cancelToken)在 d:\ a \ 1 \ s \ libraries \ Microsoft.Bot.Builder \ MiddlewareSet.cs:第55行
at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken
cancellationToken) d:\ a \ 1 \ s \ libraries \ Microsoft.Bot.Builder \ BotAdapter.cs:第167行
我已经尝试过-
if (turnContext.Activity.Value != null)
{
var activity = turnContext.Activity;
activity.Text = JsonConvert.SerializeObject(activity.Value);
await turnContext.SendActivityAsync(MessageFactory.Text("activity.Text"), cancellationToken);
}
在调试时,我可以看到像这样的值- activity.Value = {{“ startdate”:“ 2017-10-12”,“ enddate”:“ 2017-10-12”}}
namespace Microsoft.BotBuilderSamples
{
public class DispatchBot : ActivityHandler
{
private ILogger<DispatchBot> _logger;
private IBotServices _botServices;
public DispatchBot(IBotServices botServices, ILogger<DispatchBot> logger)
{
_logger = logger;
_botServices = botServices;
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
// First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use.
var recognizerResult = await _botServices.Dispatch.RecognizeAsync(turnContext, cancellationToken);
// Top intent tell us which cognitive service to use.
var topIntent = recognizerResult.GetTopScoringIntent();
// Next, we call the dispatcher with the top intent.
await DispatchToTopIntentAsync(turnContext, topIntent.intent, recognizerResult, cancellationToken);
}
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
const string WelcomeText = "I am here to make your experience much more easier";
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Hi {member.Name}, I am at your service . {WelcomeText}"), cancellationToken);
}
}
}
private async Task DispatchToTopIntentAsync(ITurnContext<IMessageActivity> turnContext, string intent, RecognizerResult recognizerResult, CancellationToken cancellationToken)
{
var activity = turnContext.Activity;
activity.Text = JsonConvert.SerializeObject(activity.Value);
switch (intent)
{
case "l_mts-bot-809f":
activity.Text = JsonConvert.SerializeObject(activity.Value);
await ProcessHomeAutomationAsync(turnContext, recognizerResult.Properties["luisResult"] as LuisResult, cancellationToken);
break;
case "q_mts-bot":
await ProcessSampleQnAAsync(turnContext, cancellationToken);
break;
default:
await ProcessHomeAutomationAsync(turnContext, recognizerResult.Properties["luisResult"] as LuisResult, cancellationToken);
Console.WriteLine(">>>>>>>>>>>"+recognizerResult.Properties["luisResult"]);
break;
// _logger.LogInformation($"Dispatch unrecognized intent: {intent}.");
// activity.Text = JsonConvert.SerializeObject(activity.Value);
// await turnContext.SendActivityAsync(MessageFactory.Text($"Dispatch unrecognized intent: {intent}."), cancellationToken);
// break;
}
}
private Activity CreateResponse(IActivity activity, Attachment attachment)
{
var response = ((Activity)activity).CreateReply();
response.Attachments = new List<Attachment>() { attachment };
return response;
}
private async Task ProcessHomeAutomationAsync(ITurnContext<IMessageActivity> turnContext, LuisResult luisResult, CancellationToken cancellationToken)
{
_logger.LogInformation("ProcessHomeAutomationAsync");
// Retrieve LUIS result for Process Automation.
var result = luisResult.ConnectedServiceResult;
var topIntent = result.TopScoringIntent.Intent;
var entity = result.Entities;
if (topIntent == "welcome")
{
await turnContext.SendActivityAsync(MessageFactory.Text("Hi,This is Alfie"), cancellationToken);
}
if (topIntent == "None")
{
await turnContext.SendActivityAsync(MessageFactory.Text("Sorry I didnt get you!"), cancellationToken);
}
if (topIntent == "LeaveApplication")
{
await turnContext.SendActivityAsync(MessageFactory.Text("Do you want to apply leaves for yourself or for someone else?"), cancellationToken);
}
if (topIntent == "LeaveSelfApplication")
{
var DatesRange = LeavesDatesAdaptiveCardAttachment();
var response = CreateResponse(turnContext.Activity, DatesRange);
await turnContext.SendActivityAsync(response, cancellationToken);
if (turnContext.Activity.Value != null)
{
var activity = turnContext.Activity;
activity.Text = JsonConvert.SerializeObject(activity.Value);
await turnContext.SendActivityAsync(MessageFactory.Text("2019-07-30"), cancellationToken);
}
//await turnContext.SendActivityAsync(MessageFactory.Text("2019-07-30"), cancellationToken);
}
if (topIntent == "LeavesDateTenure")
{
string jsonData = JsonConvert.SerializeObject(result);
dynamic json = JsonConvert.DeserializeObject(jsonData);
await turnContext.SendActivityAsync(MessageFactory.Text("Please Provide me with your Leaves Tenure"), cancellationToken);
}
}
//string jsonData = JsonConvert.SerializeObject(result);
private async Task ProcessSampleQnAAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
_logger.LogInformation("ProcessSampleQnAAsync");
var results = await _botServices.SampleQnA.GetAnswersAsync(turnContext);
if (results.Any())
{
await turnContext.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text("Sorry, could not find an answer in the Q and A system."), cancellationToken);
}
}
// Load attachment from file.
private Attachment CreateAdaptiveCardAttachment()
{
// combine path for cross platform support
string[] paths = { ".", "Cards", "AddingLeaveDetails.json" };
string fullPath = Path.Combine(paths);
var adaptiveCard = File.ReadAllText(fullPath);
return new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = JsonConvert.DeserializeObject(adaptiveCard),
};
}
private Attachment LeavesDatesAdaptiveCardAttachment()
{
// combine path for cross platform support
string[] paths = { ".", "Cards", "LeavesDates.json" };
string fullPath = Path.Combine(paths);
var adaptiveCard = File.ReadAllText(fullPath);
return new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = JsonConvert.DeserializeObject(adaptiveCard),
};
}
}
}