0

Create Task Comment

by
Published Oct 31, 2023

Add a comment to a task. `html_text` takes Asana rich text (wrapped in `<body>`) and replaces `text` when set.

Script asana Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Create Task Comment
5
 * Add a comment to a task. `html_text` takes Asana rich text (wrapped in `<body>`) and replaces `text` when set.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  task_gid: string,
10
  text: string | undefined,
11
  html_text: string | undefined,
12
  is_pinned: boolean | undefined
13
) {
14
  const response = await fetch(
15
    `https://app.asana.com/api/1.0/tasks/${task_gid}/stories`,
16
    {
17
      method: "POST",
18
      headers: {
19
        Authorization: `Bearer ${auth.token}`,
20
        "Content-Type": "application/json",
21
        Accept: "application/json",
22
      },
23
      body: JSON.stringify({
24
        data: {
25
          text: html_text ? undefined : text,
26
          html_text: html_text || undefined,
27
          is_pinned: is_pinned || undefined,
28
        },
29
      }),
30
    }
31
  )
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  return await response.json()
38
}
39

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Create a story on a task
    6
     * Adds a story to a task. This endpoint currently only allows for comment
    7
    stories to be created. The comment will be authored by the currently
    8
    authenticated user, and timestamped when the server receives the request.
    9
    
    
    10
    Returns the full record for the new story added to the task.
    11
     */
    12
    export async function main(
    13
      auth: Asana,
    14
      task_gid: string,
    15
      opt_pretty: string | undefined,
    16
      opt_fields: string | undefined,
    17
      body: {
    18
        data?: { gid?: string; resource_type?: string; [k: string]: unknown } & {
    19
          created_at?: string;
    20
          html_text?: string;
    21
          is_pinned?: boolean;
    22
          resource_subtype?: string;
    23
          sticker_name?:
    24
            | "green_checkmark"
    25
            | "people_dancing"
    26
            | "dancing_unicorn"
    27
            | "heart"
    28
            | "party_popper"
    29
            | "people_waving_flags"
    30
            | "splashing_narwhal"
    31
            | "trophy"
    32
            | "yeti_riding_unicorn"
    33
            | "celebrating_people"
    34
            | "determined_climbers"
    35
            | "phoenix_spreading_love";
    36
          text?: string;
    37
          [k: string]: unknown;
    38
        };
    39
        [k: string]: unknown;
    40
      }
    41
    ) {
    42
      const url = new URL(
    43
        `https://app.asana.com/api/1.0/tasks/${task_gid}/stories`
    44
      );
    45
      for (const [k, v] of [
    46
        ["opt_pretty", opt_pretty],
    47
        ["opt_fields", opt_fields],
    48
      ]) {
    49
        if (v !== undefined && v !== "") {
    50
          url.searchParams.append(k, v);
    51
        }
    52
      }
    53
      const response = await fetch(url, {
    54
        method: "POST",
    55
        headers: {
    56
          "Content-Type": "application/json",
    57
          Authorization: "Bearer " + auth.token,
    58
        },
    59
        body: JSON.stringify(body),
    60
      });
    61
      if (!response.ok) {
    62
        const text = await response.text();
    63
        throw new Error(`${response.status} ${text}`);
    64
      }
    65
      return await response.json();
    66
    }
    67