-
Notifications
You must be signed in to change notification settings - Fork 380
/
index.ts
83 lines (77 loc) · 2.63 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { SlackEvent, BasicSlackEvent } from './base-events';
import { StringIndexed } from '../helpers';
import { SayFn } from '../utilities';
export * from './base-events';
export {
GenericMessageEvent,
BotMessageEvent,
ChannelArchiveMessageEvent,
ChannelJoinMessageEvent,
ChannelLeaveMessageEvent,
ChannelNameMessageEvent,
ChannelPostingPermissionsMessageEvent,
ChannelPurposeMessageEvent,
ChannelTopicMessageEvent,
ChannelUnarchiveMessageEvent,
EKMAccessDeniedMessageEvent,
FileShareMessageEvent,
MeMessageEvent,
MessageChangedEvent,
MessageDeletedEvent,
MessageRepliedEvent,
ThreadBroadcastMessageEvent,
} from './message-events';
/**
* Arguments which listeners and middleware receive to process an event from Slack's Events API.
*/
export interface SlackEventMiddlewareArgs<EventType extends string = string> {
payload: EventFromType<EventType>;
event: this['payload'];
message: EventType extends 'message' ? this['payload'] : never;
body: EnvelopedEvent<this['payload']>;
say: WhenEventHasChannelContext<this['payload'], SayFn>;
}
/**
* 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.
*/
export interface EnvelopedEvent<Event = BasicSlackEvent> 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;
// TODO: the two properties below are being deprecated on Feb 24, 2021
authed_users?: string[];
authed_teams?: string[];
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.
*/
export type EventFromType<T extends string> = KnownEventFromType<T> extends never ?
BasicSlackEvent<T> :
KnownEventFromType<T>;
export type KnownEventFromType<T extends string> = Extract<SlackEvent, { type: T }>;
/**
* Type function which tests whether or not the given `Event` contains a channel ID context for where the event
* occurred, and returns `Type` when the test passes. Otherwise this returns `never`.
*/
type WhenEventHasChannelContext<Event, Type> = Event extends { channel: string } | { item: { channel: string } }
? Type
: never;