Energy Drink

Slack File Upload 변경 사항과 해결 방법 본문

Data Science/etc

Slack File Upload 변경 사항과 해결 방법

Tech Energy 2024. 10. 10. 18:18
728x90
반응형

Slack API를 이용하여 파일을 업로드하는 방법이 최근 변경되었습니다. 기존 방식으로 호출 시, {"ok":false,"error":"method_deprecated"}라는 에러 메시지를 받게 되는데, 이는 Slack에서 더 이상 지원하지 않는 방식임을 의미합니다.

Slack API 문서에 따르면, 파일 업로드는 이제 3단계 절차로 이루어져야 합니다. 아래는 최신 방식으로 파일을 업로드하는 방법을 설명하고, 실행 가능한 예시 코드를 공유합니다.

1. Slack 파일 업로드 방식의 변화

기존 Slack API를 이용한 파일 업로드 방식은 더 이상 유효하지 않습니다. 새로운 방식에서는 다음과 같은 3단계를 따릅니다:

  1. 파일 정보 생성 (files.upload 호출)
  2. 파일 콘텐츠 전송 (files.upload를 통한 실제 파일 업로드)
  3. 파일 메시지로 게시 (파일을 채널 또는 사용자에게 전송)

Slack 공식 문서에서 이러한 변화를 확인할 수 있습니다. Slack API 파일 업로드 가이드

2. 문제 해결: 스택 오버플로우에서 제안한 실행 가능한 예시 코드

새로운 방식을 시도해도 파일이 채널에 업로드되지 않는 문제가 발생할 수 있습니다. 이 문제를 해결하기 위해 스택 오버플로우에서 공유된 해결 방법을 참고할 수 있습니다. 이 예시는 curl 명령어를 통해 Slack API를 호출하는 방법을 보여줍니다.

 

#!/bin/bash

export FILE_PATH="/folder/your.file"
export CHANNEL_ID="C_some_number"
export TOKEN="your_token"
FILENAME=$(basename "$FILE_PATH")

FILENAME=$(basename "$FILE_PATH")

# Get the file size using macOS compatible stat command. use stat -c%s "$FILE_PATH" otherwise
#FILE_SIZE=$(stat -f%z "$FILE_PATH") # macOS
FILE_SIZE=$(stat -c%s "$FILE_PATH")

# Stage 1: Get an upload URL
UPLOAD_URL_RESPONSE=$(curl -s -F files=@"$FILENAME" -F filename="$FILENAME" -F token=$TOKEN -F length=$FILE_SIZE https://slack.com/api/files.getUploadURLExternal)

UPLOAD_URL=$(echo "$UPLOAD_URL_RESPONSE" | jq -r '.upload_url')
FILE_ID=$(echo "$UPLOAD_URL_RESPONSE" | jq -r '.file_id')

if [ "$UPLOAD_URL" == "null" ]; then
  echo "Error getting upload URL: $UPLOAD_URL_RESPONSE"
  exit 1
fi

# Stage 2: Upload the file to the provided URL
UPLOAD_RESPONSE=$(curl -s -X POST \
  -T "$FILE_PATH" \
  -H "Content-Type: application/octet-stream" \
  "$UPLOAD_URL")

if [ $? -ne 0 ]; then
  echo "Error uploading file: $UPLOAD_RESPONSE"
  exit 1
fi

# Stage 3: Complete the upload, and post the message and the file
COMPLETE_RESPONSE=$(curl -s -X POST \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json;charset=utf-8" \
  -d '{
        "files": [
          {
            "id": "'"$FILE_ID"'"
          }
        ],
        "channel_id": "'"$CHANNEL_ID"'",
        "initial_comment": "Hello file 001"
      }' \
  https://slack.com/api/files.completeUploadExternal)

if [ "$(echo "$COMPLETE_RESPONSE" | jq -r '.ok')" != "true" ]; then
  echo "Error completing upload: $COMPLETE_RESPONSE"
  exit 1
fi

# OPTIONAL Stage 4: Share the uploaded file in a channel, only if it was not performed in the previous stage
SHARE_RESPONSE=$(curl -s -X POST \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json;charset=utf-8" \
  -d '{
        "channel": "'"$CHANNEL_ID"'",
        "file": "'"$FILE_ID"'",
        "initial_comment": "This is File"
      }' \
  https://slack.com/api/files.sharedPublicURL)

if [ "$(echo "$SHARE_RESPONSE" | jq -r '.ok')" != "true" ]; then
  echo "Error sharing file: $SHARE_RESPONSE"
  exit 1
fi

echo "File successfully uploaded. Is not then check your token scopes"

 

https://stackoverflow.com/a/78933760

 

728x90
반응형