Edits history of script submission #11095 for ' Attach a contact to a conversation (intercom)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Intercom = {
    	apiVersion: string
    	token: string
    }
    /**
     * Attach a contact to a conversation
     * 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 %}
    
    
     */
    export async function main(
    	auth: Intercom,
    	id: string,
    	body: {
    		admin_id?: string
    		customer?:
    			| {
    					intercom_user_id: string
    					customer?: { intercom_user_id: string } | { user_id: string } | { email: string }
    			  }
    			| {
    					user_id: string
    					customer?: { intercom_user_id: string } | { user_id: string } | { email: string }
    			  }
    			| {
    					email: string
    					customer?: { intercom_user_id: string } | { user_id: string } | { email: string }
    			  }
    	}
    ) {
    	const url = new URL(`https://api.intercom.io/conversations/${id}/customers`)
    
    	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