TelegramBots / Telegram.Bot.Framework

Simple framework for building Telegram bots
MIT License
98 stars 43 forks source link

Strongly typed updates #3

Open poulad opened 7 years ago

poulad commented 7 years ago

It would be nicer to handle updates of a specific type.

Cleaner code:

class MyClass {
    void RespondMessageWithWelcome(Update<MessageUpdate> msgUpdate) { }
    void ReplyToCallbackquery(Update<CallbackQueryUpdate> queryUpdate) { }
}

Possible implementation:

Update Types:

abstract class UpdateContentBase { }
class MessageUpdate : UpdateContentBase { }
class CallbackQueryUpdate : UpdateContentBase { }

New Update class:

interface IUpdate<out T> where T : UpdateContentBase, new() {
    T Content { get; }
}

class Update<T> : IUpdate<T> where T : UpdateContentBase, new() {
    public T Content { get; }
    public Update(T content) {
        Content = content;
    }
}

Not really sure if this is going to work.