Edits history of script submission #23265 for ' Download File (slack)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { WebClient } from "@slack/web-api"
    import * as wmill from "windmill-client"
    import { S3Object } from "windmill-client"
    
    /**
     * Download File
     * 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).
     */
    export async function main(
      auth: RT.Slack,
      file: string,
      output_s3_path?: string
    ) {
      const client = new WebClient(auth.token)
      const { file: info } = await client.files.info({ file })
    
      const response = await fetch(info!.url_private_download!, {
        headers: { Authorization: `Bearer ${auth.token}` },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const s3: S3Object = await wmill.writeS3File(
        output_s3_path ? { s3: output_s3_path } : undefined,
        await response.blob(),
        undefined,
        info!.mimetype
      )
      return { s3, file: info }
    }
    

    Submitted by hugo989 2 hours ago