티스토리 뷰

과거 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부분에 같이 전달해보았지만 적용되지 않았다.

 

이후 해결책을 올릴 예정이다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/07   »
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
글 보관함