Edits history of script submission #8990 for ' Reply to 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,
    	conversationId: string,
    	body: {
    		content: string
    		mentions?: { configurationId: string }[]
    		context: {
    			username: string
    			timezone: string
    			fullName: string | null
    			email: string | null
    			profilePictureUrl: string | null
    			origin?: 'slack' | 'web' | 'api'
    		}
    	}
    ) {
    	const endpoint = `https://dust.tt/api/v1/w/${resource.workspaceId}/assistant/conversations/${conversationId}/messages`
    
    	const requestBody = {
    		...body,
    		mentions: body.mentions || [] // default empty array if mentions is not provided
    	}
    
    	const defaultContext = {
    		fullName: null,
    		email: null,
    		profilePictureUrl: null
    	}
    
    	requestBody.context = {
    		...defaultContext,
    		...requestBody.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