How ExGram.Router dispatching works
masterThe ExGram.Router uses a declarative scope / filter / handle DSL to replace manual handle/2 pattern matching.
Dispatch Logic:
- Order Matters: Scopes are evaluated top-to-bottom in the order they are declared.
- AND Logic: All filters within a single
scopemust pass for the scope to match. - First Match Wins: The first matching leaf scope executes its handler, and dispatching stops.
- Fallback: A
scopedefined without anyfiltermatches everything. Place this at the bottom of your module to act as a catch-all fallback.
Handler Arities:
- 1-arity: Receives only the
context. Use this for simple responses. - 2-arity: Receives
(update_info, context). Use this when you need to extract data (like text or callback data) directly from the parsed update tuple.
# 1-arity: receives only context
def start(context) do
answer(context, "Welcome!")
end
# 2-arity: receives (update_info, context)
def echo({:text, text, _msg}, context) do
answer(context, text)
end