0

List all conversations

by
Published Dec 20, 2024

You can fetch a list of all conversations.

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
 * List all conversations
8
 * You can fetch a list of all conversations.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	per_page: string | undefined,
13
	starting_after: string | undefined
14
) {
15
	const url = new URL(`https://api.intercom.io/conversations`)
16
	for (const [k, v] of [
17
		['per_page', per_page],
18
		['starting_after', starting_after]
19
	]) {
20
		if (v !== undefined && v !== '' && k !== undefined) {
21
			url.searchParams.append(k, v)
22
		}
23
	}
24
	const response = await fetch(url, {
25
		method: 'GET',
26
		headers: {
27
			'Intercom-Version': auth.apiVersion,
28
			Authorization: 'Bearer ' + auth.token
29
		},
30
		body: undefined
31
	})
32
	if (!response.ok) {
33
		const text = await response.text()
34
		throw new Error(`${response.status} ${text}`)
35
	}
36
	return await response.json()
37
}
38