2

我想构建一个可以利用 QnA api 和 google drive 的搜索 api 的机器人。我会问用户他是否想查询知识库或他想搜索驱动器中的文件。为此,我选择了 Bot Framework 的 Form Flow 机器人模板。在这种情况下,如果用户选择查询 qna api,那么我想将问题发布到 QNA api。如何在我的机器人中实现这一点?我在哪里可以找到用户在流程中的选择。

这是MessageController.cs

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                await Conversation.SendAsync(activity, MakeRootDialog);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

        private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                // Handle conversation state changes, like members being added and removed
                // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
                // Not available in all channels
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
            }
            else if (message.Type == ActivityTypes.Ping)
            {
            }

            return null;
        }
        internal static IDialog<UserIntent> MakeRootDialog()
        {
            return Chain.From(() => FormDialog.FromForm(UserIntent.BuildForm));
        }

表单生成器

public static IForm<UserIntent> BuildForm()
 {
   return new FormBuilder<UserIntent>()
     .Message("Welcome to the bot!")
     .OnCompletion(async (context, profileForm) =>
      {
         await context.PostAsync("Thank you");
      }).Build();
 }
4

2 回答 2

1

FormFlow更多的是用于引导对话流。在我看来,它似乎不符合您的要求。您可以只使用PromptDialog来获取用户对他们喜欢的搜索类型的回答,然后将下一条消息转发到相应的对话框。就像是:

[Serializable]
public class RootDialog : IDialog<object>
{
    const string QnAMakerOption = "QnA Maker";
    const string GoogleDriveOption = "Google Drive";
    const string QueryTypeDataKey = "QueryType";

    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        if(context.UserData.ContainsKey(QueryTypeDataKey))
        {
            var userChoice = context.UserData.GetValue<string>(QueryTypeDataKey);
            if(userChoice == QnAMakerOption)
                await context.Forward(new QnAMakerDialog(), ResumeAfterQnaMakerSearch, activity);
            else
                await context.Forward(new GoogleDialog(), ResumeAfterGoogleSearch, activity);
        }
        else
        {
            PromptDialog.Choice(
                  context: context,
                  resume: ChoiceReceivedAsync,
                  options: new[] { QnAMakerOption, GoogleDriveOption },
                  prompt: "Hi. How would you like to perform the search?",
                  retry: "That is not an option. Please try again.",
                  promptStyle: PromptStyle.Auto
            );
        }            
    }
    private Task ResumeAfterGoogleSearch(IDialogContext context, IAwaitable<object> result)
    {
        //do something after the google search dialog finishes
        return Task.CompletedTask;
    }
    private Task ResumeAfterQnaMakerSearch(IDialogContext context, IAwaitable<object> result)
    {
        //do something after the qnamaker dialog finishes
        return Task.CompletedTask;
    }

    private async Task ChoiceReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var userChoice = await result;
        context.UserData.SetValue(QueryTypeDataKey, userChoice);

        await context.PostAsync($"Okay, your preferred search is {userChoice}.  What would you like to search for?");
    }
}
于 2018-03-12T21:08:55.700 回答
0

首先,您需要在 LUIS 门户上创建 LUIS 应用程序,将您的意图与话语一起添加到那里,例如:-意图是“KnowlegeBase”,在其中您可以创建尽可能多的话语来返回此意图。

在应用程序内部,创建一个 LUISDialog 类并从您的 BuildForm 中调用它:-

    await context.Forward(new MyLuisDialog(), ResumeDialog, activity, CancellationToken.None);

LuisDialog 类:-

  public class MyLuisDialog : LuisDialog<object>
{
    private static ILuisService GetLuisService()
    {
        var modelId = //Luis modelID;
        var subscriptionKey = //Luis subscription key
        var staging = //whether point to staging or production LUIS
        var luisModel = new LuisModelAttribute(modelId, subscriptionKey) { Staging = staging };
        return new LuisService(luisModel);
    }

    public MyLuisDialog() : base(GetLuisService())
    {

    }

 [LuisIntent("KnowledgeBase")]
    public async Task KnowledgeAPICall(IDialogContext context, LuisResult result)
    {
        //your code
    }

要利用用户传递的消息,您可以从参数中的上下文中使用它。

于 2018-03-12T09:14:36.317 回答