Edits history of script submission #8991 for ' Create conversation (dust)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Dust = {
    	apiKey: string
    	workspaceId: string
    }
    
    export async function main(
    	resource: Dust,
    	body: {
    		message: {
    			content: string
    			mentions?: { configurationId: string }[]
    			context: {
    				username: string
    				timezone: string
    				fullName: string | null
    				email: string | null
    				profilePictureUrl: string | null
    				origin?: 'slack' | 'web' | 'api'
    			}
    		}
    		blocking?: boolean
    		title: string | null
    	}
    ) {
    	const endpoint = `https://dust.tt/api/v1/w/${resource.workspaceId}/assistant/conversations`
    
    	const requestBody = {
    		...body,
    		visibility: 'unlisted',
    		message: {
    			...body.message,
    			mentions: body.message.mentions || [] // default empty array if mentions is not provided
    		}
    	}
    
    	const defaultContext = {
    		fullName: null,
    		email: null,
    		profilePictureUrl: null
    	}
    
    	requestBody.message.context = {
    		...defaultContext,
    		...requestBody.message.context
    	}
    
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			accept: 'application/json',
    			'Content-Type': 'application/json',
    			Authorization: `Bearer ${resource.apiKey}`
    		},
    		body: JSON.stringify(requestBody)
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 652 days ago