👨🏽‍💻

ch3 - 6. Slack 발송하기

Slack 이란?

"모든 대화와 지식을 위한 검색 가능한 로그"(Searchable Log of All Conversation and Knowledge) 의 준말로 스튜어트 버터필드가 만든 클라우드 기반 팀 협업 도구입니다.
 

Slack 환경설치

Slack 발송을 위한 라이브러리인 slacker 를 설치합니다. slacker 를 사용하기 위해서는 slack token 을 입력해주어야 합니다. slack token 은 다음 사이트에서 발급 받으실 수 있습니다. ( https://api.slack.com )
pip install slacker
 

Token 발급받기

  1. slack api 홈페이지 우측 상단에 Your Apps 로 접근합니다.
notion imagenotion image
 
2. App 을 생성합니다.
notion imagenotion image
 
3. Bot user 를 생성합니다.
notion imagenotion image
 
4. OAuth & Permissions 로 이동하여 Workspace 를 생성합니다.
notion imagenotion image
 
5. Bot User OAuth Access Token 을 사용합니다.
notion imagenotion image
 

Message 발송

slacker 라이브러리와 Slack Token 을 이용해서 기본 메세지를 출력하는 코드입니다.
from slacker import Slacker token = 'Bot User OAuth Access Token 을 입력합니다.' slack = Slacker(token) slack.chat.post_message('#Channel 을 입력합니다.', 'Hello Slack')
 
위의 코드를 실행한 결과 다음과 같은 Message 가 발송된 것을 알 수 있습니다.
notion imagenotion image
 

Message 설정

Message 에 몇 가지 설정을 하여 다양한 유형의 Message 를 발송할 수 있습니다.
from slacker import Slacker token = 'Bot User OAuth Access Token 을 입력합니다.' slack = Slacker(token) attachments = dict() attachments['pretext'] = "네이버로 이동합니다" attachments['title'] = "네이버" attachments['title_link'] = "https://www.naver.com" attachments['text'] = "본문 텍스트로 5줄 까지만 출력됩니다. ( 5줄 초과시 show more 로 출력됩니다. )" attachments['mrkdwn_in'] = ["text", "pretext"] # 마크다운 설정입니다. attachments = [attachments] slack.chat.post_message(channel="#Channel 을 입력합니다.", text=None, attachments=attachments)
다음과 같이 텍스트의 위치, Markdown, 하이퍼링크 같은 세세한 설정이 가능한 것을 알 수 있습니다.
notion imagenotion image