This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9fffba
commit 99f17c8
Showing
5 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { SlackService } from './slack.service'; | ||
|
||
@Module({ | ||
imports: [ConfigModule], | ||
providers: [SlackService], | ||
exports: [SlackService], | ||
}) | ||
export class SlackModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { ChatPostMessageArguments, WebClient } from '@slack/web-api'; | ||
import PQueue from 'p-queue'; | ||
import pRetry from 'p-retry'; | ||
import { Configuration } from '../../config/configuration.interface'; | ||
|
||
@Injectable() | ||
export class SlackService { | ||
slack: WebClient; | ||
private logger = new Logger(SlackService.name); | ||
private queue = new PQueue({ concurrency: 1 }); | ||
|
||
constructor(private configService: ConfigService) { | ||
const config = this.configService.get<Configuration['slack']>('slack'); | ||
if (config.token) | ||
this.slack = new WebClient(config.token, { | ||
slackApiUrl: config.slackApiUrl, | ||
rejectRateLimitedCalls: config.rejectRateLimitedCalls, | ||
}); | ||
} | ||
|
||
send(options: ChatPostMessageArguments) { | ||
this.queue | ||
.add(() => | ||
pRetry(() => this.sendMessage(options), { | ||
retries: this.configService.get<number>('slack.retries') ?? 3, | ||
onFailedAttempt: (error) => { | ||
this.logger.error( | ||
`Message to ${options.channel} failed, retrying (${error.retriesLeft} attempts left)`, | ||
error.name, | ||
); | ||
}, | ||
}), | ||
) | ||
.then(() => {}) | ||
.catch(() => {}); | ||
} | ||
|
||
private async sendMessage(options: ChatPostMessageArguments) { | ||
return this.slack.chat.postMessage(options); | ||
} | ||
} |