0

Update conversation

by
Published Oct 17, 2025

Updates a conversation based on the unique conversation ID.

Script kustomer Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Kustomer = {
3
  apiKey: string;
4
};
5
/**
6
 * Update conversation
7
 * Updates a conversation based on the unique conversation ID.
8
 */
9
export async function main(
10
  auth: Kustomer,
11
  id: string,
12
  replace: string | undefined,
13
  body:
14
    | {
15
        externalId?: string;
16
        name?: string;
17
        direction?: "in" | "out";
18
        priority?: number;
19
        satisfaction?: number;
20
        satisfactionLevel?: {
21
          form?: string;
22
          formResponse?: string;
23
          createdAt: string;
24
          updatedAt?: string;
25
          status:
26
            | "scheduled"
27
            | "offered"
28
            | "rated"
29
            | "commented"
30
            | "canceled"
31
            | "unresponded";
32
          scheduledFor?: string;
33
          firstAnswer?: string;
34
          sentAt?: string;
35
          score?: 0 | 1;
36
          rating?: number;
37
          channel?:
38
            | "email"
39
            | "sms"
40
            | "chat"
41
            | "facebook"
42
            | "twitter-dm"
43
            | "twitter-tweet"
44
            | "voice"
45
            | "instagram"
46
            | "instagram-comment"
47
            | "whatsapp"
48
            | "form";
49
          sentBy?: string;
50
          sentByTeams?: string[];
51
        };
52
        suggestedShortcuts?: { shortcutId: string; confidence: number }[];
53
        status?: "open" | "snoozed" | "done";
54
        replyChannel?: string;
55
        subStatus?: string;
56
        snooze?: {
57
          time?: string;
58
          status: "scheduled" | "canceled" | "elapsed";
59
        };
60
        tags?: string[];
61
        suggestedTags?: { tag: string; confidence: number }[];
62
        sentiment?: { polarity: 0 | 1 | -1; confidence: number };
63
        assignedUsers?: string[];
64
        assignedTeams?: string[];
65
        custom?: {};
66
        deleted?: false | true;
67
        ended?: false | true;
68
        endedAt?: string;
69
        endedReason?: string;
70
        endedBy?: string;
71
        endedByType?: "user" | "customer";
72
        locked?: false | true;
73
        rev?: number;
74
        defaultLang?: string;
75
        queue?: {};
76
      }
77
    | { customer?: string },
78
) {
79
  const url = new URL(`https://api.kustomerapp.com/v1/conversations/${id}`);
80
  for (const [k, v] of [["replace", replace]]) {
81
    if (v !== undefined && v !== "" && k !== undefined) {
82
      url.searchParams.append(k, v);
83
    }
84
  }
85
  const response = await fetch(url, {
86
    method: "PUT",
87
    headers: {
88
      "Content-Type": "application/json",
89
      Authorization: "Bearer " + auth.apiKey,
90
    },
91
    body: JSON.stringify(body),
92
  });
93
  if (!response.ok) {
94
    const text = await response.text();
95
    throw new Error(`${response.status} ${text}`);
96
  }
97
  return await response.json();
98
}
99