rockneurotiko/ex_gram

How to process updates before handling them?

Closed this issue · 1 comments

I am writing a bot for a group and I want to collect some statistics from messages.
Is there a way to do it without injecting new code in every handle/2 call? For example, I want to count some keywords from received messages per sender and then handle it, how can I do it without consuming the update data?

Hey!

Yeah, ExGram have middlewares that executes before all the updates handlers, you can modify the updates, or all you want:

defmodule MyMiddleware do
  use ExGram.Middleware

  alias ExGram.Cnt

  def call(%Cnt{update: update} = cnt, _opts) do
    cnt
  end

  def call(cnt, _opts), do: cnt
end

An example that removes the bot's @username from message text:

https://github.com/rockneurotiko/ex_gram/blob/master/lib/ex_gram/middleware/ignore_username.ex