protoncr / tourmaline

Simple Telegram bot library for Crystal
https://tourmaline.dev
MIT License
158 stars 38 forks source link

Figure out better event handling #16

Closed watzon closed 4 years ago

watzon commented 4 years ago

So currently the event system isn't bad, but it could be better. As things sit right now EventHandler has no context as to what might be available for any given event. For instance:

@[On(:text)]
def on_text(ctx)
  puts ctx.message.text
end

One might expect this to compile, but it won't. This is because all events are handed the same EventContext object which may or may not have a message attached. Ideally the context should be aware of what events will certainly have what properties.

Additionally, since all On events have the same context, there's no good way to handle filters. I would like to implement filters similar to what [python-telegram-bot]() has, but to do so the filters should be context aware so that we can have certain filters specific to certain event types.

A good implementation may look something like this:

@[On(:text, filters: [ Filters::RegexFilter.new(/^hello/) ])]
def on_hello(ctx)
  ctx.message.reply("https://nohello.com")
end

Though I'd like to find a somewhat cleaner way of handling filters.

I'm 100% open to comments and suggestions on this.