Override Context.PostAsync in bot structure

I noticed that with the Bot Framework and Telegram channel, emoticons like :) are not converted to emoji.

However, it is very simple, we just need to change :) to: smile: and a nice emoji will appear.

The optimal solution would be to redefine the function context.PostAsync()

to do this kind of string replacement and then continue.

Can we override this method without recompiling the entire structure?

Thank:)

+3


source to share


1 answer


I am assuming that you want to change the message that the bot sends to the user.

Of course, the main option is to just add logic to check the channel in your bot dialog and what it is. However, I suspect you want to reuse this logic in other dialogs, which can also mean just a static method that allows you to do:

context.PostAsync(Utils.TransformMessage(message));

      

Now if you really want to take the cleanest approach I think it should implement its own IMessageActivityMapper

and register it in the Autofac container, so the implementation MapToChannelData_BotToUser

ends up calling it (see here ).



There are several implementations IMessageActivityMapper

here and here you can take a look; although the interface is very simple and the whole idea is that what you get IMessageActivity

is update any existing properties (in your case it will be the Text property) and return the updated IMessageActivity

one so it can be sent to the user.

Once your implementation is ready, you can register it with Autofac by doing the following in Global.asax.cs

.

protected void Application_Start(object sender, EventArgs e)
{
    {
        // http://docs.autofac.org/en/latest/integration/webapi.html#quick-start
        var builder = new ContainerBuilder();

        // Register your mapper 
        builder
        .RegisterType<MyActivityMapper>()
        .AsImplementedInterfaces()
        .SingleInstance();

        // Get your HttpConfiguration.
        var config = GlobalConfiguration.Configuration;

        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Set the dependency resolver to be Autofac.
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    }
}

      

+5


source







All Articles