0

Create a message

by
Published Oct 17, 2025

Create a message and either: - not send it if the attribute `sent_datetime` is filled.

Script gorgias Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gorgias = {
3
  username: string;
4
  apiKey: string;
5
  domain: string;
6
};
7
/**
8
 * Create a message
9
 * Create a message and either:
10
  - not send it if the attribute `sent_datetime` is filled.
11
 */
12
export async function main(
13
  auth: Gorgias,
14
  ticket_id: string,
15
  action: "force" | "retry" | "cancel" | undefined,
16
  body: {
17
    attachments?: {
18
      content_type: string;
19
      extra?: {};
20
      size: number;
21
      name: string;
22
      url: string;
23
      public?: false | true;
24
    }[];
25
    body_html?: string;
26
    body_text?: string;
27
    channel:
28
      | "aircall"
29
      | "api"
30
      | "chat"
31
      | "email"
32
      | "facebook"
33
      | "facebook-mention"
34
      | "facebook-messenger"
35
      | "facebook-recommendations"
36
      | "help-center"
37
      | "instagram-ad-comment"
38
      | "instagram-comment"
39
      | "instagram-mention"
40
      | "instagram-direct-message"
41
      | "internal-note"
42
      | "phone"
43
      | "sms"
44
      | "twitter"
45
      | "twitter-direct-message"
46
      | "yotpo-review";
47
    created_datetime?: string;
48
    external_id?: string;
49
    failed_datetime?: string;
50
    from_agent: false | true;
51
    message_id?: string;
52
    receiver?: { id?: number; email?: string };
53
    sender?: { id?: number; email?: string };
54
    sent_datetime?: string;
55
    source: {
56
      from?: { name?: string; address?: string };
57
      bcc?: { name?: string; address?: string }[];
58
      type?: string;
59
      to?: { name?: string; address?: string }[];
60
      cc?: { name?: string; address?: string }[];
61
    };
62
    subject?: string;
63
    via:
64
      | "aircall"
65
      | "api"
66
      | "chat"
67
      | "email"
68
      | "facebook"
69
      | "facebook-mention"
70
      | "facebook-messenger"
71
      | "facebook-recommendations"
72
      | "help-center"
73
      | "instagram-ad-comment"
74
      | "instagram-comment"
75
      | "instagram-mention"
76
      | "instagram-direct-message"
77
      | "internal-note"
78
      | "phone"
79
      | "sms"
80
      | "twitter"
81
      | "twitter-direct-message"
82
      | "yotpo-review"
83
      | "twilio"
84
      | "zendesk"
85
      | "form"
86
      | "self_service"
87
      | "yotpo"
88
      | "instagram"
89
      | "shopify"
90
      | "gorgias_chat"
91
      | "rule"
92
      | "helpdesk";
93
  },
94
) {
95
  const url = new URL(
96
    `https://${auth.domain}.gorgias.com/api/tickets/${ticket_id}/messages`,
97
  );
98
  for (const [k, v] of [["action", action]]) {
99
    if (v !== undefined && v !== "" && k !== undefined) {
100
      url.searchParams.append(k, v);
101
    }
102
  }
103
  const response = await fetch(url, {
104
    method: "POST",
105
    headers: {
106
      "Content-Type": "application/json",
107
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
108
    },
109
    body: JSON.stringify(body),
110
  });
111
  if (!response.ok) {
112
    const text = await response.text();
113
    throw new Error(`${response.status} ${text}`);
114
  }
115
  return await response.json();
116
}
117