[Slack API] Slack Bot 만들고 Slack 메세지 보내기 / Scopes / slacker
기존에 Bot user을 추가하는 방식은 deprecated되었고, token scope을 지정하는 방식으로 바뀌었다.
그런데 공식 api tutorial도 업데이트가 잘 안되있어서 직접 정리해보았다.
Slack 회원가입 후 새로운 workspace 만들기
Where work happens
Slack is where work flows. It's where the people you need, the information you share, and the tools you use come together to get things done.
slack.com
Slack API에서 새로운 app만들기
Where work happens
Slack is where work flows. It's where the people you need, the information you share, and the tools you use come together to get things done.
slack.com
Create a Slack App
OAuth & Permissions
Bot Token Scopes를 정해야하는데, 간단히 메세지만 보낼거기 때문에
chat:write만 추가해도 된다.
Install App
그러면 OAuth Tokens를 발급받을 수 있다.
Channels에 새로 만든 app 추가하기
#general 채널에 app을 추가하자.
그리고 channel의 encoded ID를 알아야한다.
예를 들어 url이 다음과 같다면
https://app.slack.com/client/T013H8JJLG1/C039UVDEZL0/
하이라이트 부분이 encoded ID이다.
Slacker
Full-featured Python interface for the Slack API [Github]
코드
from slacker import Slacker
slack = Slacker('<Your OAuth Token>')
# Send a message to #general channel
slack.chat.post_message('<channel encoded ID>', 'Hello fellow slackers!')
참고로, API Methods Documentation을 보면 다양한 메소드와 메소드별 token type과 required scope을 알 수 있다.
끝!