0

Update a news item

by
Published Dec 20, 2024
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
 * Update a news item
8
 *
9
 */
10
export async function main(
11
	auth: Intercom,
12
	id: string,
13
	body: {
14
		title: string
15
		body?: string
16
		sender_id: number
17
		state?: 'draft' | 'live'
18
		deliver_silently?: false | true
19
		labels?: string[]
20
		reactions?: string[]
21
		newsfeed_assignments?: { newsfeed_id?: number; published_at?: number }[]
22
	}
23
) {
24
	const url = new URL(`https://api.intercom.io/news/news_items/${id}`)
25

26
	const response = await fetch(url, {
27
		method: 'PUT',
28
		headers: {
29
			'Intercom-Version': auth.apiVersion,
30
			'Content-Type': 'application/json',
31
			Authorization: 'Bearer ' + auth.token
32
		},
33
		body: JSON.stringify(body)
34
	})
35
	if (!response.ok) {
36
		const text = await response.text()
37
		throw new Error(`${response.status} ${text}`)
38
	}
39
	return await response.json()
40
}
41