Skip to content
// EFFECT DAYS 2026

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 to Handler, 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()))
78 exports Since v4.1.5 Source

Definitions

button

unstable
Since v4.1.5

Defines a button.

Details

  • The id names the handler in a group. It is sent to the platform as is, and counts toward limits.actionDataLength with the encoded value.
  • With no value schema the button carries no value, and its click's value is undefined.

Signature

declare function button<Id extends string, Value extends ValueSchema | undefined = undefined>(id: Id, options: {
readonly label: string;
readonly style?: "default" | "primary" | "danger";
readonly value?: Value;
}): Button<Id, Value>

Button

interface unstable
Since v4.1.5

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;
}

command

unstable
Since v4.1.5

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 STRING option named argument.name, and a command with no argument has no options.
  • argument.required defaults to false. Discord's client enforces it. Slack cannot, so a portable handler still checks for an empty text.

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>

Command

interface unstable
Since v4.1.5

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";
}

Definition

type unstable
Since v4.1.5

Any definition that has a handler.

Signature

type Definition = Button<string, any> | Select<string, any> | Modal<string, any, any> | Command<string>

select

unstable
Since v4.1.5

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>

Select

interface unstable
Since v4.1.5

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

Since v4.1.5

Schema for a click on a button.

Details

  • value is still encoded, and absent for a button with no value.
  • thread and messageId are where the message carrying the button lives. raw is 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>;
}>

ButtonClicked

type unstable
Since v4.1.5

A click on a button.

Signature

type ButtonClicked = typeof ButtonClicked.Type
Since v4.1.5

Schema for a slash command.

Details

  • id is the command's name, without the slash.
  • text is what was typed after the name, as the platform sent it, and empty when nothing was. It is not normalised.
  • thread is 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 no messageId, since a command comes from no message.
  • raw is the platform's payload. It holds what Chat.followUp needs, so it is a credential for as long as limits.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;
}>

CommandInvoked

type unstable
Since v4.1.5

A slash command.

Signature

type CommandInvoked = typeof CommandInvoked.Type

Interaction

unstable
Since v4.1.5

Schema 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;
}>]>

Interaction

type unstable
Since v4.1.5

A click, a select choice, a modal submit or a slash command.

Signature

type Interaction = typeof Interaction.Type
Since v4.1.5

Schema for a modal submit.

Details

  • thread and messageId are those of the click that opened the modal. A modal a command opened carries the command's thread and no messageId. A modal opened from a command's private answer carries that answer's id, which no message op works with, as on ButtonClicked.
  • metadata is still encoded.
  • values are 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>;
}>

ModalSubmitted

type unstable
Since v4.1.5

A modal submit.

Signature

type ModalSubmitted = typeof ModalSubmitted.Type
Since v4.1.5

Schema 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;
}>

SelectChanged

type unstable
Since v4.1.5

A choice in a select on a message.

Signature

type SelectChanged = typeof SelectChanged.Type

Fields

choice

unstable
Since v4.1.5

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>>

Choice

interface unstable
Since v4.1.5

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;
}

Field

type unstable
Since v4.1.5

Any field of a modal.

Signature

type Field = TextInput<boolean> | Choice<string, boolean> | SelectInput<any, boolean> | Text

Fields

interface unstable
Since v4.1.5

A modal's fields by id, shown in key order.

Signature

interface Fields {
[id: string]: Field;
}

FieldValue

type unstable
Since v4.1.5

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> : never

OpenOptions

type unstable
Since v4.1.5

What 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

unstable
Since v4.1.5

Defines 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>>

SelectInput

interface unstable
Since v4.1.5

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;
}

SelectOptions

type unstable
Since v4.1.5

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 }

text

unstable
Since v4.1.5

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): Text

Text

interface unstable
Since v4.1.5

Plain text between a modal's inputs.

Signature

interface Text {
readonly "~effect/chat/ChatInteraction": "~effect/chat/ChatInteraction";
readonly kind: "text";
readonly text: string;
}

textInput

unstable
Since v4.1.5

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>>

TextInput

interface unstable
Since v4.1.5

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;
}

Values

type unstable
Since v4.1.5

submit.values: one key per input, none for text.

Signature

type Values<Fs extends Fields> = { [K in keyof Fs]: FieldValue<Fs[K]> }

Groups

group

unstable
Since v4.1.5

Groups definitions so their handlers can be registered together.

Signature

declare function group<Definitions extends readonly Array<Definition>>(...definitions: Definitions): Group<Definitions[number]>

Group

interface unstable
Since v4.1.5

A set of definitions whose handlers are registered together.

Details

  • toLayer registers the handlers, with their services provided. It needs the Registry that ChatInteraction.layer provides.
  • of is the identity, for handlers written away from toLayer.

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>>;
}

HandlerFor

type unstable
Since v4.1.5

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> : never

HandlersFrom

type unstable
Since v4.1.5

One handler per id in the group.

Signature

type HandlersFrom<D extends Definition> = { [Current in D]: HandlerFor<Current> }

HandlersServices

type unstable
Since v4.1.5

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

handle

unstable
Since v4.1.5

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 and Handler.refused is called with a ChatError whose reason is NotFound, op openModal, detail interaction_expired, platform event.thread.platform.
  • A late submit or command interrupts its handler. Late is given at the deadline, without waiting for the handler's finalizers to finish. A command handler that cannot answer in time returns defer first.
  • deadline is 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

layer

unstable
Since v4.1.5

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 ack or close. unhandled sees 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.refused is routed to unhandled too, as ReplyRefused, 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

Click

interface unstable
Since v4.1.5

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;
}

Selected

interface unstable
Since v4.1.5

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;
}

Submit

interface unstable
Since v4.1.5

A submit as a modal's handler receives it, with the metadata and values decoded.

Details

  • values has one key per input and none for text. An optional input left empty is undefined.
  • messageId is the message whose element opened the modal, and undefined when 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;
}

Unhandled

type unstable
Since v4.1.5

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.refused was called, with the adapter's error.

Signature

type Unhandled = {
readonly reason: "UnknownId" | "InvalidValue";
} | {
readonly error: ChatError;
readonly reason: "ReplyRefused";
}

ValueSchema

type unstable
Since v4.1.5

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

Button

unstable
Since v4.1.5

Types that go with a button definition.

Group

unstable
Since v4.1.5

Types that go with a group.

Replies

ack

unstable
Since v4.1.5

Acknowledges a click.

Signature

declare const ack: Ack

Ack

unstable
Since v4.1.5

Schema 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", {}>

Ack

type unstable
Since v4.1.5

The reply that acknowledges a click.

Signature

type Ack = typeof Ack.Type

ClickReply

type unstable
Since v4.1.5

What a click's handler may answer.

Signature

type ClickReply = Ack | OpenModal

close

unstable
Since v4.1.5

Closes a submitted modal.

Signature

declare const close: Close

Close

unstable
Since v4.1.5

Schema for the reply that closes a submitted modal.

Signature

declare const Close: TaggedStruct<"Close", {}>

Close

type unstable
Since v4.1.5

The reply that closes a submitted modal.

Signature

type Close = typeof Close.Type

CommandReply

type unstable
Since v4.1.5

What 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 | Defer

defer

unstable
Since v4.1.5

Answers 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

  • visibility defaults to "ephemeral", and holds for the follow-up.
  • One Chat.followUp per 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";
}

Defer

unstable
Since v4.1.5

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.
  • visibility is fixed here. followUp has none of its own.
  • The first followUp for 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"]>;
}>

Defer

type unstable
Since v4.1.5

The reply that says a command's answer comes later.

Signature

type Defer = typeof Defer.Type

errors

unstable
Since v4.1.5

Puts 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>

Errors

unstable
Since v4.1.5

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>;
}>

Errors

type unstable
Since v4.1.5

The reply that puts a message under each named field.

Signature

type Errors = typeof Errors.Type

FieldErrors

interface unstable
Since v4.1.5

Errors, 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;
}

Late

unstable
Since v4.1.5

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", {}>

Late

type unstable
Since v4.1.5

What handle gives when the handler ran past the deadline.

Signature

type Late = typeof Late.Type

OpenModal

unstable
Since v4.1.5

Schema 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;
}>;
}>

OpenModal

type unstable
Since v4.1.5

The reply that opens a modal.

Signature

type OpenModal = typeof OpenModal.Type

respond

unstable
Since v4.1.5

Answers 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";
}

Respond

unstable
Since v4.1.5

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"]>;
}>

Respond

type unstable
Since v4.1.5

The reply that answers a command with a message.

Signature

type Respond = typeof Respond.Type

SubmitReply

type unstable
Since v4.1.5

What a submit's handler may answer.

Signature

type SubmitReply = Close | Errors

Visibility

unstable
Since v4.1.5

Schema 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"]>

Visibility

type unstable
Since v4.1.5

Who sees the answer to a command.

Signature

type Visibility = typeof Visibility.Type

Services

Handler

unstable
Since v4.1.5

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.
  • refused is called when a click's handler returned OpenModal and no modal opened, after the click was acked: by the adapter (the platform has no modals, the modal's id plus metadata is over limits.modalMetadataLength, it has more than limits.modalFields fields, a title or label is too long, or the platform refused the call) or by handle (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 over limits.messageLength or 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.
  • command answers Ack for 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);
}

Registered

interface unstable
Since v4.1.5

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";
}>;
}

Registry

unstable
Since v4.1.5

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

TypeId

unstable
Since v4.1.5

The type id of definitions and groups.

Signature

declare const TypeId: "~effect/chat/ChatInteraction"

Values

decodeValue

unstable
Since v4.1.5

Decodes 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

unstable
Since v4.1.5

Encodes 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 through JSON.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