티스토리 뷰
과거 slack api를 활용한 코딩 자료를 찾아보았다.
from slacker import Slacker
slack = Slacker('---------------------------------------------')
markdown_text = '''
This message is plain.
*This message is bold.*
`This message is code.`
_This message is italic._
~This message is strike.~
'''
attach_dict = {
'color' :'#ff0000',
'author_name':'-------',
"author_link":'github.com/',
'title' :'-----------------',
'title_link' :'http://naver.com/',
'text' :'-----------------'
}
attach_list = [attach_dict]
slack.chat.post_message(channel="#general", text=markdown_text, attachments=attach_list)
다시 실행시겼을 때 다음과 같은 오류가 발생했다.
slacker.Error: invalid_auth
이유로는
2021년 2월 24일 slack에 다음과 같은 정책 변화가 있었다
https://api.slack.com/changelog/2021-02-24-how-we-broke-your-slack-app
즉, post_message, slack_sdk 서비스를 더 이상 제공하지 않는다는 것이다.
다음은 현재 slack 공식 활용 문서이다
(2022-08-02에 작성된 자료입니다)
https://api.slack.com/messaging/sending
메세지를 전달하기위한 파이썬 코드를 다음과 같이 제공하고 있었다.
import logging
import os
# Import WebClient from Python SDK (github.com/slackapi/python-slack-sdk)
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
# WebClient instantiates a client that can call API methods
# When using Bolt, you can use either `app.client` or the `client` passed to listeners.
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
logger = logging.getLogger(__name__)
channel_name = "needle"
conversation_id = None
try:
# Call the conversations.list method using the WebClient
for result in client.conversations_list():
if conversation_id is not None:
break
for channel in result["channels"]:
if channel["name"] == channel_name:
conversation_id = channel["id"]
#Print result
print(f"Found conversation ID: {conversation_id}")
break
except SlackApiError as e:
print(f"Error: {e}")
다음과 같은 오류가 발생한다.
Error: The request to the Slack API failed. (url: https://www.slack.com/api/conversations.list)
The server responded with: {'ok': False, 'error': 'not_authed'}
해결 방안은 다음과 같다.
import requests
def send_message(token, channel, text):
response = requests.post("https://slack.com/api/chat.postMessage",
headers={"Authorization": "Bearer "+token},
data={"channel": channel,"text": text}
)
print(response)
myToken = " 토큰값 입력 "
post_message(myToken," 채널명 ", " 전달할 메세지 ")
하지만, 아직 attachments를 적용하는 방법은 찾지 못했다
requests에 data부분에 같이 전달해보았지만 적용되지 않았다.
이후 해결책을 올릴 예정이다.
'프로그래밍 > 코딩기록' 카테고리의 다른 글
오류) ImportError: cannot import name 'warnings' from 'matplotlib.dates' - backtrader (0) | 2022.08.02 |
---|---|
오류) Docker 이용해 MYSQL 연동 - 2 (0) | 2022.06.26 |
오류) Docker 이용해 MYSQL 연동 (0) | 2022.06.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 365일장
- po session
- 소멸도시
- 광장시장맛집
- 폭염
- 별의별이주
- 동랑
- TOSS
- slacker
- DongRang
- slack
- SETA
- 순이네빈대떡
- 홍천
- 블로킹현상
- 소비자
- Matplotlib
- 광장시장꽈배기
- slcak api
- 고향칼국수
- 완주
- backtrader
- 광장시장
- 유럽 폭염
- 이상기후
- 옥천신문
- 기후변화
- 창업
- 수정분식
- 청년정책
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함