随机"抱歉,我的机器人代码存在问题"聊天期间出现的消息

时间:2018-01-26 09:52:27

标签: azure error-handling bots botframework

有点奇怪,但是当我与我的机器人进行对话时,我几乎是随机得到这个错误。它非常不一致,在对话过程中随时出现。对话框本身按预期工作,因此它让我怀疑可能有某些东西触发控制器的错误。 (发布在下面)

之前有没有遇到过这个?可能是因为它是如何托管的?你会建议什么是解决这个问题的最佳方法,因为除了这个模糊的错误信息之外,错误处理不能提供任何我能找到的信息。

任何帮助都将非常感激。谢谢!

控制器:

using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;

namespace ExampleBot
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {

                await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
            }
            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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

Sorry, my bot code is having a issue是机器人框架在服务器端发生错误时显示的通用消息。如果您正在使用网络聊天,您可以在导航工具上看到http回复,回复代码为500。

在您的情况下,您提供的控制器中没有任何特殊内容可能会引发错误,必须有其他内容。您可以通过检查日志和/或将ApplicationInsights添加到项目来进行调查,以获取有关遇到的异常的更多详细信息。

答案 1 :(得分:1)

您可能希望覆盖泛型错误的默认行为,以便在发生时可以更改默认错误消息并在调试时中断并查看异常。

我经常遇到botframwork错误,这有助于我调查问题。

添加新组件:

public class PostUnhandledExceptionToUser : IPostToBot
{
    private readonly ResourceManager resources;
    private readonly IPostToBot inner;
    private readonly IBotToUser botToUser;
    private readonly TraceListener trace;

    public PostUnhandledExceptionToUser(IPostToBot inner, IBotToUser botToUser, ResourceManager resources, TraceListener trace)
    {
        SetField.NotNull(out this.inner, nameof(inner), inner);
        SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
        SetField.NotNull(out this.resources, nameof(resources), resources);
        SetField.NotNull(out this.trace, nameof(trace), trace);
    }

    async Task IPostToBot.PostAsync(IActivity activity, CancellationToken token)
    {
        try
        {
            await this.inner.PostAsync(activity, token);
        }
        catch(HttpException hex)
        {
            try
            {
                //Post my custom error message
                await this.botToUser.PostAsync(ConfigurationManager.AppSettings["DefaultErrorMessage"]);
            }
            catch (Exception inner)
            {
                this.trace.WriteLine(inner);
            }

            throw hex;
        }
        catch (Exception error)
        {
            try
            {
                if (Debugger.IsAttached)
                {
                    var message = this.botToUser.MakeMessage();
                    message.Text = $"Exception: { error.Message}";
                    message.Attachments = new[]
                    {
                        new Attachment(contentType: MediaTypeNames.Text.Plain, content: error.StackTrace)
                    };

                    await this.botToUser.PostAsync(message);
                }
            }
            catch (Exception inner)
            {
                this.trace.WriteLine(inner);
            }

            throw;
        }
    }

}

 在autofac中注册组件并将其添加到链的末尾:

protected override void Load(ContainerBuilder builder)
{
    builder.RegisterType<PostUnhandledExceptionToUser>().Keyed<IPostToBot>(typeof(PostUnhandledExceptionToUser)).InstancePerLifetimeScope();


    RegisterAdapterChain<IPostToBot>(builder,
            typeof(EventLoopDialogTask),
            typeof(SetAmbientThreadCulture),
            typeof(PersistentDialogTask),
            typeof(ExceptionTranslationDialogTask),
            typeof(SerializeByConversation),
            typeof(PostUnhandledExceptionToUser),
            typeof(LogPostToBot)
        )
        .InstancePerLifetimeScope();
}