0

Create message from conversation

by
Published Oct 17, 2025

Creates a new message from a conversation.

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

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