LoginSignup
1
0

More than 3 years have passed since last update.

outgoing webhooksから画像情報が取れないことを知る。

Last updated at Posted at 2019-07-10

outgoing webhooksから画像情報が取れない

本体がlambdaに乗っかるslackbotを作ってるときにoutgoing webhooksから画像情報が取れないことに気づきました。
試行錯誤の末、何とか小細工して解決できたので共有します。

outgoing webhooksのpostdata

token=XXXXXXXXXXXXXXXXXX
team_id=T0001
team_domain=example
channel_id=C2147483705
channel_name=test
thread_ts=1504640714.003543
timestamp=1504640775.000005
user_id=U2147483697
user_name=Steve
text=googlebot: What is the air-speed velocity of an unladen swallow?
trigger_word=googlebot:

textがあるならimageも欲しかったなぁ

files.listを呼ぶという二度手間で解決しました。

てな訳で画像を投稿→slackのfile.list呼ぶ→レスポンス.filesをtimestampの降順にソート→一番先頭の要素を取得という方法で画像情報の取得に成功しました。。。
(画像を投稿してから30秒ほど待たないとfile.listには反映されないのでご注意を)

def sort_data(file_data):
    """
    timestampが一番新しいファイルを返す。


    Parameters
    ----------
    file_data : str
        ファイルデータ
    """
    try:
        print("sort_data start")
        files = file_data["files"]

        timestamp = ""
        # timestampが一番新しいファイル
        latest_file = ""

        for i,file in enumerate(files):
            if i == 0:
                timestamp = files[i]["timestamp"]
                latest_file = files[i]
            else:
                if int(files[i]["timestamp"]) > int(timestamp):
                    latest_file = files[i]
                    timestamp = files[i]["timestamp"]
        print("sort_data end")
        return latest_file
    except Exception:
        raise

もっとスマートなやり方をご存知の方いらっしゃいましたら教えてください。。。

参考URL
・slack api
https://api.slack.com/methods/files.list
・slcak outgoingwebhooks
https://api.slack.com/custom-integrations/outgoing-webhooks

1
0
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
1
0