ChatInteraction unstable
Typed buttons, selects, modals and slash commands, and the service that receives their clicks, submits and commands.
A definition carries an element's id and the schemas of its values. It
builds the element to post (of, open) and gives the typed slot of its
handler, the way Rpc and RpcGroup do. A group of definitions becomes a
layer of handlers, and layer provides the Handler service that adapters
call.
Details
- Clicks, submits and commands are not
ChatEvents. They need an answer inside the platform's 3 seconds, so they go toHandler, which answers by return value and has no error channel. - Slack and Discord don't redeliver a click, a submit or a command, so interactions carry no dedupe key. An adapter whose platform does redeliver them documents a delivery key of its own.
Example A button that opens a modal
import { Effect, Layer, Schema } from "effect"import { ChatInteraction } from "effect/chat"
const Deploy = Schema.Struct({ version: Schema.String })
const Reject = ChatInteraction.button("reject", { label: "Reject", style: "danger", value: Deploy })const RejectReason = ChatInteraction.modal("reject-reason", { title: "Reject deploy", metadata: Deploy, fields: { reason: ChatInteraction.textInput({ label: "Reason", multiline: true }) }})
const Interactions = ChatInteraction.group(Reject, RejectReason).toLayer({ reject: (click) => Effect.succeed(RejectReason.open({ metadata: click.value })), "reject-reason": (submit) => submit.values.reason.length < 10 ? Effect.succeed(ChatInteraction.errors({ reason: "Say a bit more." })) : Effect.succeed(ChatInteraction.close)}).pipe(Layer.provideMerge(ChatInteraction.layer()))Definitions
A button definition.
Details
of gives the element to post. It takes the value when the definition has
a schema for one, and overrides change the label or style of this one
element.
Gotchas
of throws when the value cannot be encoded (a schema with no JSON form),
as Schema.encodeSync does.
Signature
interface Button<out Id extends string, out Value extends ValueSchema | undefined = undefined> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly id: Id; readonly kind: "button"; readonly label: string; readonly of: (...args: Value extends ValueSchema ? [value: Value["Type"], overrides?: Overrides] : [overrides?: Overrides]) => { readonly _tag: "Button"; readonly id: string; readonly label: string; readonly style?: "default" | "primary" | "danger"; readonly value?: string; }; readonly style: "default" | "primary" | "danger" | undefined; readonly value: Value;}Defines a slash command.
Details
- The id is the command's name without the slash. It shares the registry's id space with buttons, selects and modals.
- Slack sends everything typed after the name as one string. Discord gets
one
STRINGoption namedargument.name, and a command with noargumenthas no options. argument.requireddefaults to false. Discord's client enforces it. Slack cannot, so a portable handler still checks for an emptytext.
Gotchas
A portable name matches ^[a-z0-9_-]{1,31}$ and a portable description is
100 characters or less. Neither is checked here.
Signature
declare function command<Id extends string>(id: Id, options: { readonly argument?: { readonly description: string; readonly name: string; readonly required?: boolean; }; readonly description: string;}): Command<Id>A slash command definition: a name and at most one text argument.
Details
description and argument are what registration needs. Slack reads its
own from the app manifest, so there they are documentation.
Signature
interface Command<out Id extends string> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly argument: { readonly description: string; readonly name: string; readonly required: boolean; } | undefined; readonly description: string; readonly id: Id; readonly kind: "command";}Any definition that has a handler.
Signature
type Definition = Button<string, any> | Select<string, any> | Modal<string, any, any> | Command<string>Defines a modal.
Details
- The id names the submit's handler in a
group. With the encoded metadata it counts towardlimits.modalMetadataLength. - Inputs and texts together count toward
limits.modalFields.
Gotchas
A modal the platform will not take can only fail after the click's or
command's handler returned it: the adapter opens nothing and calls
Handler.refused. A portable modal has a title of 24 or less, labels of 45
or less, at most 5 fields, and an id plus metadata under 100 characters.
Signature
declare function modal<Id extends string, Fs extends Fields, Metadata extends ValueSchema | undefined = undefined>(id: Id, options: { readonly fields: Fs; readonly metadata?: Metadata; readonly submitLabel?: string; readonly title: string;}): Modal<Id, Metadata, Fs>A modal definition.
Details
open gives the reply that opens the modal. A click's or a command's
handler can return it.
Gotchas
open throws when the metadata or an option's value cannot be encoded.
Signature
interface Modal<out Id extends string, in out Metadata extends ValueSchema | undefined = undefined, in out Fs extends Fields = {}> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly fields: Fs; readonly id: Id; readonly kind: "modal"; readonly metadata: Metadata; readonly open: (...args: {} extends OpenOptions<Metadata, Fs> ? [options?: OpenOptions<Metadata, Fs>] : [options: OpenOptions<Metadata, Fs>]) => { readonly _tag: "OpenModal"; readonly modal: { readonly _tag: "Modal"; readonly elements: readonly Array<{ readonly _tag: "TextInput"; readonly id: string; readonly initial?: string; readonly label: string; readonly maxLength?: number; readonly minLength?: number; readonly multiline?: boolean; readonly optional?: boolean; readonly placeholder?: string; } | { readonly _tag: "SelectInput"; readonly id: string; readonly initial?: string; readonly label: string; readonly optional?: boolean; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; } | { readonly _tag: "Text"; readonly text: string; }>; readonly id: string; readonly metadata?: string; readonly submitLabel?: string; readonly title: string; }; }; readonly submitLabel: string | undefined; readonly title: string;}Defines a single-choice select for a message.
Details
With no value schema the options' values are plain strings.
Signature
declare function select<Id extends string, Value extends ValueSchema | undefined = undefined>(id: Id, options: { readonly placeholder?: string; readonly value?: Value;}): Select<Id, Value>A select definition, for a message. Its options are given at the post, to
of.
Gotchas
of throws when an option's value cannot be encoded.
Signature
interface Select<out Id extends string, out Value extends ValueSchema | undefined = undefined> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly id: Id; readonly kind: "select"; readonly of: (options: { readonly options: readonly Array<{ readonly label: string; readonly value: TypeOf<Value, string>; }>; readonly selected?: TypeOf<Value, string>; }) => { readonly _tag: "Select"; readonly id: string; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; readonly selected?: string; }; readonly placeholder: string | undefined; readonly value: Value;}Events
ButtonClicked
unstableSchema for a click on a button.
Details
valueis still encoded, and absent for a button with no value.threadandmessageIdare where the message carrying the button lives.rawis the platform's interaction payload.
Gotchas
On a command's private answer, messageId still tells which answer was
clicked, but no message op works with it: edit, delete and
fetchMessage can't reach a private answer. On Slack chat.update on it
answered message_not_found live, and edit fails NotFound. On
Discord only the interaction's token reaches it, which is from Discord's
docs and was not run. Only raw tells the two apart: Slack's
container.is_ephemeral, Discord's message.flags with 64.
Signature
declare const ButtonClicked: TaggedStruct<"ButtonClicked", { readonly id: String; readonly messageId: String; readonly raw: Codec<Json, Json, never, never>; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String; readonly value: optionalKey<String>;}>A click on a button.
Signature
type ButtonClicked = typeof ButtonClicked.TypeCommandInvoked
unstableSchema for a slash command.
Details
idis the command's name, without the slash.textis what was typed after the name, as the platform sent it, and empty when nothing was. It is not normalised.threadis where the command was typed. On Slack that is always a channel or a direct: Slack does not deliver an app's command typed in a thread. There is nomessageId, since a command comes from no message.rawis the platform's payload. It holds whatChat.followUpneeds, so it is a credential for as long aslimits.followUpAge.
Signature
declare const CommandInvoked: TaggedStruct<"CommandInvoked", { readonly id: String; readonly raw: Codec<Json, Json, never, never>; readonly text: String; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String;}>A slash command.
Signature
type CommandInvoked = typeof CommandInvoked.TypeInteraction
unstableSchema for what an adapter hands to Handler.
Details
Not part of ChatEvent: these need an answer inside the platform's 3
seconds.
Signature
declare const Interaction: Union<readonly [TaggedStruct<"ButtonClicked", { readonly id: String; readonly messageId: String; readonly raw: Codec<Json, Json, never, never>; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String; readonly value: optionalKey<String>;}>, TaggedStruct<"SelectChanged", { readonly id: String; readonly messageId: String; readonly raw: Codec<Json, Json, never, never>; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String; readonly value: String;}>, TaggedStruct<"ModalSubmitted", { readonly id: String; readonly messageId: optionalKey<String>; readonly metadata: optionalKey<String>; readonly raw: Codec<Json, Json, never, never>; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String; readonly values: $Record<String, String>;}>, TaggedStruct<"CommandInvoked", { readonly id: String; readonly raw: Codec<Json, Json, never, never>; readonly text: String; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String;}>]>A click, a select choice, a modal submit or a slash command.
Signature
type Interaction = typeof Interaction.TypeModalSubmitted
unstableSchema for a modal submit.
Details
threadandmessageIdare those of the click that opened the modal. A modal a command opened carries the command'sthreadand nomessageId. A modal opened from a command's private answer carries that answer's id, which no message op works with, as onButtonClicked.metadatais still encoded.valuesare by field id, still encoded. An optional field left empty has no key.
Signature
declare const ModalSubmitted: TaggedStruct<"ModalSubmitted", { readonly id: String; readonly messageId: optionalKey<String>; readonly metadata: optionalKey<String>; readonly raw: Codec<Json, Json, never, never>; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String; readonly values: $Record<String, String>;}>A modal submit.
Signature
type ModalSubmitted = typeof ModalSubmitted.TypeSelectChanged
unstableSchema for a choice in a select on a message. value is the chosen
option's value, still encoded. messageId is as on ButtonClicked: on a
command's private answer it tells which answer it was, and no message op
works with it.
Signature
declare const SelectChanged: TaggedStruct<"SelectChanged", { readonly id: String; readonly messageId: String; readonly raw: Codec<Json, Json, never, never>; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String; readonly value: String;}>A choice in a select on a message.
Signature
type SelectChanged = typeof SelectChanged.TypeFields
Defines a select for a modal whose options are fixed here. Its value is the union of the option values.
Signature
declare function choice<Value extends string, Optional extends boolean = false>(options: { readonly label: string; readonly optional?: Optional; readonly options: readonly Array<{ readonly label: string; readonly value: Value; }>; readonly placeholder?: string;}): Choice<Value, NoInfer<Optional>>A select in a modal whose options are fixed in the definition.
Signature
interface Choice<out Value extends string, out Optional extends boolean = false> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly kind: "choice"; readonly label: string; readonly optional: Optional; readonly options: readonly Array<{ readonly label: string; readonly value: Value; }>; readonly placeholder: string | undefined;}Any field of a modal.
Signature
type Field = TextInput<boolean> | Choice<string, boolean> | SelectInput<any, boolean> | TextA modal's fields by id, shown in key order.
Signature
interface Fields { [id: string]: Field;}The decoded value of one field.
Signature
type FieldValue<F> = F extends TextInput<infer Optional> ? Maybe<string, Optional> : F extends Choice<infer Value, infer Optional> ? Maybe<Value, Optional> : F extends SelectInput<infer Value, infer Optional> ? Maybe<TypeOf<Value, string>, Optional> : neverWhat a modal's open takes: the metadata when the modal has a schema for
it, the options of each selectInput, and initial values.
Signature
type OpenOptions<Metadata extends ValueSchema | undefined, Fs extends Fields> = Metadata extends ValueSchema ? { readonly metadata: Metadata["Type"];} : {} & {} extends SelectOptions<Fs> ? {} : { readonly options: SelectOptions<Fs>;} & { readonly initial?: { [K in keyof Values<Fs>]: Values<Fs>[K] };}selectInput
unstableDefines a select for a modal. Its id is its key in fields, and its
options are given to the modal's open.
Signature
declare function selectInput<Value extends ValueSchema | undefined = undefined, Optional extends boolean = false>(options: { readonly label: string; readonly optional?: Optional; readonly placeholder?: string; readonly value?: Value;}): SelectInput<Value, NoInfer<Optional>>A select in a modal whose options are given when the modal opens.
Signature
interface SelectInput<out Value extends ValueSchema | undefined = undefined, out Optional extends boolean = false> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly kind: "selectInput"; readonly label: string; readonly optional: Optional; readonly placeholder: string | undefined; readonly value: Value;}The options open must be given: one list per selectInput field.
Signature
type SelectOptions<Fs extends Fields> = { [K in keyof Fs]: Fs[K] extends SelectInput<infer Value, any> ? ReadonlyArray<{ readonly label: string; readonly value: TypeOf<Value, string>;}> : never }Defines plain text for a modal. It has no value, so its key is not in
submit.values.
Gotchas
The text is plain, not Markdown.
Signature
declare function text(text: string): TextPlain text between a modal's inputs.
Signature
interface Text { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly kind: "text"; readonly text: string;}Defines a text input for a modal. Its id is its key in fields.
Details
Its value is a string, or string | undefined when optional is true.
Signature
declare function textInput<Optional extends boolean = false>(options: { readonly label: string; readonly maxLength?: number; readonly minLength?: number; readonly multiline?: boolean; readonly optional?: Optional; readonly placeholder?: string;}): TextInput<NoInfer<Optional>>A text input in a modal, one line or several.
Signature
interface TextInput<out Optional extends boolean = false> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly kind: "textInput"; readonly label: string; readonly maxLength: number | undefined; readonly minLength: number | undefined; readonly multiline: boolean; readonly optional: Optional; readonly placeholder: string | undefined;}submit.values: one key per input, none for text.
Signature
type Values<Fs extends Fields> = { [K in keyof Fs]: FieldValue<Fs[K]> }Groups
Groups definitions so their handlers can be registered together.
Signature
declare function group<Definitions extends readonly Array<Definition>>(...definitions: Definitions): Group<Definitions[number]>A set of definitions whose handlers are registered together.
Details
toLayerregisters the handlers, with their services provided. It needs theRegistrythatChatInteraction.layerprovides.ofis the identity, for handlers written away fromtoLayer.
Gotchas
Two definitions with one id, in one group or across groups, are a defect when the layer is built.
Signature
interface Group<in out D extends Definition> { readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction"; readonly add: <More extends readonly Array<Definition>>(...definitions: More) => Group<D | More[number]>; readonly definitions: ReadonlyMap<string, D>; readonly merge: <Groups extends readonly Array<Group<any>>>(...groups: Groups) => Group<D | Definitions<Groups[number]>>; readonly of: <Handlers extends HandlersFrom<D>>(handlers: Handlers) => Handlers; readonly toLayer: <Handlers extends HandlersFrom<D>, EX = never, RX = never>(build: Handlers | Effect<Handlers, EX, RX>) => Layer<never, EX, Registry | Exclude<RX, Scope> | HandlersServices<Handlers>>;}The handler a definition asks for. None has an error channel.
Details
A button or a select answers ack or a modal to open. A modal answers
close or errors naming its own inputs. A command answers respond,
a modal to open or defer, and never ack: Discord has no silent answer
to a command.
Signature
type HandlerFor<D extends Definition> = D extends Button<any, infer Value> ? (click: Click<TypeOf<Value, undefined>>) => Effect.Effect<ClickReply, never, any> : D extends Select<any, infer Value> ? (selected: Selected<TypeOf<Value, string>>) => Effect.Effect<ClickReply, never, any> : D extends Modal<any, infer Metadata, infer Fs> ? (submit: Submit<TypeOf<Metadata, undefined>, Values<Fs>>) => Effect.Effect<Close | FieldErrors<keyof Values<Fs> & string>, never, any> : D extends Command<any> ? (command: CommandInvoked) => Effect.Effect<Respond | OpenModal | Defer, never, any> : neverOne handler per id in the group.
Signature
type HandlersFrom<D extends Definition> = { [Current in D]: HandlerFor<Current> }The services a set of handlers needs.
Signature
type HandlersServices<Handlers> = { [K in keyof Handlers]: Handlers[K] extends (...args: any) => Effect.Effect<any, any, infer R> ? R : never }[keyof Handlers]Handling
Calls Handler.click, Handler.submit or Handler.command and gives
the reply, or Late when deadline passes first.
When to use
Use to deliver an interaction from an adapter. This is the one place the
deadline rule lives. Both adapters and
ChatTest call it, and a third-party adapter should.
Details
- A late click leaves its handler running, detached from the caller. When
it later returns
OpenModal, the modal is dropped andHandler.refusedis called with aChatErrorwhose reason isNotFound, opopenModal, detailinteraction_expired, platformevent.thread.platform. - A late submit or command interrupts its handler.
Lateis given at the deadline, without waiting for the handler's finalizers to finish. A command handler that cannot answer in time returnsdeferfirst. deadlineis the handler's share of the platform's 3 seconds, not the 3 seconds. The adapter subtracts what its own answer costs. This function knows no platform.
Gotchas
A late click's handler keeps running only if the runtime lets it. One that
stops work once the response is sent (Cloudflare Workers without
waitUntil) kills it.
Signature
declare const handle: { (event: { readonly _tag: tag<Tag>; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value?: string; } | { readonly _tag: tag<Tag>; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: string; }, deadline: Duration): Effect<{ readonly _tag: tag<Tag>; } | ClickReply, never, Handler>; (event: { readonly _tag: tag<Tag>; readonly id: string; readonly messageId?: string; readonly metadata?: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly values: { [key: string]: string; }; }, deadline: Duration): Effect<{ readonly _tag: tag<Tag>; } | SubmitReply, never, Handler>; (event: { readonly _tag: tag<Tag>; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; }, deadline: Duration): Effect<{ readonly _tag: tag<Tag>; } | CommandReply, never, Handler>; (event: { readonly _tag: tag<Tag>; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; } | { readonly _tag: tag<Tag>; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value?: string; } | { readonly _tag: tag<Tag>; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: string; } | { readonly _tag: tag<Tag>; readonly id: string; readonly messageId?: string; readonly metadata?: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly values: { [key: string]: string; }; }, deadline: Duration): Effect<{ readonly _tag: tag<Tag>; readonly command: { readonly _tag: tag<Tag>; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; }; readonly visibility: "public" | "ephemeral"; } | { readonly _tag: tag<Tag>; } | { readonly _tag: tag<Tag>; readonly modal: { readonly _tag: tag<Tag>; readonly elements: readonly Array<{ readonly _tag: tag<Tag>; readonly id: string; readonly initial?: string; readonly label: string; readonly maxLength?: number; readonly minLength?: number; readonly multiline?: boolean; readonly optional?: boolean; readonly placeholder?: string; } | { readonly _tag: tag<Tag>; readonly id: string; readonly initial?: string; readonly label: string; readonly optional?: boolean; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; } | { readonly _tag: tag<Tag>; readonly text: string; }>; readonly id: string; readonly metadata?: string; readonly submitLabel?: string; readonly title: string; }; } | { readonly _tag: tag<Tag>; } | { readonly _tag: tag<Tag>; readonly errors: { [key: string]: string; }; } | { readonly _tag: tag<Tag>; readonly actions?: readonly Array<{ readonly _tag: tag<Tag>; readonly id: string; readonly label: string; readonly style?: "default" | "primary" | "danger"; readonly value?: string; } | { readonly _tag: tag<Tag>; readonly label: string; readonly url: string; } | { readonly _tag: tag<Tag>; readonly id: string; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; readonly selected?: string; }>; readonly markdown: string; readonly visibility: "public" | "ephemeral"; } | { readonly _tag: tag<Tag>; }, never, Handler>;}Layers
The Handler that routes by id to the registered definitions, and an
empty Registry. Group layers go on top with Layer.provideMerge.
Details
- An interaction with an unknown id, or with a value that no longer
decodes, is logged and answered
ackorclose.unhandledsees it first. It cannot change the answer. - A command with an unknown id is answered
ack, which the adapter sends nothing for, and logged at debug level: on Discord it may belong to another library in the same process. Handler.refusedis routed tounhandledtoo, asReplyRefused, and logged.
Signature
declare function layer<R = never>(options?: { readonly unhandled?: (event: { readonly _tag: "CommandInvoked"; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; } | { readonly _tag: "ButtonClicked"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value?: string; } | { readonly _tag: "SelectChanged"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: string; } | { readonly _tag: "ModalSubmitted"; readonly id: string; readonly messageId?: string; readonly metadata?: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly values: { [key: string]: string; }; }, why: Unhandled) => Effect<void, never, R>;}): Layer<Registry | Handler, never, R>Models
A click as a button's handler receives it.
Details
value is decoded with the button's value schema, and undefined when it
has none.
Signature
interface Click<out Value> { readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: Value;}A choice as a select's handler receives it. value is the chosen option,
decoded.
Signature
interface Selected<out Value> { readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: Value;}A submit as a modal's handler receives it, with the metadata and values decoded.
Details
valueshas one key per input and none fortext. An optional input left empty isundefined.messageIdis the message whose element opened the modal, andundefinedwhen a command opened it.
Signature
interface Submit<out Metadata, out Values> { readonly id: string; readonly messageId: string | undefined; readonly metadata: Metadata; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly values: Values;}Why an interaction reached no handler, or why its handler's reply was not carried out.
Details
UnknownId: no definition has the id, or it is a definition of another kind.InvalidValue: the value or metadata no longer decodes, or a required input is missing.ReplyRefused:Handler.refusedwas called, with the adapter's error.
Signature
type Unhandled = { readonly reason: "UnknownId" | "InvalidValue";} | { readonly error: ChatError; readonly reason: "ReplyRefused";}A value's schema.
Details
It may not need services, because it is encoded inside of and open,
which are plain functions.
Signature
type ValueSchema = Schema.Codec<any, any, never, never>Other
Replies
Acknowledges a click.
Signature
declare const ack: AckSchema for the reply that acknowledges a click. It is also what the handler service answers a command with no definition, which the adapter sends nothing for.
Signature
declare const Ack: TaggedStruct<"Ack", {}>The reply that acknowledges a click.
Signature
type Ack = typeof Ack.TypeWhat a click's handler may answer.
Signature
type ClickReply = Ack | OpenModalCloses a submitted modal.
Signature
declare const close: CloseSchema for the reply that closes a submitted modal.
Signature
declare const Close: TaggedStruct<"Close", {}>The reply that closes a submitted modal.
Signature
type Close = typeof Close.TypeWhat the handler service may answer a command with.
Details
Ack is what a command with no definition gets, and the adapter sends
nothing for it. A typed command handler cannot return it.
Signature
type CommandReply = Ack | Respond | OpenModal | DeferAnswers a command with a promise of a later answer, sent with
Chat.followUp.
When to use
Use when the work does not fit in the deadline. The handler returns the deferral and hands it to whatever does the work.
Details
visibilitydefaults to"ephemeral", and holds for the follow-up.- One
Chat.followUpper deferral is the portable contract. The first call is the answer. A second is platform-specific: a new message on Slack, up to five, and an overwrite of the first on Discord.
Signature
declare function defer(command: { readonly _tag: "CommandInvoked"; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string;}, options?: { readonly visibility?: "public" | "ephemeral";}): { readonly _tag: "Defer"; readonly command: { readonly _tag: "CommandInvoked"; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; }; readonly visibility: "public" | "ephemeral";}Schema for the reply that says a command's answer comes later, and the
value Chat.followUp takes to send it.
Details
- It is plain data, so it can go on a queue to another process.
visibilityis fixed here.followUphas none of its own.- The first
followUpfor it is the answer. A second one is platform-specific: Slack posts another message, up to five, and Discord overwrites the first. Neither adapter stops it.
Gotchas
It holds the command's raw, which is a credential for as long as
limits.followUpAge.
Signature
declare const Defer: TaggedStruct<"Defer", { readonly command: TaggedStruct<"CommandInvoked", { readonly id: String; readonly raw: Codec<Json, Json, never, never>; readonly text: String; readonly thread: Struct<{ readonly id: String; readonly platform: String; }>; readonly userId: String; }>; readonly visibility: Literals<readonly ["ephemeral", "public"]>;}>The reply that says a command's answer comes later.
Signature
type Defer = typeof Defer.TypePuts a message under each named field of a submitted modal.
Details
In a typed submit handler, only the modal's input ids are accepted.
Signature
declare function errors<E extends { [field: string]: string;}>(errors: E): FieldErrors<keyof E & string>Schema for the reply that puts a message under each named field of a
submitted modal. errors is by field id.
Details
With modalErrors the modal stays open with the messages under its
fields. Without it the adapter answers some other way: on Discord a private
reply lists them and the modal closes.
Signature
declare const Errors: TaggedStruct<"Errors", { readonly errors: $Record<String, String>;}>The reply that puts a message under each named field.
Signature
type Errors = typeof Errors.TypeErrors, with the field ids it names kept in the type, so a typed submit
handler can refuse a field the modal does not have. The extra property
exists only in the type.
Signature
interface FieldErrors<out Field extends string> extends Errors { readonly "~fields"?: Field;}Schema for what handle gives when the handler ran past the deadline.
Gotchas
Late is never sent to a platform. It is a tagged struct, so it is itself
a JSON value: test for it with Schema.is(ChatInteraction.Late).
Signature
declare const Late: TaggedStruct<"Late", {}>What handle gives when the handler ran past the deadline.
Signature
type Late = typeof Late.TypeSchema for the reply that opens a modal. A click or a command can return it.
Signature
declare const OpenModal: TaggedStruct<"OpenModal", { readonly modal: TaggedStruct<"Modal", { readonly elements: $Array<Union<readonly [TaggedStruct<"TextInput", { readonly id: String; readonly initial: optionalKey<String>; readonly label: String; readonly maxLength: optionalKey<Int>; readonly minLength: optionalKey<Int>; readonly multiline: optionalKey<Boolean>; readonly optional: optionalKey<Boolean>; readonly placeholder: optionalKey<String>; }>, TaggedStruct<"SelectInput", { readonly id: String; readonly initial: optionalKey<String>; readonly label: String; readonly optional: optionalKey<Boolean>; readonly options: $Array<Struct<...>>; readonly placeholder: optionalKey<String>; }>, TaggedStruct<"Text", { readonly text: String; }>]>>; readonly id: String; readonly metadata: optionalKey<String>; readonly submitLabel: optionalKey<String>; readonly title: String; }>;}>The reply that opens a modal.
Signature
type OpenModal = typeof OpenModal.TypeAnswers a command with a message, at once.
Details
visibility defaults to "ephemeral": only the person who ran the
command sees it. A platform without ephemeral refuses a private answer
and sends nothing. It never sends it publicly instead.
Signature
declare function respond(options: { readonly actions?: readonly Array<{ readonly _tag: "Button"; readonly id: string; readonly label: string; readonly style?: "default" | "primary" | "danger"; readonly value?: string; } | { readonly _tag: "LinkButton"; readonly label: string; readonly url: string; } | { readonly _tag: "Select"; readonly id: string; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; readonly selected?: string; }>; readonly markdown: string; readonly visibility?: "public" | "ephemeral";}): { readonly _tag: "Respond"; readonly actions?: readonly Array<{ readonly _tag: "Button"; readonly id: string; readonly label: string; readonly style?: "default" | "primary" | "danger"; readonly value?: string; } | { readonly _tag: "LinkButton"; readonly label: string; readonly url: string; } | { readonly _tag: "Select"; readonly id: string; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; readonly selected?: string; }>; readonly markdown: string; readonly visibility: "public" | "ephemeral";}Schema for the reply that answers a command with a message, at once.
Details
markdown and actions are rendered and checked the way post renders
and checks them, with the default post options. withPostOptions does
not reach it, so it never mentions anyone: on Slack a mention token is
escaped and shows as text, and on Discord it renders and notifies nobody.
The message gets no id: it cannot be edited, deleted or fetched
afterwards. A click on its elements carries a messageId, which for a
private answer tells which answer it was and works with no message op.
Signature
declare const Respond: TaggedStruct<"Respond", { readonly actions: optionalKey<$Array<Union<readonly [TaggedStruct<"Button", { readonly id: String; readonly label: String; readonly style: optionalKey<Literals<readonly [..., ..., ...]>>; readonly value: optionalKey<String>; }>, TaggedStruct<"LinkButton", { readonly label: String; readonly url: String; }>, TaggedStruct<"Select", { readonly id: String; readonly options: $Array<Struct<{ readonly label: ...; readonly value: ...; }>>; readonly placeholder: optionalKey<String>; readonly selected: optionalKey<String>; }>]>>>; readonly markdown: String; readonly visibility: Literals<readonly ["ephemeral", "public"]>;}>The reply that answers a command with a message.
Signature
type Respond = typeof Respond.TypeWhat a submit's handler may answer.
Signature
type SubmitReply = Close | ErrorsVisibility
unstableSchema for who sees the answer to a command: "ephemeral" for the person
who ran it, "public" for the channel.
Signature
declare const Visibility: Literals<readonly ["ephemeral", "public"]>Who sees the answer to a command.
Signature
type Visibility = typeof Visibility.TypeServices
What an adapter calls with an interaction. Public, for a consumer that routes clicks itself.
Details
- The contract is to answer inside the platform's 3 seconds. There is no error channel: a handler that cannot do its work still has to answer.
refusedis called when a click's handler returnedOpenModaland no modal opened, after the click was acked: by the adapter (the platform has nomodals, the modal's id plus metadata is overlimits.modalMetadataLength, it has more thanlimits.modalFieldsfields, a title or label is too long, or the platform refused the call) or byhandle(the reply came after the deadline).- A command's reply can be refused too: a private answer without
ephemeral, an element the platform lacks, an answer overlimits.messageLengthor an element limit, a modal that cannot open, or a refusal by the platform. A submit's replies always have an answer the adapter can give. commandanswersAckfor a command it has no definition for, and the adapter sends nothing for that. On Discord another library in the same process can still answer it.
Gotchas
Neither platform redelivers a click or a submit, so there is no dedupe key. A submit can still arrive twice for one modal: a handler that runs past the deadline is interrupted, the platform shows its own error, the modal stays open and the user can submit again. Nothing marks the second one as a repeat, and the first run may have been cut off part way.
Signature
declare class Handler extends Shape<"effect/chat/ChatInteraction/Handler", { readonly click: (event: { readonly _tag: "ButtonClicked"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value?: string; } | { readonly _tag: "SelectChanged"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: string; }) => Effect<ClickReply>; readonly command: (event: { readonly _tag: "CommandInvoked"; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; }) => Effect<CommandReply>; readonly refused: (event: { readonly _tag: "CommandInvoked"; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; } | { readonly _tag: "ButtonClicked"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value?: string; } | { readonly _tag: "SelectChanged"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: string; }, error: ChatError) => Effect<void>; readonly submit: (event: { readonly _tag: "ModalSubmitted"; readonly id: string; readonly messageId?: string; readonly metadata?: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly values: { [key: string]: string; }; }) => Effect<SubmitReply>;}, this> { constructor(_: never);}One registered definition with its handler, services already provided.
Details
handle answers ack or close without calling the handler when the
event does not match the definition or its value no longer decodes. A
command that does not match is answered ack.
Signature
interface Registered { readonly definition: Definition; readonly handle: (event: { readonly _tag: "CommandInvoked"; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; } | { readonly _tag: "ButtonClicked"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value?: string; } | { readonly _tag: "SelectChanged"; readonly id: string; readonly messageId: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly value: string; } | { readonly _tag: "ModalSubmitted"; readonly id: string; readonly messageId?: string; readonly metadata?: string; readonly raw: Json; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; readonly values: { [key: string]: string; }; }) => Effect<{ readonly _tag: "Defer"; readonly command: { readonly _tag: "CommandInvoked"; readonly id: string; readonly raw: Json; readonly text: string; readonly thread: { readonly id: string; readonly platform: string; }; readonly userId: string; }; readonly visibility: "public" | "ephemeral"; } | { readonly _tag: "Ack"; } | { readonly _tag: "OpenModal"; readonly modal: { readonly _tag: "Modal"; readonly elements: readonly Array<{ readonly _tag: "TextInput"; readonly id: string; readonly initial?: string; readonly label: string; readonly maxLength?: number; readonly minLength?: number; readonly multiline?: boolean; readonly optional?: boolean; readonly placeholder?: string; } | { readonly _tag: "SelectInput"; readonly id: string; readonly initial?: string; readonly label: string; readonly optional?: boolean; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; } | { readonly _tag: "Text"; readonly text: string; }>; readonly id: string; readonly metadata?: string; readonly submitLabel?: string; readonly title: string; }; } | { readonly _tag: "Close"; } | { readonly _tag: "Errors"; readonly errors: { [key: string]: string; }; } | { readonly _tag: "Respond"; readonly actions?: readonly Array<{ readonly _tag: "Button"; readonly id: string; readonly label: string; readonly style?: "default" | "primary" | "danger"; readonly value?: string; } | { readonly _tag: "LinkButton"; readonly label: string; readonly url: string; } | { readonly _tag: "Select"; readonly id: string; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; readonly selected?: string; }>; readonly markdown: string; readonly visibility: "public" | "ephemeral"; }>;}Where groups put their handlers. ChatInteraction.layer provides it.
Details
register dies when an id is already registered.
Signature
declare class Registry extends Shape<"effect/chat/ChatInteraction/Registry", { readonly get: (id: string) => Effect<Registered | undefined>; readonly register: (entries: readonly Array<Registered>) => Effect<void>;}, this> { constructor(_: never);}Type Ids
Values
decodeValue
unstableDecodes the string an element carried, as encodeValue wrote it.
Details
Gives none for a string that no longer decodes, which is what a value posted under an older schema can do.
Signature
declare function decodeValue<S extends ValueSchema>(schema: S, encoded: string): Option<S["Type"]>encodeValue
unstableEncodes a value into the string an element carries.
When to use
Use when routing clicks yourself. of and open already run it.
Details
The value goes through Schema.toCodecJson. The form on the wire is decided
once per schema, from the codec's encoded side, never per value:
- when the encoded side can only be a string (
String, a brand of it, string literals, a template literal, or a union of those), the string is sent as it is - otherwise the encoded value goes through
JSON.stringify, and back throughJSON.parse
Gotchas
Throws when the value has no JSON form, as Schema.encodeSync does.
Signature
declare function encodeValue<S extends ValueSchema>(schema: S, value: S["Type"]): string