0

Manage a conversation

by
Published Dec 20, 2024

For managing conversations you can: - Close a conversation - Snooze a conversation to reopen on a future date - Open a conversation which is `snoozed` or `closed` - Assign a conversation to an admin and/or team.

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
 * Manage a conversation
8
 * For managing conversations you can:
9
- Close a conversation
10
- Snooze a conversation to reopen on a future date
11
- Open a conversation which is `snoozed` or `closed`
12
- Assign a conversation to an admin and/or team.
13

14
 */
15
export async function main(
16
	auth: Intercom,
17
	id: string,
18
	body:
19
		| { message_type: 'close'; type: 'admin'; admin_id: string; body?: string }
20
		| { message_type: 'snoozed'; admin_id: string; snoozed_until: number }
21
		| { message_type: 'open'; admin_id: string }
22
		| {
23
				message_type: 'assignment'
24
				type: 'admin' | 'team'
25
				admin_id: string
26
				assignee_id: string
27
				body?: string
28
		  }
29
) {
30
	const url = new URL(`https://api.intercom.io/conversations/${id}/parts`)
31

32
	const response = await fetch(url, {
33
		method: 'POST',
34
		headers: {
35
			'Intercom-Version': auth.apiVersion,
36
			'Content-Type': 'application/json',
37
			Authorization: 'Bearer ' + auth.token
38
		},
39
		body: JSON.stringify(body)
40
	})
41
	if (!response.ok) {
42
		const text = await response.text()
43
		throw new Error(`${response.status} ${text}`)
44
	}
45
	return await response.json()
46
}
47