ChatHandler unstable
The service a consumer provides to receive chat events.
Adapters take the ChatHandler service 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 ChatEvent.dedupeKey.
Errors
NotAccepted
unstableError for an event that was not accepted.
Details
A handler fails with it to refuse an event, and an adapter's entry point fails with it when it could not finish normalising one. Slack answers 503 so the event is redelivered; the Discord gateway layer drops it.
Gotchas
It has no fields, so whoever raises it logs the reason first.
Signature
declare class NotAccepted extends { readonly _tag: "NotAccepted";} & YieldableError<this> { constructor(...args: [props?: { readonly _tag?: "NotAccepted"; }, options?: MakeOptions]);}Layers
Creates the ChatHandler layer from a function that handles one event.
Details
- The bot's own messages are dropped before
handleruns, unlessdeliverSelfistrue. Other bots' messages are delivered, flagged withauthor.isBot. - Every event runs in a
ChatHandler.handlespan and its ownScope.
Signature
declare function layer<R>(handle: (event: { readonly _tag: "MessageCreated"; readonly kind: "channel" | "thread" | "direct"; readonly message: { readonly attachments: readonly Array<{ readonly contentType?: string; readonly id: string; readonly name?: string; readonly size?: number; }>; readonly author: { readonly id: string; readonly isBot: boolean; readonly isSelf: boolean; readonly name?: string; }; readonly id: string; readonly mentions: readonly Array<{ readonly end: number; readonly id: string; readonly isSelf: boolean; readonly kind: "role" | "user" | "channel" | "broadcast"; readonly start: number; }>; readonly raw: Json; readonly replyTo?: { readonly authorId?: string; readonly isSelf?: boolean; readonly messageId: string; }; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly timestamp: Utc; }; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; };} | { readonly _tag: "ReactionAdded"; readonly emoji: string; readonly messageAuthorId?: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly timestamp?: Utc; readonly userId: string;} | { readonly _tag: "ReactionRemoved"; readonly emoji: string; readonly messageAuthorId?: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly timestamp?: Utc; readonly userId: string;} | { readonly _tag: "StopRequested"; readonly id: string; readonly messageIds: readonly Array<string>; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string;}) => Effect<void, NotAccepted, R>, options?: { readonly deliverSelf?: boolean;}): Layer<ChatHandler, never, Exclude<R, Scope>>Services
ChatHandler
unstableService that receives inbound chat events, provided by the consumer.
Details
Adapters take the service and call handle. Success hands the event to the
consumer, and NotAccepted refuses it. Success does not stop the platform
from delivering the event again: delivery is at-least-once.
Gotchas
Forking the work and returning loses events on a crash. How durable an accepted event is belongs to the consumer.
See
- layer for the only constructor
Signature
declare class ChatHandler extends Shape<"effect/chat/ChatHandler", { readonly handle: (event: { readonly _tag: "MessageCreated"; readonly kind: "channel" | "thread" | "direct"; readonly message: { readonly attachments: readonly Array<{ readonly contentType?: string; readonly id: string; readonly name?: string; readonly size?: number; }>; readonly author: { readonly id: string; readonly isBot: boolean; readonly isSelf: boolean; readonly name?: string; }; readonly id: string; readonly mentions: readonly Array<{ readonly end: number; readonly id: string; readonly isSelf: boolean; readonly kind: "role" | "user" | "channel" | "broadcast"; readonly start: number; }>; readonly raw: Json; readonly replyTo?: { readonly authorId?: string; readonly isSelf?: boolean; readonly messageId: string; }; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly timestamp: Utc; }; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; } | { readonly _tag: "ReactionAdded"; readonly emoji: string; readonly messageAuthorId?: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly timestamp?: Utc; readonly userId: string; } | { readonly _tag: "ReactionRemoved"; readonly emoji: string; readonly messageAuthorId?: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly timestamp?: Utc; readonly userId: string; } | { readonly _tag: "StopRequested"; readonly id: string; readonly messageIds: readonly Array<string>; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; }) => Effect<void, NotAccepted>;}, this> { constructor(_: never);}