Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add Slack module
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 13, 2020
1 parent b9fffba commit 99f17c8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ElasticSearchModule } from './providers/elasticsearch/elasticsearch.mod
import { GeolocationModule } from './providers/geolocation/geolocation.module';
import { MailModule } from './providers/mail/mail.module';
import { PrismaModule } from './providers/prisma/prisma.module';
import { SlackModule } from './providers/slack/slack.module';
import { TasksModule } from './providers/tasks/tasks.module';

@Module({
Expand Down Expand Up @@ -64,6 +65,7 @@ import { TasksModule } from './providers/tasks/tasks.module';
AuditLogsModule,
WebhooksModule,
ElasticSearchModule,
SlackModule,
],
providers: [
{
Expand Down
7 changes: 7 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ export interface Configuration {
deleteOldLogs: boolean;
deleteOldLogsDays: number;
};

slack: {
token: string;
slackApiUrl?: string;
rejectRateLimitedCalls?: boolean;
retries: number;
};
}
6 changes: 6 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ const configuration: Configuration = {
: true,
deleteOldLogsDays: int(process.env.TRACKING_DELETE_OLD_LOGS_DAYS, 90),
},
slack: {
token: process.env.SLACK_TOKEN ?? '',
slackApiUrl: process.env.SLACK_API_URL,
rejectRateLimitedCalls: !!process.env.SLACK_REJECT_RATE_LIMITED_CALLS,
retries: int(process.env.SLACK_FAIL_RETRIES, 3),
},
};

const configFunction: ConfigFactory<Configuration> = () => configuration;
Expand Down
10 changes: 10 additions & 0 deletions src/providers/slack/slack.module.ts
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 {}
43 changes: 43 additions & 0 deletions src/providers/slack/slack.service.ts
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);
}
}

0 comments on commit 99f17c8

Please sign in to comment.