1 | |
2 |
|
3 | export type DynSelect_chat_id = string |
4 |
|
5 | |
6 | export async function chat_id(auth: RT.MsTeams) { |
7 | const response = await fetch( |
8 | `https://graph.microsoft.com/v1.0/me/chats?$expand=members&$top=50`, |
9 | { |
10 | headers: { |
11 | Authorization: `Bearer ${auth.token}`, |
12 | Accept: "application/json", |
13 | }, |
14 | } |
15 | ) |
16 | if (!response.ok) { |
17 | throw new Error(`${response.status} ${await response.text()}`) |
18 | } |
19 | const { value } = (await response.json()) as { value: any[] } |
20 | return value.map((x) => ({ |
21 | value: x.id, |
22 | label: |
23 | x.topic || |
24 | (x.members ?? []) |
25 | .map((m: any) => m.displayName) |
26 | .filter(Boolean) |
27 | .join(", ") || |
28 | x.chatType, |
29 | })) |
30 | } |
31 |
|
32 | |
33 | * List Chat Messages |
34 | * List the messages of a chat, newest first. Pass the returned @odata.nextLink as `next_link` for the next page. Requires Chat.Read (covered by the default Chat.ReadWrite). |
35 | */ |
36 | export async function main( |
37 | auth: RT.MsTeams, |
38 | chat_id: DynSelect_chat_id, |
39 | top: number = 50, |
40 | next_link: string | undefined |
41 | ) { |
42 | if ( |
43 | next_link != null && |
44 | next_link !== "" && |
45 | !next_link.startsWith("https://graph.microsoft.com/") |
46 | ) { |
47 | throw new Error( |
48 | "next_link must be an @odata.nextLink returned by Microsoft Graph" |
49 | ) |
50 | } |
51 | let url: URL |
52 | if (next_link != null && next_link !== "") { |
53 | url = new URL(next_link) |
54 | } else { |
55 | url = new URL( |
56 | `https://graph.microsoft.com/v1.0/chats/${encodeURIComponent(chat_id)}/messages` |
57 | ) |
58 | url.searchParams.append("$top", String(top)) |
59 | url.searchParams.append("$orderby", "createdDateTime desc") |
60 | } |
61 |
|
62 | const response = await fetch(url, { |
63 | headers: { |
64 | Authorization: `Bearer ${auth.token}`, |
65 | Accept: "application/json", |
66 | }, |
67 | }) |
68 |
|
69 | if (!response.ok) { |
70 | throw new Error(`${response.status} ${await response.text()}`) |
71 | } |
72 |
|
73 | return await response.json() |
74 | } |
75 |
|