0

Reply to a conversation

by
Published Dec 20, 2024

You can reply to a conversation with a message from an admin or on behalf of a contact, or with a note for admins.

Script intercom Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Intercom = {
3
	apiVersion: string
4
	token: string
5
}
6
/**
7
 * Reply to a conversation
8
 * You can reply to a conversation with a message from an admin or on behalf of a contact, or with a note for admins.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	id: string,
13
	body:
14
		| {
15
				message_type: 'comment'
16
				type: 'user'
17
				body: string
18
				created_at?: number
19
				attachment_urls?: string[]
20
		  }
21
		| {
22
				message_type: 'comment'
23
				type: 'user'
24
				body: string
25
				created_at?: number
26
				attachment_urls?: string[]
27
		  }
28
		| {
29
				message_type: 'comment'
30
				type: 'user'
31
				body: string
32
				created_at?: number
33
				attachment_urls?: string[]
34
		  }
35
		| {
36
				message_type: 'comment' | 'note'
37
				type: 'admin'
38
				body?: string
39
				admin_id: string
40
				created_at?: number
41
				attachment_urls?: string[]
42
				attachment_files?: {
43
					content_type?: string
44
					data?: string
45
					name?: string
46
				}[]
47
		  }
48
) {
49
	const url = new URL(`https://api.intercom.io/conversations/${id}/reply`)
50

51
	const response = await fetch(url, {
52
		method: 'POST',
53
		headers: {
54
			'Intercom-Version': auth.apiVersion,
55
			'Content-Type': 'application/json',
56
			Authorization: 'Bearer ' + auth.token
57
		},
58
		body: JSON.stringify(body)
59
	})
60
	if (!response.ok) {
61
		const text = await response.text()
62
		throw new Error(`${response.status} ${text}`)
63
	}
64
	return await response.json()
65
}
66