1 | import { AnyObject, CommonLogger } from '@naturalcycles/js-lib'
|
2 | import { InspectAnyOptions } 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 | /**
|
48 | * Keys-values will be rendered as MessageAttachment with Fields
|
49 | */
|
50 | kv?: AnyObject
|
51 |
|
52 | /**
|
53 | * If specified - adds @name1, @name2 in the end of the message
|
54 | */
|
55 | mentions?: string[]
|
56 |
|
57 | /**
|
58 | * By default it ignores possible errors from slack
|
59 | *
|
60 | * @default false
|
61 | */
|
62 | throwOnError?: boolean
|
63 | }
|
64 |
|
65 | export interface SlackAttachmentField {
|
66 | title: string
|
67 | value: string
|
68 | short?: boolean
|
69 | }
|
70 |
|
71 | // Taken from here: https://github.com/slackapi/node-slack-sdk/blob/master/packages/types/src/index.ts
|
72 | export interface SlackMessageAttachment {
|
73 | // blocks?: (KnownBlock | Block)[];
|
74 | fallback?: string // either this or text must be defined
|
75 | color?: 'good' | 'warning' | 'danger' | string
|
76 | pretext?: string
|
77 | author_name?: string
|
78 | author_link?: string // author_name must be present
|
79 | author_icon?: string // author_name must be present
|
80 | title?: string
|
81 | title_link?: string // title must be present
|
82 | text?: string // either this or fallback must be defined
|
83 | fields?: SlackAttachmentField[]
|
84 | image_url?: string
|
85 | thumb_url?: string
|
86 | footer?: string
|
87 | footer_icon?: string // footer must be present
|
88 | ts?: number
|
89 | // actions?: AttachmentAction[];
|
90 | callback_id?: string
|
91 | mrkdwn_in?: ('pretext' | 'text' | 'fields')[]
|
92 | }
|
93 |
|
94 | /**
|
95 | * Return `null` to skip (filter out) the message completely.
|
96 | */
|
97 | export type SlackMessagePrefixHook<CTX = any> = (
|
98 | msg: SlackMessage<CTX>,
|
99 | ) => string[] | null | Promise<string[] | null>
|
100 |
|
101 | export interface SlackServiceCfg<CTX = any> {
|
102 | /**
|
103 | * Undefined means slack is disabled.
|
104 | */
|
105 | webhookUrl?: string
|
106 |
|
107 | defaults?: Partial<SlackMessage>
|
108 |
|
109 | /**
|
110 | * Function to return an array of "prefix tokens" (will be joined by ': ').
|
111 | * Allows to skip (filter out) the message by returning `null`.
|
112 | */
|
113 | messagePrefixHook: SlackMessagePrefixHook<CTX>
|
114 |
|
115 | /**
|
116 | * By default SlackService logs every message to console.log
|
117 | * Pass another logger if needed.
|
118 | * Pass `noopLogger` to suppress logging completely.
|
119 | */
|
120 | logger: CommonLogger
|
121 |
|
122 | /**
|
123 | * Defaults to:
|
124 | * includeErrorData: true
|
125 | * includeErrorStack: true
|
126 | */
|
127 | inspectOptions: InspectAnyOptions
|
128 | }
|