File tree 2 files changed +45
-0
lines changed
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { Module } from '@nestjs/common' ;
2
+ import { SlackService } from './slack.service' ;
3
+
4
+ @Module ( {
5
+ providers : [ SlackService ] ,
6
+ exports : [ SlackService ] ,
7
+ } )
8
+ export class SlackModule { }
Original file line number Diff line number Diff line change
1
+ import { Injectable , Logger } from '@nestjs/common' ;
2
+ import axios from 'axios' ;
3
+
4
+ @Injectable ( )
5
+ export class SlackService {
6
+ private readonly logger = new Logger ( SlackService . name ) ;
7
+
8
+ constructor ( ) { }
9
+
10
+ async sendDirectMessage ( slackId : string , message : string ) : Promise < void > {
11
+ if ( slackId . length === 0 || slackId === undefined || slackId === null ) {
12
+ this . logger . warn ( 'Slack ID is not defined' ) ;
13
+ return ;
14
+ }
15
+
16
+ const slackToken = process . env . SLACK_BOT_USER_OAUTH_ACCESS_TOKEN ;
17
+ const postMessageUrl = 'https://slack.com/api/chat.postMessage' ;
18
+
19
+ try {
20
+ await axios . post (
21
+ postMessageUrl ,
22
+ {
23
+ channel : slackId ,
24
+ text : message ,
25
+ } ,
26
+ {
27
+ headers : {
28
+ Authorization : `Bearer ${ slackToken } ` ,
29
+ 'Content-Type' : 'application/json' ,
30
+ } ,
31
+ } ,
32
+ ) ;
33
+ } catch ( error ) {
34
+ this . logger . error ( error ) ;
35
+ }
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments