C# 在bot框架中将QnAMaker路由到英雄卡
我正在使用Microsoft Azure Web App服务开发聊天机器人。其中,我想在问候语中插入一张英雄卡,然后在相应的英雄按钮中单击Info Dynamics365,开始调用我创建的卡片列表源,如果我按FAQ,我想与QnAMaker连接进行提问。如果您现在使用ActionType,两者都将调用一个卡片列表,其中一种类型应用于连接QnAMaker,或者如何指定路径 Bpts/QnABot.csC# 在bot框架中将QnAMaker路由到英雄卡,c#,azure,botframework,qnamaker,C#,Azure,Botframework,Qnamaker,我正在使用Microsoft Azure Web App服务开发聊天机器人。其中,我想在问候语中插入一张英雄卡,然后在相应的英雄按钮中单击Info Dynamics365,开始调用我创建的卡片列表源,如果我按FAQ,我想与QnAMaker连接进行提问。如果您现在使用ActionType,两者都将调用一个卡片列表,其中一种类型应用于连接QnAMaker,或者如何指定路径 Bpts/QnABot.cs namespace Microsoft.BotBuilderSamples { public c
namespace Microsoft.BotBuilderSamples
{
public class QnABot<T> : ActivityHandler
{
private BotState _conversationState;
private BotState _userState;
//QnAMaker
//protected readonly BotState ConversationState;
//protected readonly Microsoft.Bot.Builder.Dialogs.Dialog Dialog;
//protected readonly BotState UserState;
private string KBID = "fcd905a3-7269-4ea5-9a58-7b02c888ddb6";
private string ENDPOINT_KEY = "7b9a938a-e7ac-46f6-ab69-97d4e2e04f66";
private string HOST = "myfirstqa.azurewebsites.net";
//Azure 첫 세팅 소스
public QnABot(ConversationState conversationState, UserState userState)
{
_conversationState = conversationState;
_userState = userState;
}
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
await base.OnTurnAsync(turnContext, cancellationToken);
// Save any state changes that might have occured during the turn.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
var card = new HeroCard();
card.Title = "";
card.Text = @"Welcome to Welcome Users bot sample! This Introduction card";
card.Images = new List<CardImage>() { new CardImage("https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwjQjeeS4obmAhUIfnAKHQGgCB0QjRx6BAgBEAQ&url=https%3A%2F%2Fdougame.tistory.com%2F98&psig=AOvVaw11Y-BZJtsxh1pTp0Qxzedb&ust=1574819546545481") };
card.Buttons = new List<CardAction>()
{
new CardAction(ActionTypes.PostBack, "테스트 FAQ연결", null,"Connect QnA-Makeshfrjflrk todrur","Connect QnA-Maker", "Connection QnAMaker"),
new CardAction(ActionTypes.PostBack, "YOUTUBE LINK", null,"Connect YouTube","Connect YouTube", "Connection YouTube"),
new CardAction(ActionTypes.PostBack, "테스트 F연결", null,"Connect QnA-Maker","Connect QnA-Maker", "Connection A")
};
var response = MessageFactory.Attachment(card.ToAttachment());
await turnContext.SendActivityAsync(response, cancellationToken);
}
}
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
//QnA Maker연결
var qnaMaker = new QnAMaker(new QnAMakerEndpoint
{
KnowledgeBaseId = "fcd905a3-7269-4ea5-9a58-7b02c888ddb6",
EndpointKey = "7b9a938a-e7ac-46f6-ab69-97d4e2e04f66",
Host = "myfirstqa.azurewebsites.net"
},
null,
new System.Net.Http.HttpClient());
var conversationStateAccessors = _conversationState.CreateProperty<ConversationService>(nameof(ConversationService));
var conversationService = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationService());
var input = turnContext.Activity.Text;
if (String.IsNullOrEmpty(conversationService.currentService) && (input.Equals("Connection QnAMaker")))
{
conversationService.currentService = input;
await turnContext.SendActivityAsync(MessageFactory.Text("선택 : " + input + " service ,\n 입력할 내용 " + input + " question"), cancellationToken);
}
else if (String.IsNullOrEmpty(conversationService.currentService))
{
await turnContext.SendActivityAsync(MessageFactory.Text("select a service from hero card first"), cancellationToken);
}
else if (conversationService.currentService.Equals("Connection QnAMaker"))
{
//call your dy QNA service here
var result = qnaMaker.GetAnswersAsync(turnContext).GetAwaiter().GetResult();
if (result.Length == 0)
{
await turnContext.SendActivityAsync(MessageFactory.Text("Sorry , I can't find any answer for it"), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text(result[0].Answer), cancellationToken);
}
////await turnContext.SendActivityAsync(MessageFactory.Text(result[0].Answer), cancellationToken);
}
else if (conversationService.currentService.Equals("dy365"))
{
//call your dy 365 service here
await turnContext.SendActivityAsync(MessageFactory.Text("dy365 response"), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text("error"), cancellationToken);
};
}
}
public class ConversationService
{
public string currentService { get; set; }
}
名称空间Microsoft.BotBuilderSamples
{
公共类QnABot:ActivityHandler
{
私有BotState_会话状态;
私有BotState_userState;
//QnAMaker
//受保护的只读BotState会话状态;
//受保护的只读Microsoft.Bot.Builder.Dialogs.Dialog;
//受保护的只读BotState UserState;
专用字符串KBID=“fcd905a3-7269-4ea5-9a58-7b02c888ddb6”;
专用字符串端点_KEY=“7b9a938a-e7ac-46f6-ab69-97d4e2e04f66”;
私有字符串HOST=“myfirstqa.azurewebsites.net”;
//蔚蓝첫 세팅 소스
公共QnABot(会话状态会话状态,用户状态用户状态)
{
_会话状态=会话状态;
_userState=userState;
}
公共覆盖异步任务OnTurnAsync(ITurnContext turnContext,CancellationToken CancellationToken=default(CancellationToken))
{
wait base.OnTurnAsync(turnContext,cancellationToken);
//保存转弯期间可能发生的任何状态更改。
wait_conversationState.saveChangesSync(turnContext,false,cancellationToken);
wait_userState.saveChangesSync(turnContext,false,cancellationToken);
}
MemberSaddedAsync上的受保护重写异步任务(IList membersAdded、iTurContext turnContext、CancellationToken CancellationToken)
{
foreach(membersAdded中的var成员)
{
if(member.Id!=turnContext.Activity.Recipient.Id)
{
var卡=新卡();
卡片名称=”;
Text=@“欢迎欢迎用户bot示例!此介绍卡”;
card.Images=new List(){new CardImage('https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwjQjeeS4obmAhUIfnAKHQGgCB0QjRx6BAgBEAQ&url=https%3A%2F%2Fdougame.tistory.com%2F98&psig=AOvVaw11Y-BZJtsxh1pTp0Qxzedb&ust=15748195465481”);
card.Buttons=新列表()
{
新的CardAction(ActionTypes.PostBack,“테스트 常见问题연결", 空,“连接QnA Makeshfrjflrk todrur”、“连接QnA Maker”、“连接QnAMaker”),
新的CardAction(ActionTypes.PostBack,“YOUTUBE链接”,null,“连接YOUTUBE”,“连接YOUTUBE”,“连接YOUTUBE”),
新的CardAction(ActionTypes.PostBack,“테스트 F연결,null,“连接QnA生成器”、“连接QnA生成器”、“连接A”)
};
var response=MessageFactory.Attachment(card.ToAttachment());
等待turnContext.SendActivityAsync(响应,取消令牌);
}
}
}
受保护的重写异步任务OnMessageActivityAsync(ITurnContext turnContext,CancellationToken CancellationToken)
{
//QnA生成器연결
var qnaMaker=新qnaMaker(新qnaMaker点
{
KnowledgeBaseId=“fcd905a3-7269-4ea5-9a58-7b02c888ddb6”,
EndpointKey=“7b9a938a-e7ac-46f6-ab69-97D4D4E2E04F66”,
Host=“myfirstqa.azurewebsites.net”
},
无效的
新系统.Net.Http.HttpClient());
var conversationStateAccessors=_conversationState.CreateProperty(nameof(ConversationService));
var conversationService=等待conversationStateAccessors.GetAsync(turnContext,()=>new conversationService());
var输入=turnContext.Activity.Text;
if(String.IsNullOrEmpty(conversationService.currentService)和&(input.Equals(“连接QnAMaker”))
{
conversationService.currentService=输入;
等待turnContext.SendActivityAsync(MessageFactory.Text(“선택 : “+输入+”服务,\n입력할 내용 “+输入+”问题“),取消令牌);
}
else if(String.IsNullOrEmpty(conversationService.currentService))
{
Wait turnContext.SendActivityAsync(MessageFactory.Text(“首先从英雄卡中选择服务”),cancellationToken);
}
else if(conversationService.currentService.Equals(“连接QnAMaker”))
{
//在这里呼叫您的dy QNA服务
var result=qnaMaker.GetAnswersAsync(turnContext.GetAwaiter().GetResult();
如果(result.Length==0)
{
Wait turnContext.SendActivityAsync(MessageFactory.Text(“对不起,我找不到任何答案”)、cancellationToken);
}
其他的
{
等待turnContext.SendActivityAsync(MessageFactory.Text(结果[0]。答案),cancellationToken);
}
////等待turnContext.SendActivityAsync(MessageFactory.Text(结果[0]。答案),cancellationToken);
}
else if(conversationService.currentService.Equals(“dy365”))
{
//在这里呼叫您的dy 365服务
Wait turnContext.SendActivityAsync(MessageFactory.Text(“dy365响应”),cancellationToken);
}
其他的
{
Wait turnContext.SendActivityAsync(MessageFactory.Text(“错误”),cancellationToken);
};
}
}
公共课堂会话服务
{
公共字符串currentService{get;set;}
}
}
Dialog/RootDialog.cs
命名空间Microsoft.BotBuilderSamples.Dialog
{
公共类RootDialog:ComponentDialog
{
private const string InitialDialog=“InitialDialog”;
公共根对话框(IBotServices)
:基(“根”)
{
添加对话框(新QnAMakerBaseDialog(服务
public class RootDialog : ComponentDialog
{
private const string InitialDialog = "initial-dialog";
public RootDialog(IBotServices services)
: base("root")
{
AddDialog(new QnAMakerBaseDialog(services));
AddDialog(new WaterfallDialog(InitialDialog)
.AddStep(InitialStepAsync));
// The initial child Dialog to run.
InitialDialogId = InitialDialog;
}
private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Set values for generate answer options.
var qnamakerOptions = new QnAMakerOptions
{
ScoreThreshold = QnAMakerBaseDialog.DefaultThreshold,
Top = QnAMakerBaseDialog.DefaultTopN,
Context = new QnARequestContext()
};
// Set values for dialog responses.
var qnaDialogResponseOptions = new QnADialogResponseOptions
{
NoAnswer = QnAMakerBaseDialog.DefaultNoAnswer,
ActiveLearningCardTitle = QnAMakerBaseDialog.DefaultCardTitle,
CardNoMatchText = QnAMakerBaseDialog.DefaultCardNoMatchText,
CardNoMatchResponse = QnAMakerBaseDialog.DefaultCardNoMatchResponse
};
var dialogOptions = new Dictionary<string, object>
{
[QnAMakerBaseDialog.QnAOptions] = qnamakerOptions,
[QnAMakerBaseDialog.QnADialogResponseOptions] = qnaDialogResponseOptions
};
return await stepContext.BeginDialogAsync(nameof(QnAMakerBaseDialog), dialogOptions, cancellationToken);
}
}
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Builder.AI.QnA;
namespace Microsoft.BotBuilderSamples
{
public class StateManagementBot : ActivityHandler
{
private BotState _conversationState;
private BotState _userState;
private string KBID = "<KBID>";
private string ENDPOINT_KEY = "<KEY>";
private string HOST = "<QnA maker host>";
public StateManagementBot(ConversationState conversationState, UserState userState)
{
_conversationState = conversationState;
_userState = userState;
}
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
await base.OnTurnAsync(turnContext, cancellationToken);
// Save any state changes that might have occured during the turn.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
var card = new HeroCard();
card.Title = "";
card.Text = @"Welcome to Welcome Users bot sample! This Introduction card";
card.Images = new List<CardImage>() { new CardImage("https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwjQjeeS4obmAhUIfnAKHQGgCB0QjRx6BAgBEAQ&url=https%3A%2F%2Fdougame.tistory.com%2F98&psig=AOvVaw11Y-BZJtsxh1pTp0Qxzedb&ust=1574819546545481") };
card.Buttons = new List<CardAction>()
{
//new CardAction(ActionTypes.OpenUrl, "FAQ", null, "Get an overview", "Get an overview", "https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0"),
new CardAction(ActionTypes.PostBack, "Info Dynamics365", null, "Ask a question", "Ask a question", "dy365"),
new CardAction(ActionTypes.PostBack, "FAQ",null , "Ask a question", "Ask a question", "FAQ" ),
new CardAction(ActionTypes.OpenUrl, "Connect", null, "Learn how to deploy", "Learn how to deploy", "https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0"),
};
var response = MessageFactory.Attachment(card.ToAttachment());
await turnContext.SendActivityAsync(response, cancellationToken);
}
}
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var qnaMaker = new QnAMaker(new QnAMakerEndpoint
{
KnowledgeBaseId = KBID,
EndpointKey = ENDPOINT_KEY,
Host = HOST
},
null,
new System.Net.Http.HttpClient());
var conversationStateAccessors = _conversationState.CreateProperty<ConversationService>(nameof(ConversationService));
var conversationService = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationService());
var input = turnContext.Activity.Text;
if (String.IsNullOrEmpty(conversationService.currentService) && (input.Equals("FAQ") || input.Equals("dy365")))
{
conversationService.currentService = input;
await turnContext.SendActivityAsync(MessageFactory.Text("using "+ input + " service , pls enter your " + input + " question"), cancellationToken);
} else if (String.IsNullOrEmpty(conversationService.currentService)) {
await turnContext.SendActivityAsync(MessageFactory.Text("select a service from hero card first"), cancellationToken);
}
else if (conversationService.currentService.Equals("FAQ"))
{
var result = qnaMaker.GetAnswersAsync(turnContext).GetAwaiter().GetResult();
if (result.Length == 0)
{
await turnContext.SendActivityAsync(MessageFactory.Text("Sorry , I can't find any answer for it"), cancellationToken);
}
else {
await turnContext.SendActivityAsync(MessageFactory.Text(result[0].Answer), cancellationToken);
}
}
else if (conversationService.currentService.Equals("dy365"))
{
//call your dy 365 service here
await turnContext.SendActivityAsync(MessageFactory.Text("dy365 response"), cancellationToken);
}
else {
await turnContext.SendActivityAsync(MessageFactory.Text("error"), cancellationToken);
};
}
}
public class ConversationService{
public string currentService { get; set; }
}
}