@microfox/webhook-slack
Version:
A TypeScript package for sending webhooks to Slack.
185 lines (181 loc) • 7.2 kB
TypeScript
import { Webhook, WebhookRequest, WebhookResponse } from '@microfox/webhook-core';
import { Context, StringIndexed as StringIndexed$1, SayFn as SayFn$1, EnvelopedEvent as EnvelopedEvent$1, SlashCommand as SlashCommand$1, AckFn as AckFn$1, RespondArguments as RespondArguments$1, RespondFn as RespondFn$1, BlockElementAction, InteractiveAction, SayArguments as SayArguments$1, SlackAction, DialogSubmitAction, DialogValidation, SlackShortcut, KnownEventFromType as KnownEventFromType$1 } from '@slack/bolt';
import { SlackEvent, FunctionExecutedEvent, AllMessageEvents } from '@slack/types';
export * from '@slack/types';
import { ChatPostMessageArguments, ChatPostMessageResponse } from '@slack/web-api';
/**
/** Using type parameter T (generic), can distribute the Omit over a union set. */
type DistributiveOmit<T, K extends PropertyKey> = T extends any ? Omit<T, K> : never;
/**
* Extend this interface to build a type that is treated as an open set of properties, where each key is a string.
*/
type StringIndexed = Record<string, any>;
type SayArguments = DistributiveOmit<ChatPostMessageArguments, 'channel'> & {
channel?: string;
};
type SayFn = (message: string | SayArguments) => Promise<ChatPostMessageResponse>;
type RespondArguments = DistributiveOmit<ChatPostMessageArguments, 'channel' | 'text'> & {
/** Response URLs can be used to send ephemeral messages or in-channel messages using this argument */
response_type?: 'in_channel' | 'ephemeral';
replace_original?: boolean;
delete_original?: boolean;
text?: string;
};
type RespondFn = (message: string | RespondArguments) => Promise<any>;
type AckFn<Response> = (response?: Response) => Promise<void>;
/**
* Arguments which listeners and middleware receive to process an event from Slack's Events API.
*/
type SlackEventMiddlewareArgs<EventType extends string = string> = {
payload: EventFromType<EventType>;
event: EventFromType<EventType>;
body: EnvelopedEvent<EventFromType<EventType>>;
} & (EventType extends 'message' ? {
message: EventFromType<EventType>;
} : unknown) & (EventFromType<EventType> extends {
channel: string;
} | {
item: {
channel: string;
};
} ? {
say: SayFn;
} : unknown) & (EventType extends 'function_executed' ? {
inputs: FunctionExecutedEvent['inputs'];
ack: AckFn<void>;
} : {
ack?: undefined;
});
interface BaseSlackEvent<T extends string = string> {
type: T;
}
type EventTypePattern = string | RegExp;
type FunctionInputs = Record<string, unknown>;
/**
* A Slack Events API event wrapped in the standard envelope.
*
* This describes the entire JSON-encoded body of a request from Slack's Events API.
*/
interface EnvelopedEvent<Event = BaseSlackEvent> extends StringIndexed {
token: string;
team_id: string;
enterprise_id?: string;
api_app_id: string;
event: Event;
type: 'event_callback';
event_id: string;
event_time: number;
is_ext_shared_channel?: boolean;
authorizations?: Authorization[];
}
interface Authorization {
enterprise_id: string | null;
team_id: string | null;
user_id: string;
is_bot: boolean;
is_enterprise_install?: boolean;
}
/**
* Type function which given a string `T` returns a type for the matching Slack event(s).
*
* When the string matches known event(s) from the `SlackEvent` union, only those types are returned (also as a union).
* Otherwise, the `BasicSlackEvent<T>` type is returned.
*/
type EventFromType<T extends string> = KnownEventFromType<T> extends never ? BaseSlackEvent<T> : KnownEventFromType<T>;
type KnownEventFromType<T extends string> = Extract<SlackEvent, {
type: T;
}>;
/**
* Arguments which listeners and middleware receive to process a slash command from Slack.
*/
interface SlackCommandMiddlewareArgs {
payload: SlashCommand;
command: this['payload'];
body: this['payload'];
say: SayFn;
respond: RespondFn;
ack: AckFn<string | RespondArguments>;
}
/**
* A Slack slash command
*
* This describes the entire URL-encoded body of a request from Slack's slash commands.
*/
interface SlashCommand extends StringIndexed {
token: string;
command: string;
text: string;
response_url: string;
trigger_id: string;
user_id: string;
user_name: string;
team_id: string;
team_domain: string;
channel_id: string;
channel_name: string;
api_app_id: string;
enterprise_id?: string;
enterprise_name?: string;
is_enterprise_install?: string;
}
declare class WebhookParseError extends Error {
constructor(message: string);
}
interface MessageCallbackArgs {
message: AllMessageEvents;
context: Context & StringIndexed$1;
say: SayFn$1;
body: EnvelopedEvent$1<AllMessageEvents>;
}
/**
* SlackWebhook - A class for handling Slack webhooks
*/
declare class SlackWebhook extends Webhook {
private botToken?;
private callbacks;
constructor(options: {
secret: string;
botToken?: string;
});
sign(payload: string | Buffer): Promise<string>;
verify(payload: string | Buffer, signature: string, timestamp?: number): Promise<boolean>;
receive(request: WebhookRequest): Promise<WebhookResponse | void>;
on(event: string, listener: (...args: any[]) => void): this;
on(event: 'error', listener: (error: Error) => void): this;
onMessage(callback: (args: MessageCallbackArgs) => Promise<void>): void;
onCommand(command: string, callback: (args: {
command: SlashCommand$1;
context: Context & StringIndexed$1;
say: SayFn$1;
ack: AckFn$1<string | RespondArguments$1>;
body: SlashCommand$1;
respond: RespondFn$1;
}) => Promise<void>): void;
onAction(actionId: string, callback: (args: {
action: BlockElementAction | InteractiveAction;
context: Context & StringIndexed$1;
ack: AckFn$1<string | SayArguments$1> | AckFn$1<void>;
body: SlackAction;
respond: RespondFn$1;
}) => Promise<void>): void;
onDialogSubmission(dialogCallbackId: string, callback: (args: {
dialog: DialogSubmitAction;
context: Context & StringIndexed$1;
ack: AckFn$1<string | SayArguments$1> | AckFn$1<void> | AckFn$1<DialogValidation>;
body: SlackAction;
respond: RespondFn$1;
}) => Promise<void>): void;
onShortcut(shortcutId: string, callback: (args: {
shortcut: SlackShortcut;
context: Context & StringIndexed$1;
ack: AckFn$1<void>;
body: SlackShortcut;
respond: RespondFn$1;
}) => Promise<void>): void;
onEvent(eventType: string, callback: (args: {
event: KnownEventFromType$1<string>;
context: Context & StringIndexed$1;
body: EnvelopedEvent$1<KnownEventFromType$1<string>>;
}) => Promise<void>): void;
}
export { type AckFn, type BaseSlackEvent, type EnvelopedEvent, type EventFromType, type EventTypePattern, type FunctionInputs, type KnownEventFromType, type RespondArguments, type RespondFn, type SayArguments, type SayFn, type SlackCommandMiddlewareArgs, type SlackEventMiddlewareArgs, SlackWebhook, type SlashCommand, type StringIndexed, WebhookParseError };