0

Add Comment to Thread

by
Published today

Add an internal comment to a conversation thread.

Script hubspot Verified

The script

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

3
/**
4
 * Add Comment to Thread
5
 * Add an internal comment to a conversation thread. Comments are visible to your team only, never to the visitor. Requires conversations.write.
6
 */
7
export async function main(
8
  auth: RT.Hubspot,
9
  thread_id: string,
10
  text: string,
11
  rich_text: string | undefined,
12
  file_ids: string[] | undefined
13
) {
14
  const body: { [key: string]: any } = { type: "COMMENT", text }
15
  if (rich_text != null && rich_text !== "") body.richText = rich_text
16
  if (file_ids?.length) {
17
    body.attachments = file_ids.map((fileId) => ({ fileId, type: "FILE" }))
18
  }
19

20
  const response = await fetch(
21
    `https://api.hubapi.com/conversations/v3/conversations/threads/${thread_id}/messages`,
22
    {
23
      method: "POST",
24
      headers: {
25
        Authorization: `Bearer ${auth.token}`,
26
        "Content-Type": "application/json",
27
        Accept: "application/json",
28
      },
29
      body: JSON.stringify(body),
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