LoginSignup
0
1

More than 3 years have passed since last update.

Slackのfiles.upload APIにはreply_broadcastが設定出来ないので

Posted at

今日もSlack Botについてのpostです。
表題の件ですが、APIドキュメントを見てみると分かるように、reply_broadcastオプションがありません。
アプリ上では出来ることが確認できるのですが。
Slack___000-waigaya___Leave_a_Nest_Co__ltd_.png

現状、Slack API経由では出来ないんですね。悲しい。

Workaround

毎度おなじみになりつつある @seratch さんに助けを求めてみた所、現状の実装としては
- ファイルアップする
- ファイルのURLを取得する
- ファイルアップしたpostを消す
- Chat.messageAPIを使ってpostし直す

という流れになるようです。

参考コードはこちらです

fileupload_reply_broadcast.py
import os
from slack_sdk import WebClient

client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))

# 親メッセージ
new_message = client.chat_postMessage(channel="#random", text="Hi")
channel_id, ts = new_message["channel"], new_message["message"]["ts"]

# ファイルを一旦アップロード
f = client.files_upload(content="this is a test", channels=channel_id, thread_ts=ts)
file_url = f["file"]["permalink"]

# ファイル共有のために投稿したメッセージを消し込み
file_upload_message_ts = f["file"]["shares"]["public"][channel_id][0]["ts"]
deletion = client.chat_delete(channel=channel_id, ts=file_upload_message_ts)

# ファイルを引用するメッセージを投稿
reply = client.chat_postMessage(
    channel=channel_id,
    thread_ts=ts,
    reply_broadcast=True,
    text=f"Here is your file: {file_url}",
)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1