Skip to content
Peter Csajtai edited this page Oct 30, 2018 · 2 revisions

If you are facing a use case which is not covered by the built-in bots in Trybot, you have the option to make your own bot. All you have to do is to inherit from one of the Bot, Bot<TResult>, ConfigurableBot, ConfigurableBot<TResult> abstract classes.

They are for different cases:

  • Bot: Inheriting from this allows you to create a bot without configuration which can handle operations without return value.
    class CustomBot : Bot
    {
        internal CustomBot(Bot innerBot) // the inner bot is the next bot in the chain
            : base(innerBot)
        { }
    
        public override void Execute(IBotOperation operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // base.InnerBot.Execute(operation, context, token);
        }
    
        public override Task ExecuteAsync(IAsyncBotOperation operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // base.InnerBot.ExecuteAsync(operation, context, token);
        }
    }
  • Bot<TResult>: Inheriting from this allows you to create a bot without configuration which can handle operations with return value.
    class CustomBot<TResult> : Bot<TResult>
    {
        internal CustomBot(Bot<TResult> innerBot) // the inner bot is the next bot in the chain
            : base(innerBot)
        { }
    
        public override TResult Execute(IBotOperation<TResult> operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // return base.InnerBot.Execute(operation, context, token);
        }
    
        public override Task<TResult> ExecuteAsync(IAsyncBotOperation<TResult> operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // return base.InnerBot.ExecuteAsync(operation, context, token);
        }
    }
  • ConfigurableBot: Inheriting from this allows you to create a bot with configuration which can handle operations without return value.
    class CustomBot : ConfigurableBot<CustomConfiguration>
    {
        internal CustomBot(Bot innerBot, CustomConfiguration configuration) 
            : base(innerBot, configuration) // the inner bot is the next bot in the chain
        { }
    
        public override void Execute(IBotOperation operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // base.InnerBot.Execute(operation, context, token);
        }
    
        public override Task ExecuteAsync(IAsyncBotOperation operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // base.InnerBot.ExecuteAsync(operation, context, token);
        }
    }
  • ConfigurableBot<TResult>: Inheriting from this allows you to create a bot with configuration which can handle operations with return value.
    class CustomBot<TResult, CustomConfiguration> : ConfigurableBot<CustomConfiguration, TResult>
    {
        internal CustomBot(Bot<TResult> innerBot, CustomConfiguration configuration) 
            : base(innerBot, configuration) // the inner bot is the next bot in the chain
        { }
    
        public override TResult Execute(IBotOperation<TResult> operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // return base.InnerBot.Execute(operation, context, token);
        }
    
        public override Task<TResult> ExecuteAsync(IAsyncBotOperation<TResult> operation, ExecutionContext context, CancellationToken token)
        { 
            // here you can use your custom handling logic on the inner bots call:
            // return base.InnerBot.ExecuteAsync(operation, context, token);
        }
    }

Then you can register your custom bot into a BotPolicy like this:

  • In case of a simple bot
    policy.Configure(policyConfig => policyConfig
        .AddBot(innerBot => new CustomBot(innerBot));
  • In case of a bot with configuration
    policy.Configure(policyConfig => policyConfig
        .AddBot((innerBot, config) => new CustomBot(innerBot, config), new CustomConfiguration());

Clone this wiki locally