GitHub

chat_handler

API reference for chat_handler.

The service a consumer provides to receive chat events (Effect's effect/chat/ChatHandler).

Adapters take ChatHandler from context and call handle for every inbound event. The contract is "accept, fast": success means the event is the consumer's. The platform can still deliver it again, so drop repeats with chat_event.dedupe_key. Forking the work and returning loses events on a crash: how durable an accepted event is belongs to the consumer.

Classes

Functions

layer

layer(handle: Callable[[ChatEvent], Effect[None, NotAccepted, Scope]], *, deliver_self: bool = False) -> Layer[ChatHandler]
layer[R](handle: Callable[[ChatEvent], Effect[None, NotAccepted, R | Scope]], *, deliver_self: bool = False) -> Layer[ChatHandler, Never, R]

The ChatHandler layer from a function that handles one event.

  • The bot's own messages are dropped before handle runs, unless deliver_self. Other bots' messages are delivered, flagged with author.is_bot.
  • Every event runs in a ChatHandler.handle span and its own Scope.
ParameterTypeDescription
handle
deliver_self= False
@overload
def layer(
    handle: Callable[[ChatEvent], Effect[None, NotAccepted, Scope]],
    *,
    deliver_self: bool = False,
) -> Layer[ChatHandler]: ...
@overload
def layer[R](
    handle: Callable[[ChatEvent], Effect[None, NotAccepted, R | Scope]],
    *,
    deliver_self: bool = False,
) -> Layer[ChatHandler, Never, R]: ...
def layer[R](
    handle: Callable[[ChatEvent], Effect[None, NotAccepted, R | Scope]],
    *,
    deliver_self: bool = False,
) -> Layer[ChatHandler, Never, R]:
    """The ``ChatHandler`` layer from a function that handles one event.

    - The bot's own messages are dropped before ``handle`` runs, unless
      ``deliver_self``. Other bots' messages are delivered, flagged with
      ``author.is_bot``.
    - Every event runs in a ``ChatHandler.handle`` span and its own
      ``Scope``.
    """

    def make(context: Context[R]) -> Service:
        def run(event: ChatEvent) -> Effect[None, NotAccepted]:
            if (
                not deliver_self
                and isinstance(event, MessageCreated)
                and event.message.author.is_self
            ):
                return fx.void
            return (
                fx.scoped(_call_user(handle, event))
                .pipe(fx.provide_context(context))
                .with_span(
                    "ChatHandler.handle",
                    attributes={"chat.platform": event.thread.platform, "chat.op": "handle"},
                )
            )

        return Service(run)

    return _layer_effect(ChatHandler, fx.context().map(make))  # pyright: ignore[reportArgumentType]