Edits history of script submission #11103 for ' Create a news item (intercom)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Intercom = {
    	apiVersion: string
    	token: string
    }
    /**
     * Create a news item
     * You can create a news item
     */
    export async function main(
    	auth: Intercom,
    	body: {
    		title: string
    		body?: string
    		sender_id: number
    		state?: 'draft' | 'live'
    		deliver_silently?: false | true
    		labels?: string[]
    		reactions?: string[]
    		newsfeed_assignments?: { newsfeed_id?: number; published_at?: number }[]
    	}
    ) {
    	const url = new URL(`https://api.intercom.io/news/news_items`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Intercom-Version': auth.apiVersion,
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.token
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 536 days ago