1 | |
2 | type Gorgias = { |
3 | username: string; |
4 | apiKey: string; |
5 | domain: string; |
6 | }; |
7 | |
8 | * Update a message |
9 | * |
10 | */ |
11 | export async function main( |
12 | auth: Gorgias, |
13 | ticket_id: string, |
14 | 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 | external_id?: string; |
48 | failed_datetime?: string; |
49 | from_agent: false | true; |
50 | message_id?: string; |
51 | receiver?: { id?: number; email?: string }; |
52 | sender?: { id?: number; email?: string }; |
53 | sent_datetime?: string; |
54 | source?: { |
55 | from?: { name?: string; address?: string }; |
56 | bcc?: { name?: string; address?: string }[]; |
57 | type?: string; |
58 | to?: { name?: string; address?: string }[]; |
59 | cc?: { name?: string; address?: string }[]; |
60 | }; |
61 | subject?: string; |
62 | via: |
63 | | "aircall" |
64 | | "api" |
65 | | "chat" |
66 | | "email" |
67 | | "facebook" |
68 | | "facebook-mention" |
69 | | "facebook-messenger" |
70 | | "facebook-recommendations" |
71 | | "help-center" |
72 | | "instagram-ad-comment" |
73 | | "instagram-comment" |
74 | | "instagram-mention" |
75 | | "instagram-direct-message" |
76 | | "internal-note" |
77 | | "phone" |
78 | | "sms" |
79 | | "twitter" |
80 | | "twitter-direct-message" |
81 | | "yotpo-review" |
82 | | "twilio" |
83 | | "zendesk" |
84 | | "form" |
85 | | "self_service" |
86 | | "yotpo" |
87 | | "instagram" |
88 | | "shopify" |
89 | | "gorgias_chat" |
90 | | "rule" |
91 | | "helpdesk"; |
92 | }, |
93 | ) { |
94 | const url = new URL( |
95 | `https://${auth.domain}.gorgias.com/api/tickets/${ticket_id}/messages/${id}/`, |
96 | ); |
97 | for (const [k, v] of [["action", action]]) { |
98 | if (v !== undefined && v !== "" && k !== undefined) { |
99 | url.searchParams.append(k, v); |
100 | } |
101 | } |
102 | const response = await fetch(url, { |
103 | method: "PUT", |
104 | headers: { |
105 | "Content-Type": "application/json", |
106 | Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`), |
107 | }, |
108 | body: JSON.stringify(body), |
109 | }); |
110 | if (!response.ok) { |
111 | const text = await response.text(); |
112 | throw new Error(`${response.status} ${text}`); |
113 | } |
114 | return await response.json(); |
115 | } |
116 |
|