0

Reply to conversation

by
Published Aug 26, 2024

Send reply to existing conversation

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
	conversationId: string,
9
	body: {
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
) {
22
	const endpoint = `https://dust.tt/api/v1/w/${resource.workspaceId}/assistant/conversations/${conversationId}/messages`
23

24
	const requestBody = {
25
		...body,
26
		mentions: body.mentions || [] // default empty array if mentions is not provided
27
	}
28

29
	const defaultContext = {
30
		fullName: null,
31
		email: null,
32
		profilePictureUrl: null
33
	}
34

35
	requestBody.context = {
36
		...defaultContext,
37
		...requestBody.context
38
	}
39

40
	const response = await fetch(endpoint, {
41
		method: 'POST',
42
		headers: {
43
			accept: 'application/json',
44
			'Content-Type': 'application/json',
45
			Authorization: `Bearer ${resource.apiKey}`
46
		},
47
		body: JSON.stringify(requestBody)
48
	})
49

50
	if (!response.ok) {
51
		throw new Error(`HTTP error! status: ${response.status}`)
52
	}
53

54
	const data = await response.json()
55

56
	return data
57
}
58