-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeliverer.ts
More file actions
47 lines (44 loc) · 1.32 KB
/
deliverer.ts
File metadata and controls
47 lines (44 loc) · 1.32 KB
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
import { IncomingWebhook } from '@slack/webhook';
import { createLogger, format, Logger, transports } from 'winston';
/**
* Deliverer is the man who will send messages to external service
* Separated from the provider to allow testing 'send' method
* Loop is Slack-like platform, so we use Slack API to send messages.
*
*/
export default class LoopDeliverer {
/**
* Logger module
* (default level='info')
*/
private logger: Logger = createLogger({
level: process.env.LOG_LEVEL || 'info',
transports: [
new transports.Console({
format: format.combine(
format.timestamp(),
format.colorize(),
format.simple(),
format.printf((msg) => `${msg.timestamp} - ${msg.level}: ${msg.message}`)
),
}),
],
});
/**
* Sends message to the Loop through the Incoming Webhook app
* https://developers.loop.ru/integrate/webhooks/incoming/
*
* @param endpoint - where to send
* @param message - what to send
*/
public async deliver(endpoint: string, message: string): Promise<void> {
try {
const webhook = new IncomingWebhook(endpoint, {
username: 'Hawk',
});
await webhook.send(message);
} catch (e) {
this.logger.log('error', 'Can\'t deliver Incoming Webhook. Loop returns an error: ', e);
}
}
}