How can I change the error message "Sorry, my bot code has a problem"?

I need to change the error message "Sorry, my bot code has a problem." This seems like a somewhat complicated process. I tried to do as the blog post says: http://wp.sjkp.dk/change-the-sorry-my-bot-code-is-having-an-issue-in-microsoft-bot-framework /

I don’t have knowledge about dependency injection or control inversion, so this becomes quite a challenge. I am using Bot Builder 3.5.5.

This is the code from the blog I was trying to use on my bot:

Class PostUnhandledExceptionToUser:

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Connector;
using System;
using System.Diagnostics;
using System.Net.Mime;
using System.Resources;
using System.Threading;
using System.Threading.Tasks;

namespace Tiimo.Bot.BotFramework
{
    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 (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);
                    }
                    else
                    {
                        await this.botToUser.PostAsync("My Personal Error Message");
                    }
                }
                catch (Exception inner)
                {
                    this.trace.WriteLine(inner);
                }

                throw;
            }
        }
    }
}

Class DefaultExceptionMessageOverrideModule:

using Autofac;
using Autofac.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.History;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Scorables.Internals;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Resources;
using System.Web;


namespace Tiimo.Bot.BotFramework
{
    public class DefaultExceptionMessageOverrideModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<PostUnhandledExceptionToUser>().Keyed<IPostToBot>(typeof(PostUnhandledExceptionToUser)).InstancePerLifetimeScope();


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

        public static IRegistrationBuilder<TLimit, SimpleActivatorData, SingleRegistrationStyle> RegisterAdapterChain<TLimit>(ContainerBuilder builder, params Type[] types)
        {
            return
                builder
                .Register(c =>
                {
                // http://stackoverflow.com/questions/23406641/how-to-mix-decorators-in-autofac
                TLimit service = default(TLimit);
                    for (int index = 0; index < types.Length; ++index)
                    {
                    // resolve the keyed adapter, passing the previous service as the inner parameter
                    service = c.ResolveKeyed<TLimit>(types[index], TypedParameter.From(service));
                    }

                    return service;
                })
                .As<TLimit>();
        }
    }  
}

After creating these classes, I went to the Global.asax file and put this code:

var builder = new ContainerBuilder();
builder.RegisterModule(new DefaultExceptionMessageOverrideModule());
builder.Update(Conversation.Container);

, , :

"Void.ctor(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot, Microsoft.Bot.Builder.Dialogs.Internals.IBotData) " PersistentDialogTask. → Valor não pode ser nulo. Nome do parâmetro: (. ).

, -

'Void.ctor(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot, Microsoft.Bot.Builder.Dialogs.Internals.IBotData) "PersistentDialogTask. → . : ( . ).

- ? ? ?

+4
1

:

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

, - PersistentDialogTask IPostToBot, , . DialogModule.

, , EventLoopDialogTask - IPostToBot. , , PersisteDialogTask .

+3

Source: https://habr.com/ru/post/1672997/


All Articles