0

Send an image to channel

by
Published Aug 16, 2022
Script slack Verified

The script

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

5
type Slack = {
6
  token: string;
7
};
8

9
/**
10
 * Send an image to channel
11
 * Upload an image from Windmill object storage to a Slack channel.
12
 */
13
export async function main(
14
  slack: Slack,
15
  channel: string,
16
  image: S3Object,
17
  imagename: string = "image.png",
18
) {
19
  const bytes = await wmill.loadS3File(image);
20
  if (!bytes) {
21
    throw new Error("Could not load the file from object storage");
22
  }
23

24
  const client = new WebClient(slack.token);
25
  return await client.filesUploadV2({
26
    channel_id: channel,
27
    file: Buffer.from(bytes),
28
    filename: imagename,
29
  });
30
}
31

Other submissions
  • Submitted by admin Deno
    Created 384 days ago
    1
    import type { Base64 } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { decode } from "https://deno.land/[email protected]/encoding/base64.ts";
    3
    
    
    4
    type Slack = {
    5
      token: string;
    6
    };
    7
    export async function main(
    8
      image: Base64,
    9
      channel: string,
    10
      slack: Slack,
    11
      imagename: string = "image.png",
    12
    ) {
    13
      const formData = new FormData();
    14
      formData.append("token", slack.token);
    15
      formData.append("file", new File([decode(image)], imagename));
    16
      formData.append("channels", channel);
    17
      formData.append("filename", "image.png");
    18
      formData.append("filetype", imagename);
    19
    
    
    20
      return await (
    21
        await fetch("https://slack.com/api/files.upload", {
    22
          method: "POST",
    23
          body: formData,
    24
        })
    25
      ).json();
    26
    }
    27