0

Search conversations

by
Published Dec 20, 2024

You can search for multiple conversations by the value of their attributes in order to fetch exactly which ones you want.

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
 * Search conversations
8
 * You can search for multiple conversations by the value of their attributes in order to fetch exactly which ones you want.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	body: {
13
		query:
14
			| {
15
					field?: string
16
					operator?: '=' | '!=' | 'IN' | 'NIN' | '<' | '>' | '~' | '!~' | '^' | '$'
17
					value?: string
18
			  }
19
			| {
20
					operator?: 'AND' | 'OR'
21
					value?:
22
						| {}[]
23
						| {
24
								field?: string
25
								operator?: '=' | '!=' | 'IN' | 'NIN' | '<' | '>' | '~' | '!~' | '^' | '$'
26
								value?: string
27
						  }[]
28
			  }
29
		pagination?: { per_page?: number; starting_after?: string }
30
	}
31
) {
32
	const url = new URL(`https://api.intercom.io/conversations/search`)
33

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