0

Creates a conversation

by
Published Dec 20, 2024

You can create a conversation that has been initiated by a contact (ie.

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
 * Creates a conversation
8
 * You can create a conversation that has been initiated by a contact (ie.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	body: {
13
		from: { type: 'lead' | 'user' | 'contact'; id: string }
14
		body: string
15
	}
16
) {
17
	const url = new URL(`https://api.intercom.io/conversations`)
18

19
	const response = await fetch(url, {
20
		method: 'POST',
21
		headers: {
22
			'Intercom-Version': auth.apiVersion,
23
			'Content-Type': 'application/json',
24
			Authorization: 'Bearer ' + auth.token
25
		},
26
		body: JSON.stringify(body)
27
	})
28
	if (!response.ok) {
29
		const text = await response.text()
30
		throw new Error(`${response.status} ${text}`)
31
	}
32
	return await response.json()
33
}
34