如何将LUIS服务与bot集成?

时间:2017-12-02 08:12:23

标签: c# botframework cortana

我正在使用LUIS开发CORTANA应用程序,Root对话框正在使用LUIS,并且根据用户意图我将其重定向到另一个对话框但我观察到执行Root对话框构造函数后发生了异常,而我有另一个对话框没有LUIS(即InsightsDialog评论如下),工作正常。

以下代码从MessagesController重定向到Root Dialog:

error: underscore lifetimes are unstable (see issue #44524)

根对话代码:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                    await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
                   //await Conversation.SendAsync(activity, () => new Dialogs.InsightsDialog());
            }
            else
            {
                await HandleSystemMessage(activity);
            }

            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

我在日志中发现了以下异常,它在调用RootDialog类的构造函数后立即发生:

[Serializable]
    public class RootDialog : LuisDialog<object>
    {
        InsightsDialog customerInsightsDialog = null;
        public RootDialog() : base(new LuisService(Utility.GetLUISAttributesFromConfig()))
        {
            customerInsightsDialog = new InsightsDialog();
        }
        public static LuisModelAttribute GetLUISAttributesFromConfig()
        {
                return new LuisModelAttribute(Constants.RootDialogLuisModelId, Constants.RootDialogLuisSubscriptionKey);
        }

       #region Intents
        [LuisIntent("")]
        [LuisIntent("None")]
        public async Task None(IDialogContext context, LuisResult result)
        {
                string userInput = result.Query;
                context.Call(customerInsightsDialog, ResumeAfterInsightsDialog);
        }

        [LuisIntent("Greetings")]
        public async Task Greetings(IDialogContext context, LuisResult result)
        {
                string userInput = result.Query;
                context.Call(customerInsightsDialog, ResumeAfterInsightsDialog);
        }

        private Task ResumeAfterInsightsDialog(IDialogContext context, IAwaitable<object> result)
        {
            return Task.CompletedTask;
        }
       #endregion
    }

此代码在模拟器和网络聊天频道上运行良好,但仅在CORTANA频道上抛出异常。感谢帮助我。

1 个答案:

答案 0 :(得分:1)

通过修改类“MessagesController”的现有代码解决了该问题,如下所示:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                  if (string.IsNullOrEmpty(activity.Text))
                     {
                          var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                          Activity reply = activity.CreateReply();
                          reply.Text = string.Format(Resources.WELCOME_MESSAGE, Constants.UserInfo.FullName);
                          reply.Speak = SSMLHelper.Speak(string.Format(Resources.WELCOME_MESSAGE, Constants.UserInfo.FullName));
                          reply.InputHint = InputHints.ExpectingInput;

                          await connector.Conversations.ReplyToActivityAsync(reply);
                     }
                 else
                    {
                          await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
                     }
            }
            else
            {
                await HandleSystemMessage(activity);
            }

            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

实际上,“Activity”对象的“Text”属性为空。所以现在我要求用户输入(通过发言)一些文字继续进行。