1 | import { StringMap } from '@naturalcycles/js-lib'
|
2 | import { DebugLogLevel } from '..'
|
3 |
|
4 | /**
|
5 | * Properties that exists both in SlackApiBody (as per Slack API) and SlackMessage (our abstraction).
|
6 | */
|
7 | export interface SlackMessageProps {
|
8 | /**
|
9 | * @default bot
|
10 | */
|
11 | username?: string
|
12 |
|
13 | channel?: string
|
14 | icon_url?: string
|
15 |
|
16 | /**
|
17 | * @default :spider_web:
|
18 | */
|
19 | icon_emoji?: string
|
20 |
|
21 | attachments?: SlackMessageAttachment[]
|
22 | }
|
23 |
|
24 | export interface SlackApiBody extends SlackMessageProps {
|
25 | text: string
|
26 | }
|
27 |
|
28 | export interface SlackMessage<CTX = any> extends SlackMessageProps {
|
29 | /**
|
30 | * The only *required* field.
|
31 | *
|
32 | * You can throw anything at it, it'll handle it appropriately:
|
33 | * String - as is
|
34 | * Object - pass via util.inspect()
|
35 | * Array - will pass each item via util.inspect() and join with \n
|
36 | * Error - print the stack nicely
|
37 | *
|
38 | * If you don't want the default Array behavior - you can pre-util.inspect() it yourself to your liking.
|
39 | */
|
40 | items: any
|
41 |
|
42 | /**
|
43 | * Optional "context object", to be used by `messagePrefixHook`.
|
44 | */
|
45 | ctx?: CTX
|
46 |
|
47 | level?: DebugLogLevel
|
48 |
|
49 | /**
|
50 | * Keys-values will be rendered as MessageAttachment with Fields
|
51 | */
|
52 | kv?: StringMap<any>
|
53 |
|
54 | /**
|
55 | * If specified - adds @name1, @name2 in the end of the message
|
56 | */
|
57 | mentions?: string[]
|
58 |
|
59 | /**
|
60 | * @default false
|
61 | * By default it ignores possible errors from slack
|
62 | */
|
63 | throwOnError?: boolean
|
64 |
|
65 | /**
|
66 | * @default false
|
67 | * Skips logging message
|
68 | */
|
69 | noLog?: boolean
|
70 | }
|
71 |
|
72 | export interface SlackAttachmentField {
|
73 | title: string
|
74 | value: string
|
75 | short?: boolean
|
76 | }
|
77 |
|
78 | // Taken from here: https://github.com/slackapi/node-slack-sdk/blob/master/packages/types/src/index.ts
|
79 | export interface SlackMessageAttachment {
|
80 | // blocks?: (KnownBlock | Block)[];
|
81 | fallback?: string // either this or text must be defined
|
82 | color?: 'good' | 'warning' | 'danger' | string
|
83 | pretext?: string
|
84 | author_name?: string
|
85 | author_link?: string // author_name must be present
|
86 | author_icon?: string // author_name must be present
|
87 | title?: string
|
88 | title_link?: string // title must be present
|
89 | text?: string // either this or fallback must be defined
|
90 | fields?: SlackAttachmentField[]
|
91 | image_url?: string
|
92 | thumb_url?: string
|
93 | footer?: string
|
94 | footer_icon?: string // footer must be present
|
95 | ts?: number
|
96 | // actions?: AttachmentAction[];
|
97 | callback_id?: string
|
98 | mrkdwn_in?: ('pretext' | 'text' | 'fields')[]
|
99 | }
|
100 |
|
101 | /**
|
102 | * Return `null` to skip (filter out) the message completely.
|
103 | */
|
104 | export type SlackMessagePrefixHook<CTX = any> = (
|
105 | msg: SlackMessage<CTX>,
|
106 | ) => string[] | null | Promise<string[] | null>
|
107 |
|
108 | export interface SlackServiceCfg<CTX = any> {
|
109 | /**
|
110 | * Undefined means slack is disabled.
|
111 | */
|
112 | webhookUrl?: string
|
113 |
|
114 | defaults?: Partial<SlackMessage>
|
115 |
|
116 | /**
|
117 | * Override channel when msg.level is set.
|
118 | * key: DebugLogLevel
|
119 | * value: channel name to send message to
|
120 | */
|
121 | channelByLevel?: StringMap
|
122 |
|
123 | /**
|
124 | * Function to return an array of "prefix tokens" (will be joined by ': ').
|
125 | * Allows to skip (filter out) the message by returning `null`.
|
126 | */
|
127 | messagePrefixHook: SlackMessagePrefixHook<CTX>
|
128 | }
|