0

Upload Ticket Attachments

by
Published Nov 7, 2023

Upload files from Windmill object storage to Zendesk and return one upload token per file. Attach them to a ticket by passing the tokens as `upload_tokens` to Update Ticket (or `comment.uploads` in Create Ticket's `fields`). Tokens expire after 60 minutes if unused. Each file keeps the name at the end of its storage path.

Script zendesk Verified

The script

Submitted by hugo989 Bun
Verified 21 days ago
1
import * as wmill from "windmill-client"
2
import { S3Object } from "windmill-client"
3

4
/**
5
 * Upload Ticket Attachments
6
 * Upload files from Windmill object storage to Zendesk and return one upload token per file. Attach them to a ticket by passing the tokens as `upload_tokens` to Update Ticket (or `comment.uploads` in Create Ticket's `fields`). Tokens expire after 60 minutes if unused. Each file keeps the name at the end of its storage path.
7
 */
8
export async function main(auth: RT.Zendesk, files: S3Object[]) {
9
  const tokens: string[] = []
10
  for (const file of files) {
11
    const bytes = await wmill.loadS3File(file)
12
    if (!bytes) {
13
      throw new Error(`Could not load ${file.s3} from object storage`)
14
    }
15
    const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/uploads`)
16
    url.searchParams.append("filename", file.s3.split("/").pop()!)
17

18
    const response = await fetch(url, {
19
      method: "POST",
20
      headers: {
21
        Authorization: auth.token
22
          ? `Bearer ${auth.token}`
23
          : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
24
        "Content-Type": "application/binary",
25
        Accept: "application/json",
26
      },
27
      body: bytes,
28
    })
29

30
    if (!response.ok) {
31
      throw new Error(`${response.status} ${await response.text()}`)
32
    }
33

34
    const { upload } = await response.json()
35
    tokens.push(upload.token)
36
  }
37

38
  return { tokens }
39
}
40

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 548 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * Upload Files
    8
     * Uploads a file that can be attached to a ticket comment.
    9
     */
    10
    export async function main(auth: Zendesk) {
    11
      const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/uploads`);
    12
    
    
    13
      const response = await fetch(url, {
    14
        method: "POST",
    15
        headers: {
    16
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    17
        },
    18
        body: undefined,
    19
      });
    20
      if (!response.ok) {
    21
        const text = await response.text();
    22
        throw new Error(`${response.status} ${text}`);
    23
      }
    24
      return await response.json();
    25
    }
    26