0

List Chats

by
Published today

List the one-on-one, group and meeting chats of the connected user, optionally with their members. Pass the returned @odata.nextLink as `next_link` for the next page. Requires Chat.ReadBasic (covered by the default Chat.ReadWrite).

Script ms_teams Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * List Chats
5
 * List the one-on-one, group and meeting chats of the connected user, optionally with their members. Pass the returned @odata.nextLink as `next_link` for the next page. Requires Chat.ReadBasic (covered by the default Chat.ReadWrite).
6
 */
7
export async function main(
8
  auth: RT.MsTeams,
9
  top: number = 50,
10
  expand_members: boolean = false,
11
  next_link: string | undefined
12
) {
13
  if (
14
    next_link != null &&
15
    next_link !== "" &&
16
    !next_link.startsWith("https://graph.microsoft.com/")
17
  ) {
18
    throw new Error(
19
      "next_link must be an @odata.nextLink returned by Microsoft Graph"
20
    )
21
  }
22
  let url: URL
23
  if (next_link != null && next_link !== "") {
24
    url = new URL(next_link)
25
  } else {
26
    url = new URL(`https://graph.microsoft.com/v1.0/me/chats`)
27
    url.searchParams.append("$top", String(top))
28
    if (expand_members) {
29
      url.searchParams.append("$expand", "members")
30
    }
31
  }
32

33
  const response = await fetch(url, {
34
    headers: {
35
      Authorization: `Bearer ${auth.token}`,
36
      Accept: "application/json",
37
    },
38
  })
39

40
  if (!response.ok) {
41
    throw new Error(`${response.status} ${await response.text()}`)
42
  }
43

44
  return await response.json()
45
}
46