0

List Channel Messages

by
Published today

List the root messages of a channel, most recently active thread first, optionally with their replies. Pass the returned @odata.nextLink as `next_link` for the next page. Requires ChannelMessage.Read.All, which needs admin consent and is not a connection default: tick it when connecting.

Script ms_teams Verified

The script

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

3
export type DynSelect_team_id = string
4

5
// Dropdown of the teams the connected user has joined.
6
export async function team_id(auth: RT.MsTeams) {
7
  const response = await fetch(
8
    `https://graph.microsoft.com/v1.0/me/joinedTeams`,
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) => ({ value: x.id, label: x.displayName }))
21
}
22

23
export type DynSelect_channel_id = string
24

25
// Dropdown of the chosen team's channels.
26
export async function channel_id(auth: RT.MsTeams, team_id: string) {
27
  if (team_id == null || team_id === "") return []
28
  const response = await fetch(
29
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels`,
30
    {
31
      headers: {
32
        Authorization: `Bearer ${auth.token}`,
33
        Accept: "application/json",
34
      },
35
    }
36
  )
37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40
  const { value } = (await response.json()) as { value: any[] }
41
  return value.map((x) => ({ value: x.id, label: x.displayName }))
42
}
43

44
/**
45
 * List Channel Messages
46
 * List the root messages of a channel, most recently active thread first, optionally with their replies. Pass the returned @odata.nextLink as `next_link` for the next page. Requires ChannelMessage.Read.All, which needs admin consent and is not a connection default: tick it when connecting.
47
 */
48
export async function main(
49
  auth: RT.MsTeams,
50
  team_id: DynSelect_team_id,
51
  channel_id: DynSelect_channel_id,
52
  top: number = 20,
53
  expand_replies: boolean = false,
54
  next_link: string | undefined
55
) {
56
  if (
57
    next_link != null &&
58
    next_link !== "" &&
59
    !next_link.startsWith("https://graph.microsoft.com/")
60
  ) {
61
    throw new Error(
62
      "next_link must be an @odata.nextLink returned by Microsoft Graph"
63
    )
64
  }
65
  let url: URL
66
  if (next_link != null && next_link !== "") {
67
    url = new URL(next_link)
68
  } else {
69
    url = new URL(
70
      `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels/${encodeURIComponent(channel_id)}/messages`
71
    )
72
    url.searchParams.append("$top", String(top))
73
    if (expand_replies) {
74
      url.searchParams.append("$expand", "replies")
75
    }
76
  }
77

78
  const response = await fetch(url, {
79
    headers: {
80
      Authorization: `Bearer ${auth.token}`,
81
      Accept: "application/json",
82
    },
83
  })
84

85
  if (!response.ok) {
86
    throw new Error(`${response.status} ${await response.text()}`)
87
  }
88

89
  return await response.json()
90
}
91