0

Create a message

by
Published Dec 20, 2024

You can create a message that has been initiated by an admin.

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
 * Create a message
8
 * You can create a message that has been initiated by an admin.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	body:
13
		| ({} & {
14
				message_type?: 'in_app' | 'email'
15
				subject?: string
16
				body?: string
17
				template?: string
18
				from?: { type: 'admin'; id: number }
19
				to?: { type: 'user' | 'lead'; id: string }
20
				created_at?: number
21
				create_conversation_without_contact_reply?: false | true
22
		  })
23
		| ({} & {
24
				message_type?: 'in_app' | 'email'
25
				subject?: string
26
				body?: string
27
				template?: string
28
				from?: { type: 'admin'; id: number }
29
				to?: { type: 'user' | 'lead'; id: string }
30
				created_at?: number
31
				create_conversation_without_contact_reply?: false | true
32
		  })
33
) {
34
	const url = new URL(`https://api.intercom.io/messages`)
35

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