201028 TIL 슬랙봇 만들기

» 1.5막, TIL (Today I Learned), 사이드프로젝트

슬랙봇 만들기

  1. slack 계정 파기
  • https://slack.com/intl/en-kr/get-started#/
  • Create a Slack Workspace
  1. 볼트

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

  1. node , app.js

    npm install @slack/bolt

  2. ngrok

    https://api.slack.com/tutorials/tunneling-with-ngrok

    그냥 해도 되네…?

  3. 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), and message.mpim (listens for messages in multi-person DMs).
    • save Change 꼭 하기 => reinstall!!

Bot Token Scopes

Scopes that govern what your app can access.

OAuth Scope

Description

app_mentions:read

View messages that directly mention @onuii_bot in conversations that the app is in

channels:history

View messages and other content in public channels that 오누이_봇 has been added to

chat:write

Send messages as @onuii_bot

chat:write.customize

Send messages as @onuii_bot with a customized username and avatar

chat:write.public

Send messages to channels @onuii_bot isn’t a member of

groups:history

View messages and other content in private channels that 오누이_봇 has been added to

im:history

View messages and other content in direct messages that 오누이_봇 has been added to

mpim:history

View messages and other content in group direct messages that 오누이_봇 has been added to

  1. 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 안됨…
  1. 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"
  },
  1. 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);
});