0

Create conversation

by
Published Aug 26, 2024

Create a new conversation with a specific Dust assistant

Script dust Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 652 days ago
1
type Dust = {
2
	apiKey: string
3
	workspaceId: string
4
}
5

6
export async function main(
7
	resource: Dust,
8
	body: {
9
		message: {
10
			content: string
11
			mentions?: { configurationId: string }[]
12
			context: {
13
				username: string
14
				timezone: string
15
				fullName: string | null
16
				email: string | null
17
				profilePictureUrl: string | null
18
				origin?: 'slack' | 'web' | 'api'
19
			}
20
		}
21
		blocking?: boolean
22
		title: string | null
23
	}
24
) {
25
	const endpoint = `https://dust.tt/api/v1/w/${resource.workspaceId}/assistant/conversations`
26

27
	const requestBody = {
28
		...body,
29
		visibility: 'unlisted',
30
		message: {
31
			...body.message,
32
			mentions: body.message.mentions || [] // default empty array if mentions is not provided
33
		}
34
	}
35

36
	const defaultContext = {
37
		fullName: null,
38
		email: null,
39
		profilePictureUrl: null
40
	}
41

42
	requestBody.message.context = {
43
		...defaultContext,
44
		...requestBody.message.context
45
	}
46

47
	const response = await fetch(endpoint, {
48
		method: 'POST',
49
		headers: {
50
			accept: 'application/json',
51
			'Content-Type': 'application/json',
52
			Authorization: `Bearer ${resource.apiKey}`
53
		},
54
		body: JSON.stringify(requestBody)
55
	})
56

57
	if (!response.ok) {
58
		throw new Error(`HTTP error! status: ${response.status}`)
59
	}
60

61
	const data = await response.json()
62

63
	return data
64
}
65