https://slack.com/intl/ko-kr/get-started#/landing 으로 접속하여 봇을 적용할 워크 스페이스 생성
워크 스페이스 이름 적기
http://{생성한-워크스페이스-이름}.slack.com/apps
접속아까 생성한 워크스페이스로 접속되었는지 확인!
API 토큰 값은 타인에게 공개 금지!!
6. 워크스페이스에서 봇 생성 확인
봇이 away 상태인지 확인
Node.js
: v11.6.0TypeScript
: 4.2.4Node.js 기반이기 때문에 node.js는 필수로 설치되더 있어야 함
env
파일을 불러와 설정값을 process.env
에 저장시켜 어디에서든 불러와 사용 가능mkdir slack-bot
cd slack-bot
npm init --yes
모두 프로젝트 루트에 생성!!!
.env
환경변수 파일 생성
BOT_TOKEN=생성된-봇의-API토큰-값
package.json
{
"name": "daily-alarm",
"main": "bot.ts",
"version": "0.0.0",
"private": true,
"description": "",
"scripts": {
"start": "nodemon --exec ts-node bot.ts"
},
"dependencies": {
"@slack/web-api": "^6.1.0",
"dotenv": "^9.0.1",
"moment": "^2.29.1",
"node-schedule": "^2.0.0"
},
"devDependencies": {
"@types/node-schedule": "^1.3.1",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
}
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2017",
"moduleResolution": "node",
"sourceMap": false,
"allowJs": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
}
},
"compileOnSave": true,
"include": ["*.ts"],
"exclude": [
"node_modules",
]
}
필요할 때 마다 process.env로 환경변수를 직접 접근하기 보다는 따로 모듈화 시켜서 호출
import * as dotenv from "dotenv";
// .env 파일의 위치를 직접 지정
dotenv.config({ path: `${__dirname}/.env` });
export const BOT_TOKEN = process.env.BOT_TOKEN;
const { WebClient } = require("@slack/web-api");
import { BOT_TOKEN } from "./env";
import * as schedule from "node-schedule";
import * as moment from "moment";
// 봇 접근
const web = new WebClient(BOT_TOKEN);
// 봇이 메세지를 보낼 채널 ID
const conversationId = "daily";
// 월요일부터 금요일까지 0시에 오늘 날짜 보내는 스케줄러 작성
const scheduler = schedule.scheduleJob("0 0 ? * MON-FRI *", async () => {
const res = await web.chat.postMessage({
channel: conversationId,
text: moment().format("MM-DD"),
});
console.log("Message sent: ", res.ts);
});
const { WebClient } = require("@slack/web-api");
import { BOT_TOKEN } from "./env";
import * as schedule from "node-schedule";
import * as moment from "moment";
const web = new WebClient(BOT_TOKEN);
const conversationId = "daily";
// 1분마다 작동하도록 cron 표현식 수정
const scheduler = schedule.scheduleJob("0 */1 * * * *", async () => {
const res = await web.chat.postMessage({
channel: conversationId,
text: moment().format("MM-DD"),
});
console.log("Message sent: ", res.ts);
});
npm start
요거 혹시 질문 좀 드려도 될까요~??