0

Download File

by
Published today

Download a Slack file's content into Windmill object storage.

Script slack Verified

The script

Submitted by hugo989 Bun
Verified 1 hour ago
1
import { WebClient } from "@slack/web-api"
2
import * as wmill from "windmill-client"
3
import { S3Object } from "windmill-client"
4

5
/**
6
 * Download File
7
 * Download a file's content by ID (e.g. from a message's `files` or List Files) into Windmill object storage, at `output_s3_path` or a generated key. Returns the S3 reference plus the file's metadata. Needs files:read (user-token default; tick it for a bot token, which also only sees files shared where it is a member).
8
 */
9
export async function main(
10
  auth: RT.Slack,
11
  file: string,
12
  output_s3_path?: string
13
) {
14
  const client = new WebClient(auth.token)
15
  const { file: info } = await client.files.info({ file })
16

17
  const response = await fetch(info!.url_private_download!, {
18
    headers: { Authorization: `Bearer ${auth.token}` },
19
  })
20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`)
22
  }
23

24
  const s3: S3Object = await wmill.writeS3File(
25
    output_s3_path ? { s3: output_s3_path } : undefined,
26
    await response.blob(),
27
    undefined,
28
    info!.mimetype
29
  )
30
  return { s3, file: info }
31
}
32