0

Get Channel Message

by
Published today

Get a single root message of a channel by id. 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 2 hours 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
 * Get Channel Message
46
 * Get a single root message of a channel by id. 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
  message_id: string
53
) {
54
  const url = new URL(
55
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels/${encodeURIComponent(channel_id)}/messages/${encodeURIComponent(message_id)}`
56
  )
57

58
  const response = await fetch(url, {
59
    headers: {
60
      Authorization: `Bearer ${auth.token}`,
61
      Accept: "application/json",
62
    },
63
  })
64

65
  if (!response.ok) {
66
    throw new Error(`${response.status} ${await response.text()}`)
67
  }
68

69
  return await response.json()
70
}
71