0

Attach a contact to a conversation

by
Published Dec 20, 2024

You can add participants who are contacts to a conversation, on behalf of either another contact or an admin. {% admonition type="attention" name="Contacts without an email" %} If you add a contact via the email parameter and there is no user/lead found on that workspace with he given email, then we will create a new contact with `role` set to `lead`. {% /admonition %}

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
 * Attach a contact to a conversation
8
 * You can add participants who are contacts to a conversation, on behalf of either another contact or an admin.
9

10
{% admonition type="attention" name="Contacts without an email" %}
11
If you add a contact via the email parameter and there is no user/lead found on that workspace with he given email, then we will create a new contact with `role` set to `lead`.
12
{% /admonition %}
13

14

15
 */
16
export async function main(
17
	auth: Intercom,
18
	id: string,
19
	body: {
20
		admin_id?: string
21
		customer?:
22
			| {
23
					intercom_user_id: string
24
					customer?: { intercom_user_id: string } | { user_id: string } | { email: string }
25
			  }
26
			| {
27
					user_id: string
28
					customer?: { intercom_user_id: string } | { user_id: string } | { email: string }
29
			  }
30
			| {
31
					email: string
32
					customer?: { intercom_user_id: string } | { user_id: string } | { email: string }
33
			  }
34
	}
35
) {
36
	const url = new URL(`https://api.intercom.io/conversations/${id}/customers`)
37

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