UNPKG

slack-edge

Version:

Slack app development framework for edge functions with streamlined TypeScript support

327 lines 14.6 kB
import { SlackAppEnv, SlackEdgeAppEnv, SlackSocketModeAppEnv } from "./app-env"; import { BlockActionAckHandler, BlockActionLazyHandler, BlockSuggestionAckHandler, EventLazyHandler, GlobalShortcutAckHandler, GlobalShortcutLazyHandler, MessageEventLazyHandler, MessageShortcutAckHandler, MessageShortcutLazyHandler, ShortcutAckHandler, ShortcutLazyHandler, SlashCommandAckHandler, SlashCommandLazyHandler, SourceSpecifiedBlockActionAckHandler, SourceSpecifiedBlockActionLazyHandler, ViewAckHandler, ViewClosedAckHandler, ViewClosedLazyHandler, ViewLazyHandler, ViewSubmissionAckHandler, ViewSubmissionLazyHandler } from "./handler/handler"; import { SlackRequest, SlackRequestWithChannelId } from "./request/request"; import { AnySlackEvent, AnySlackEventWithChannelId } from "./request/payload/event"; import { AnyEventType, SlackAPIClient } from "slack-web-api-client"; import { PreAuthorizeMiddleware, Middleware } from "./middleware/middleware"; import { Authorize } from "./authorization/authorize"; import { BlockAction, BlockElementActions, BlockElementTypes } from "./request/payload/block-action"; import { ExecutionContext } from "./execution-context"; import { SocketModeClient } from "./socket-mode/socket-mode-client"; import { Assistant, AssistantBotMessageHandler, AssistantThreadContextChangedHandler, AssistantThreadStartedHandler, AssistantUserMessageHandler } from "./assistant/assistant"; import { AssistantThreadContextStore } from "./assistant/thread-context-store"; import { AuthorizeErrorHandler } from "./authorization/authorize-error-handler"; /** * Options for initializing SlackApp instance. */ export interface SlackAppOptions<E extends SlackEdgeAppEnv | SlackSocketModeAppEnv> { /** * Passed env variables for configuring the app. */ env: E; /** * The function that resolves the OAuth token associated with an incoming request. */ authorize?: Authorize<E>; /** * The hoook that handles authorization failure. */ authorizeErrorHandler?: AuthorizeErrorHandler<E>; /** * The endpoint routes to handle requests from Slack's API server. * When this app connects to Slack through Socket Mode, this setting won't be used. */ routes?: { events: string; }; /** * True when Socket Mode is enabled. */ socketMode?: boolean; /** * When this is set to true, all lazy listeners are invoked after the ack function completion. * The default is set to false. */ startLazyListenerAfterAck?: boolean; /** * When this is set to false, the built-in ignoringSelfEvents middleware is disabled. * The default is set to true. */ ignoreSelfEvents?: boolean; /** * When this is set to false, the built-in ignoringSelfEvents middleware does not block this app's assistant bot message event. * The default is set to true. */ ignoreSelfAssistantMessageEvents?: boolean; /** * Your custom assistant thread context store implementation. */ assistantThreadContextStore?: AssistantThreadContextStore; } /** * The class representing a Slack app process that handles requests from Slack's API server. */ export declare class SlackApp<E extends SlackEdgeAppEnv | SlackSocketModeAppEnv> { #private; /** * Passed env variables for configuring the app. */ env: E; /** * The singleton SlackAPIClient instance that embodies a bot token. * SlackOAuthApp initializes this property without a token. */ client: SlackAPIClient; /** * The function that resolves the OAuth token associated with an incoming request. */ authorize: Authorize<E>; /** * The hoook that handles authorization failure. */ authorizeErrorHandler: AuthorizeErrorHandler<E>; /** * The endpoint routes to handle requests from Slack's API server. * When this app connects to Slack through Socket Mode, this setting won't be used. */ routes: { events: string | undefined; }; /** * The credential used for verifying a request's signature. */ signingSecret: string; /** * The app-level token for Socket Mode. * When Socket Mode is not enabled, this can be undefined. */ appLevelToken: string | undefined; /** * True when Socket Mode is enabled. */ socketMode: boolean; /** * The underlying Socket Mode client * that manages WebSocket connections and dispatches messages from Slack. */ socketModeClient: SocketModeClient | undefined; /** * When this is set to true, all lazy listeners are invoked after the ack function completion. * The default is set to false. */ startLazyListenerAfterAck: boolean; /** * When this is set to false, the built-in ignoringSelfEvents middleware is disabled. * The default is set to true. */ ignoreSelfEvents: boolean; /** * The custom middleware that are called before authorize() function. */ preAuthorizeMiddleware: PreAuthorizeMiddleware<any>[]; /** * The custom middleware that are called after authorize() function. */ postAuthorizeMiddleware: Middleware<any>[]; eventsToSkipAuthorize: string[]; /** * Your custom assistant thread context store implementation. */ assistantThreadContextStore?: AssistantThreadContextStore; constructor(options: SlackAppOptions<E>); /** * Registers a pre-authorize middleware. * @param middleware middleware * @returns this instance */ beforeAuthorize(middleware: PreAuthorizeMiddleware<E>): SlackApp<E>; /** * Registers a post-authorize middleware. This naming is for consistency with bolt-js. * @param middleware middleware * @returns this instance */ middleware(middleware: Middleware<E>): SlackApp<E>; /** * Registers a post-authorize middleware. This naming is for consistency with bolt-js. * @param middleware middleware * @returns this instance */ use(middleware: Middleware<E>): SlackApp<E>; /** * Registers a post-authorize middleware. * @param middleware middleware * @returns this instance */ afterAuthorize(middleware: Middleware<E>): SlackApp<E>; /** * Registers a listener that handles slash command executions. * @param pattern the pattern to match slash command name * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ command(pattern: StringOrRegExp, ack: SlashCommandAckHandler<E>, lazy?: SlashCommandLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles custom function calls within Workflow Builder. * Please be aware that this feature is still in beta as of April 2024. * @param callbackId the pattern to match callback_id in a payload * @param lazy lazy function that can do anything asynchronously * @returns this instance */ function(callbackId: FunctionExecutedEventCallbackIdPattern, lazy: EventLazyHandler<"function_executed", E>): this; /** * Registers a listener that handles Events API request. * @param event the pattern to match event type in a payload * @param lazy lazy function that can do anything asynchronously * @returns this instance */ event<Type extends AnyEventType>(event: Type, lazy: EventLazyHandler<Type, E>): SlackApp<E>; assistant(assistant: Assistant<E>): SlackApp<E>; assistantThreadStarted(lazy: AssistantThreadStartedHandler<E>): SlackApp<E>; assistantThreadContextChanged(lazy: AssistantThreadContextChangedHandler<E>): SlackApp<E>; assistantUserMessage(lazy: AssistantUserMessageHandler<E>): SlackApp<E>; assistantBotMessage(lazy: AssistantBotMessageHandler<E>): SlackApp<E>; /** * Registers a listener that handles all newly posted message events. * @param lazy lazy function that can do anything asynchronously * @returns this instance */ anyMessage(lazy: MessageEventLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles newly posted message events that matches the pattern. * @param pattern the pattern to match a message event's text * @param lazy lazy function that can do anything asynchronously * @returns this instance */ message(pattern: MessageEventPattern, lazy: MessageEventLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles global/message shortcut executions. * @param callbackId the pattern to match callback_id in a payload * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ shortcut(callbackId: StringOrRegExp, ack: ShortcutAckHandler<E>, lazy?: ShortcutLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles global shortcut executions. * @param callbackId the pattern to match callback_id in a payload * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ globalShortcut(callbackId: StringOrRegExp, ack: GlobalShortcutAckHandler<E>, lazy?: GlobalShortcutLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles message shortcut executions. * @param callbackId the pattern to match callback_id in a payload * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ messageShortcut(callbackId: StringOrRegExp, ack: MessageShortcutAckHandler<E>, lazy?: MessageShortcutLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles type: "block_actions" requests. * @param constraints the constraints to match block_id/action_id in a payload * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ action<T extends BlockElementTypes, A extends BlockAction<Extract<BlockElementActions, { type: T; }>> = BlockAction<Extract<BlockElementActions, { type: T; }>>>(constraints: StringOrRegExp | { type: T; block_id?: string; action_id: string; }, ack: BlockActionAckHandler<T, E, A> | SourceSpecifiedBlockActionAckHandler<E, A>, lazy?: BlockActionLazyHandler<T, E, A> | SourceSpecifiedBlockActionLazyHandler<E, A>): SlackApp<E>; /** * Registers a listener that handles type: "block_suggestion" requests. * Note that your app must return the options/option_groups within 3 seconds, * so slack-edge intentionally does not accept lazy here. * @param constraints the constraints to match block_id/action_id in a payload * @param ack ack function that must complete within 3 seconds * @returns this instance */ options(constraints: StringOrRegExp | { block_id?: string; action_id: string; }, ack: BlockSuggestionAckHandler<E>): SlackApp<E>; /** * Registers a listener that handles type: "view_submission"/"view_closed" requests. * @param callbackId the constraints to match callback_id in a payload * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ view(callbackId: StringOrRegExp, ack: ViewAckHandler<E>, lazy?: ViewLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles type: "view_submission" requests. * @param callbackId the constraints to match callback_id in a payload * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ viewSubmission(callbackId: StringOrRegExp, ack: ViewSubmissionAckHandler<E>, lazy?: ViewSubmissionLazyHandler<E>): SlackApp<E>; /** * Registers a listener that handles type: "view_closed" requests. * @param callbackId the constraints to match callback_id in a payload * @param ack ack function that must complete within 3 seconds * @param lazy lazy function that can do anything asynchronously * @returns this instance */ viewClosed(callbackId: StringOrRegExp, ack: ViewClosedAckHandler<E>, lazy?: ViewClosedLazyHandler<E>): SlackApp<E>; /** * Handles an http request and returns a response to it. * @param request request * @param ctx execution context * @returns response */ run(request: Request, ctx?: ExecutionContext): Promise<Response>; /** * Establishes a WebSocket connection for Socket Mode. */ connect(): Promise<void>; /** * Disconnect a WebSocket connection for Socket Mode. */ disconnect(): Promise<void>; /** * Handles an HTTP request from Slack's API server and returns a response to it. * @param request request * @param ctx execution context * @returns response */ handleEventRequest(request: Request, ctx: ExecutionContext): Promise<Response>; } export type StringOrRegExp = string | RegExp; export type MessageEventPattern = string | RegExp | undefined; /** * Events API request */ export type EventRequest<E extends SlackAppEnv, T> = Extract<AnySlackEventWithChannelId, { type: T; }> extends never ? SlackRequest<E, Extract<AnySlackEvent, { type: T; }>> : SlackRequestWithChannelId<E, Extract<AnySlackEventWithChannelId, { type: T; }>>; /** * Events API: "function_executed" event for custom functions in Workflow Builder */ export type FunctionExecutedEventCallbackIdPattern = string | RegExp | undefined; /** * Events API: "message" event */ export type MessageEventRequest<E extends SlackAppEnv, ST extends string | undefined> = SlackRequestWithChannelId<E, Extract<AnySlackEventWithChannelId, { subtype: ST; }>>; export type MessageEventSubtypes = undefined | "bot_message" | "thread_broadcast" | "file_share"; /** * Handler function for "message" events */ export type MessageEventHandler<E extends SlackAppEnv> = (req: MessageEventRequest<E, MessageEventSubtypes>) => Promise<void>; /** * Singleton lazy listener that does not do anything */ export declare const noopLazyHandler: () => Promise<void>; //# sourceMappingURL=app.d.ts.map