0

Retrieve a conversation

by
Published Dec 20, 2024

You can fetch the details of a single conversation.

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