201028 TIL 슬랙봇 만들기
슬랙봇 만들기
- slack 계정 파기
- https://slack.com/intl/en-kr/get-started#/
- Create a Slack Workspace
- 볼트
https://slack.dev/bolt-js/tutorial/getting-started#create-an-app
2-1. create an app
2-2. Auth
-
Scopes > Bot Token Scopes =
chat:write
-> Install To your app space
2-3 setting up local project
mkdir first-bolt-app
cd first-bolt-app
npm init
2-4 save bot info
-
OAuth & Permissions > Tokens
xoxb-
-
Basic Info > App Credential > Signing Secret
-
node , app.js
npm install @slack/bolt
-
ngrok
https://api.slack.com/tutorials/tunneling-with-ngrok
그냥 해도 되네…?
-
Event Subscription
- /slack/events
- Subscribe to bot events
- Subscribe to Bot Events. There are four events related to messages:
message.channels
(listens for messages in public channels),message.groups
(listens for messages in private channels),message.im
(listens for messages in the App Home/DM space), andmessage.mpim
(listens for messages in multi-person DMs).
- Subscribe to Bot Events. There are four events related to messages:
- save Change 꼭 하기 => reinstall!!
Bot Token Scopes
Scopes that govern what your app can access.
OAuth Scope
Description
View messages that directly mention @onuii_bot in conversations that the app is in
View messages and other content in public channels that 오누이_봇 has been added to
Send messages as @onuii_bot
Send messages as @onuii_bot with a customized username and avatar
Send messages to channels @onuii_bot isn’t a member of
View messages and other content in private channels that 오누이_봇 has been added to
View messages and other content in direct messages that 오누이_봇 has been added to
View messages and other content in group direct messages that 오누이_봇 has been added to
- heroku 배포하기
https://devcenter.heroku.com/articles/getting-started-with-nodejs?singlepage=true
git init
heroku login
git init
heroku create
.gitignore
/ gaa / gcmsg / gp- 왜인지 verify 안됨…
- verify 하기
npm i body-parser
/* 슬랙 봇 */
const { App, ExpressReceiver} = require('@slack/bolt');
const bodyParser = require('body-parser');
const port = process.env.PORT || 3030;
const receiver = new ExpressReceiver({ signingSecret: '2bd8a9b8c0f5b9462787b73c6d601d76' });
// parse application/x-www-form-urlencoded
receiver.router.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
receiver.router.use(bodyParser.json());
receiver.router.post('/slack/events', function (req, res) {
const value = req.body.challenge;
res.send(value);
});
package.json
"scripts": {
"start": "node app.js"
},
- heroku server get 스케줄러
https://cron-job.org/en/members/
- create cronjob
// Other web requests are methods on receiver.router
receiver.router.get('/cronjob', (req, res) => {
// You're working with an express req and res now.
return res.send(true);
});