Skip to content

DrivedInComingHandlers

immmdreza edited this page Jul 3, 2021 · 2 revisions

This type of handlers are useful when have a lot of handlers and you need to categorize them or when you need a isolated and feature-full environment for every one of your handlers.

In order to do that you have to create a class that is inherited from one of our base inComing handler classes ( one class foreach update type ).

The filters should be passed to the base constructor and callback function is an override method.

Base Classes

Base classes are available under following namespace:

using Flamingo.Fishes.InComingFishes

As said before, there're one base class for each update type like following:

InComingMessage // for messages
InComingCallbackQuery // for callback queries
InComingInlineQuery // for inline queries
...
...

// and for other update types

So, the class you are creating should be inherited from one of these.

Example

Imagine you are creating an handler for /start command in private chat

First of all create a class that is inherited from InComingMessage:

public class InComingStartMessage : InComingMessage
{

}

Then create constructor:

public class InComingStartMessage : InComingMessage
{
    public InComingStartMessage() 
        : base(filters)
    { }
}

The filters should be passed to base()

Here we need CommandFilter and PrivateFilter<Message>

public class InComingStartMessage : InComingMessage
{
    public InComingStartMessage() 
        : base(new PrivateFilter<Message>() & new CommandFilter("start"))
    { }
}

Filters combined using &, meaning both of them should be available

And as callback function you should override a protected method which calls GetEatenWrapper and it takes Update type as parameter ( here Message )

public class InComingStartMessage : InComingMessage
{
    public InComingStartMessage() 
        : base(new PrivateFilter<Message>() & new CommandFilter("start"))
    { }

    protected override async Task GetEatenWrapper(Message inComing)
    {
        await ReplyText("Just Started!");
    }
}

And yes! there are helper methods like ReplyText() and more available.

Also properties like Cdmt, BotClient, Sender and more are available.

using Flamingo.Filters.MessageFilters;
using Flamingo.Filters.SharedFilters;
using Flamingo.Fishes.InComingFishes;
using System.Threading.Tasks;
using Telegram.Bot.Types;

namespace FlamingoProduction.MyInComings
{
    public class InComingStartMessage : InComingMessage
    {
        public InComingStartMessage() 
            : base(new PrivateFilter<Message>() & new CommandFilter("start"))
        { }

        protected override async Task GetEatenWrapper(Message inComing)
        {
            await ReplyText("Just Started!");
        }
    }
}

Don't forget to add an instance of your class to the flamingo

flamingo.AddInComing(new InComingStartMessage());

You can add parameters to constructor if you need something special!

Flamingo Framework written in pure c#, install from Nuget

Clone this wiki locally