[NestJS] discord webhook

Eden·2023년 12월 4일
0

부스 서비스를 운영중 특정 이벤트에 대해 관리자에게 알림을 보내줄 필요가 있었는데 평소 커뮤니케이션에 주로 이용하는 discord에 알림을 받을 채널을 만들어 사용하게 되었다.

1. discord 채널에 서버 설정에 들어간다.

2. 웹후크로 이동

3. 웹후크 생성 및 URL 복사

4.nestjs 코드에서 메세지 전송

디스코드 웹훅 패키지를 설치

npm install discord-webhook-node 

코드 작성

import { Webhook } from 'discord-webhook-node'; //패키지 추가

//디스코드 메세지 보내는 함수
async sendingMsg(){
  try {
      await this.contributeRepository.save(contribute);
      const hook = new Webhook(
        '위에서 복사한 URL',
      );
      const message = 'test';
      try {
        await hook
          .send(message)
          .then(() => console.log('메시지가 성공적으로 전송되었습니다.'))
          .catch((err) => console.log(err.message));
      } catch (error) {
        console.error(`메시지 전송 실패. 에러: ${error.message}`);
      }
}
profile
주섬주섬..

0개의 댓글