DOCS/troubleshooting/251014_slack_lists_file_attachment.md

3.3 KiB

Slack Lists 첨부파일 업로드 구현

날짜: 2025-10-14 작성자: happybell80 관련 파일: rb8001/app/scheduler/jobs/coldmail_briefing.py 전체 시나리오: 251014_coldmail_ir_analysis_scenario.md


문제 상황

coldmail_briefing.py:207에서 document_id를 attachment에 전달하지만, Slack Lists API는 file_id 필요


현재 상태

  • skill-rag-file/app/main.py:54-55: upload, search만 등록, download 없음
  • skill-slack/app/api/init.py:4: files import 없음

해결 방안

1단계: skill-rag-file 다운로드 API 추가

파일: skill-rag-file/app/api/download.py (신규) import: upload.py:1, 10, 12 참고 (FastAPI, get_db, TeamDocument) router: @router.get("/download/{document_id}") DB 조회: upload.py:60-65 패턴 사용

  • select(TeamDocument).where(TeamDocument.id == document_id)
  • db.execute(stmt), result.scalar_one_or_none()
  • 없으면 HTTPException(404) 반환: FileResponse(document.storage_path, media_type="application/pdf") 등록: skill-rag-file/app/main.py:55 이후
  • from app.api import upload, search, download
  • app.include_router(download.router, prefix="/api", tags=["download"])

2단계: skill-slack 파일 업로드 API 추가

파일: skill-slack/app/api/endpoints/files.py (신규) import: lists.py:4-9 참고 (FastAPI, settings, verify_api_key)

  • from slack_sdk import WebClient (test_lists_with_file.py:4) router: @router.post("/files/upload", dependencies=[Depends(verify_api_key)]) 파라미터: file (UploadFile), title (str), token (Optional[str]) 업로드: test_lists_with_file.py:18, 21-24 패턴
  • client = WebClient(token or settings.SLACK_BOT_TOKEN)
  • 임시 파일 저장 → client.files_upload_v2(file=temp_path, title=title)
  • 업로드 후 임시 파일 삭제 반환: {"file_id": result["file"]["id"], "url_private": result["file"]["url_private"]} 에러: HTTPException(400 or 500) 등록: skill-slack/app/api/init.py:4, 13 수정

3단계: coldmail_briefing.py 통합

환경변수 추가 (coldmail_briefing.py:19 이후):

  • SKILL_RAG_FILE_URL = os.getenv("SKILL_RAG_FILE_URL", "http://localhost:8508")

파일 변환 로직 (coldmail_briefing.py:189 이후, 191 이전):

  • GET {SKILL_RAG_FILE_URL}/api/download/{document_ids[0]} → pdf_content
  • aiohttp.FormData(): file, title 추가
  • POST {SKILL_SLACK_URL}/api/v1/files/upload → file_id
  • 실패 시 file_id = None, logger.error()

attachment 필드 수정 (coldmail_briefing.py:207):

  • 변경 전: [f"document_{doc_id}" for doc_id in document_ids][:1]
  • 변경 후: [file_id] if file_id else []

테스트 완료

  • 워크스페이스: T0925SXPS4D, 리스트: F09J1HPPQJG
  • files_upload_v2(file, title) 성공 (channel 파라미터 불필요)
  • attachment: [file_id] 전달 확인
  • 테스트: skill-slack/tests/test_lists_with_file.py

교훈

files_upload_v2 channel 파라미터

  • channel 없이도 업로드 가능하나 문서 미확인
  • 교훈: 테스트 코드 주석 우선 확인

리스트 컬럼 구조

  • F09J1HPPQJG(테스트) ≠ F09L4S2C6BG(운영)
  • 교훈: 운영 배포 전 실제 컬럼 ID 확인 필수