슬랙 워크스페이스 생성
https://slack.com/intl/ko-kr/get-started#/landing
앱 생성
api.slack.com
create an app
from scratch
app feature 에서 bots 선택
권한설정
워크스페이스에 추가
import { WebClient } from '@slack/web-api';
const token = process.env.BOT_TOCKEN;
console.log(token);
const web = new WebClient(token);
export const sendSlackMessage = async (message: string) => {
try {
const result = await web.chat.postMessage({
channel: '채널ID',
text: message,
});
console.log('Message sent: ', result.ts);
} catch (error) {
console.error('Error sending message to Slack: ', error);
}
};
Error sending message to Slack: Error: An API error occurred: not_authed
슬랙봇 권한설정 후 다시 시도해서 같은 에러가 발생함
console.log(token) -> undefined
환경변수를 받아오지 못하는 문제.
슬랙 코드를 클래스화해서 configService를 사용하는 방법으로 변경
슬랙 모듈, 서비스 생성
app.모듈에 추가되었는지 확인, 슬랙모듈에서 슬랙서비스 export
토큰 환경변수 configService사용해 불러오기
import { ConfigService } from '@nestjs/config';
//...
const token = this.configService.get<string>('SLACK_BOT_TOKEN');