0

Create message

by
Published Oct 17, 2025

Adds an instance of a message to an existing conversation timeline with a customer.

Script kustomer Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Kustomer = {
3
  apiKey: string;
4
};
5
/**
6
 * Create message
7
 * Adds an instance of a message to an existing conversation timeline with a customer.
8
 */
9
export async function main(
10
  auth: Kustomer,
11
  body: {
12
    id?: string;
13
    externalId?: string;
14
    customer?: string;
15
    conversation?: string;
16
    reference?: { message: string; type: "reply" | "replyAll" | "forward" };
17
    channel:
18
      | "email"
19
      | "sms"
20
      | "chat"
21
      | "facebook"
22
      | "twitter-dm"
23
      | "twitter-tweet"
24
      | "voice"
25
      | "instagram"
26
      | "whatsapp";
27
    preview?: string;
28
    subject?: string;
29
    direction: "in" | "out";
30
    app: string;
31
    size?: number;
32
    related?: string;
33
    status?: "sent" | "received" | "error";
34
    error?: {
35
      status?: number;
36
      code?: string;
37
      title?: string;
38
      detail?: string;
39
      source?: {};
40
      meta?: {};
41
      links?: {};
42
    };
43
    errorAt?: string;
44
    auto?: false | true;
45
    sentAt?: string;
46
    source?: "bulk" | "satisfaction";
47
    shortcuts?: string[];
48
    kbArticles?: string[];
49
    attachments?:
50
      | string[]
51
      | {
52
          _id: string;
53
          name: string;
54
          contentType: string;
55
          contentLength: number;
56
          sourceId?: string;
57
        }[];
58
    location?: {
59
      name?: string;
60
      address?: string;
61
      address2?: string;
62
      address3?: string;
63
      latitude?: number;
64
      longitude?: number;
65
      countryCode?: string;
66
      countryName?: string;
67
      regionCode?: string;
68
      regionName?: string;
69
      cityName?: string;
70
      zipCode?: string;
71
      areaCode?: string;
72
    };
73
    meta?: {};
74
    custom?: {};
75
    sentiment?: { polarity: 0 | 1 | -1; confidence: number };
76
    createdAt?: string;
77
    modifiedAt?: string;
78
    createdBy?: string;
79
    modifiedBy?: string;
80
    importedAt?: string;
81
    lang?: string;
82
    queue?: {};
83
  },
84
) {
85
  const url = new URL(`https://api.kustomerapp.com/v1/messages`);
86

87
  const response = await fetch(url, {
88
    method: "POST",
89
    headers: {
90
      "Content-Type": "application/json",
91
      Authorization: "Bearer " + auth.apiKey,
92
    },
93
    body: JSON.stringify(body),
94
  });
95
  if (!response.ok) {
96
    const text = await response.text();
97
    throw new Error(`${response.status} ${text}`);
98
  }
99
  return await response.json();
100
}
101