-
Notifications
You must be signed in to change notification settings - Fork 1
/
teams.js
311 lines (264 loc) · 7.25 KB
/
teams.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const os = require('os');
const {cfg, Connect} = require('sm-utils');
const startCase = require('lodash/startCase');
const Notify = require('./Notify');
const {getPackageInfo, getLogger} = require('./helpers');
const DEFAULT_CHANNEL = 'default';
const DEFAULT_WEBHOOK_NAME = 'default';
/**
* @typedef {import('@smpx/notify').TeamsTypes.Section} Section
* @typedef {import('@smpx/notify').TeamsTypes.Action} Action
* @typedef {import('@smpx/notify').TeamsTypes.MessageCard} MessageCard
*/
/**
* @param {string} [channel] `<channelName>[.<webhookName>]`
* @returns {string}
*/
function getTeamsWebhookUrl(channel = Teams.defaultChannel) {
if (!channel.includes('.')) channel += `.${DEFAULT_WEBHOOK_NAME}`;
// @ts-ignore
return cfg(`teams.${channel}`);
}
/**
* @returns {Section[]}
*/
function getDefaultAttachments() {
const {name, version} = getPackageInfo();
const facts = [
{
name: 'Hostname',
value: os.hostname(),
},
{
name: 'Node Environment',
value: cfg.getEnv(),
},
];
let footerSuffix = '';
if (process.env.name || process.env.pm_id) {
footerSuffix = `| ${process.env.name} ${process.env.pm_id || -1}`
}
return [{
activityTitle: 'App Info:',
activitySubtitle: `${name} v${version} ${footerSuffix} | ${new Date().toLocaleString()}`,
facts,
}];
}
class Teams extends Notify {
/**
* Overwrite this function to skip teams message sending in some cnditions
* and log the message instead. By default skips in test environment
*/
static logCondition() {
return cfg.isTest();
}
/** @param {{text?: string, channel?: string}} opts */
constructor({text, channel} = {}) {
super();
this._themeColor = '439FE0'; // blue
/** @type {Section[]} */
this._sections = [];
/** @type {Error[]} */
this._errors = [];
/** @type {Action[]} */
this._actions = [];
if (channel) this.channel(channel);
if (text) this.text(text);
}
/** @param {string} channel */
static set defaultChannel(channel) {
this._defaultChannel = channel;
}
/** @returns {string} */
static get defaultChannel() {
return this._defaultChannel || DEFAULT_CHANNEL;
}
/** [Dummy] Teams does not support username */
username() { return this }
/** [Dummy] Teams does not support icon */
icon() { return this }
/** @param {string} channel */
channel(channel) {
this._channel = channel;
return this;
}
/** @param {string} summary */
summary(summary) {
this._summary = summary;
return this;
}
/** @param {string} color */
color(color) {
this._themeColor = color.replace('#', '');
return this;
}
/** @param {string} title */
title(title) {
this._title = title;
return this;
}
/** @param {string} text */
text(text) {
this._text = Teams.escapeText(text);
return this;
}
/** @param {Section | Section[]} sections */
attachment(sections) {
if (!Array.isArray(sections)) sections = [sections];
sections = sections.map((section) => {
if (section.text) section.text = Teams.escapeText(section.text);
return section;
});
this._sections = this._sections.concat(sections);
return this;
}
/** @param {Action | Action[]} actions */
action(actions) {
if (!Array.isArray(actions)) actions = [actions];
this._actions = this._actions.concat(actions);
return this;
}
/**
* Create a MessageBox
* @param {Error} err
* @param {{label?: string, title?: string}} [param1={}]
*/
error(err, {label = '', title = ''} = {}) {
this._errors.push(err);
const {bugs, version} = getPackageInfo();
const bugsUrl = bugs && bugs.url;
err.stack = err.stack.replace(/ /g, ' ');
label = `[${label || err.name}] `;
this.color('F00'); // Red
this.title(`Error: ${err.message}`);
this.text(err.stack);
this.button(
'Create an issue for this error?',
`${bugsUrl}/new?title=${
encodeURIComponent(`${label}${title || err.message}`)
}&body=${encodeURIComponent(
`Error encountered on ${new Date().toLocaleString()}\n` +
`App version: v${version}\n\n` +
`Full Stack: ${err.stack}`
)}&labels=bug`
);
return this;
}
/**
* Add a section with facts
* @param {string} title
* @param {{[statTitle: string]: string | number | boolean | object}} keyValues
* @param {{ignoreUndefined?: boolean}} opts
*/
stats(title, keyValues, {ignoreUndefined = true} = {}) {
/** @type {Section} */
const section = {
title,
facts: [],
};
Object.keys(keyValues).forEach((key) => {
let value = keyValues[key];
if(ignoreUndefined && value === undefined) return;
if (['boolean', 'number', 'undefined'].includes(typeof value)) value = String(value);
else if (typeof value !== 'string') value = String(JSON.stringify(value));
value = Teams.escapeText(value.trim());
section.facts.push({
name: startCase(key.trim()),
value,
});
})
return this.attachment(section);
}
/**
* @param {string} name
* @param {string} uri
*/
button(name, uri) {
/** @type {Action} */
const button = {
"@type": 'OpenUri',
name,
targets: [{
os: 'default',
uri,
}],
};
return this.action(button);
}
/**
* @param {{defaultAttachment?: boolean}} opts
*/
async send({defaultAttachment = true} = {}) {
if (!this._summary && !this._text) throw new Error('Either summary or text is required');
/** @type {MessageCard} */
const message = {
summary: this._summary,
themeColor: this._themeColor,
title: this._title,
text: this._text,
sections: this._sections,
potentialAction: this._actions,
}
return Teams.postMessage(message, {channel: this._channel, defaultAttachment});
}
/**
* @param {MessageCard} message
* @param {object} [opts]
* @param {string} [opts.channel]
* @param {boolean} [opts.defaultAttachment=true]
*/
static async postMessage(message, {channel, defaultAttachment = true} = {}) {
// Do not modify original message;
message = Object.assign({}, message);
message.sections = message.sections || [];
if (defaultAttachment) message.sections = message.sections.concat(getDefaultAttachments());
if (this.logCondition()) {
getLogger().info({label: 'Teams', ...message}, 'Teams message');
return;
}
const webhookUrl = getTeamsWebhookUrl(channel);
if (!webhookUrl) throw new Error(`No webhook url for channel: "${channel}"`)
return this._postMessage(webhookUrl, message);
}
static bold(txt) {
return `**${txt}**`;
}
static italics(txt) {
return `*${txt}*`;
}
static strikethrough(txt) {
return `~~${txt}~~`;
}
/**
* @param {string} url
* @param {string} text
*/
static formatUrl(url, text) {
return `[${text}](${url})`;
}
/**
* Replace `\n` with `3 spaces + \n`
* @see https://stackoverflow.com/questions/52637567/how-to-insert-newline-into-ms-teams-markdown
* @param {string} text
*/
static escapeText(text) {
if (!text) return text;
return text.replace(/(\n*)/g, (_, lineBreaks) => {
if (!lineBreaks.length) return lineBreaks;
let ret = '';
for (let i = 0; i < lineBreaks.length - 1; i++) ret += ' \n ';
return ret + ' \n';
});
}
/**
* @param {string} webhookUrl
* @param {string} [channel] default: `Teams.defaultChannel`
*/
static setWebhook(webhookUrl, channel = Teams.defaultChannel) {
if (!channel.includes('.')) channel += `.${DEFAULT_WEBHOOK_NAME}`;
cfg.set(`teams.${channel}`, webhookUrl);
}
/** [Dummy] */
static setToken() {}
}
module.exports = Teams;