0

Create Channel

by
Published today

Create a standard channel in a team. Requires Channel.Create, 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
/**
24
 * Create Channel
25
 * Create a standard channel in a team. Requires Channel.Create, which needs admin consent and is not a connection default: tick it when connecting.
26
 */
27
export async function main(
28
  auth: RT.MsTeams,
29
  team_id: DynSelect_team_id,
30
  display_name: string,
31
  description: string | undefined
32
) {
33
  const payload: { [key: string]: any } = {
34
    displayName: display_name,
35
    membershipType: "standard",
36
  }
37
  if (description != null && description !== "") {
38
    payload.description = description
39
  }
40

41
  const response = await fetch(
42
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels`,
43
    {
44
      method: "POST",
45
      headers: {
46
        Authorization: `Bearer ${auth.token}`,
47
        "Content-Type": "application/json",
48
        Accept: "application/json",
49
      },
50
      body: JSON.stringify(payload),
51
    }
52
  )
53

54
  if (!response.ok) {
55
    throw new Error(`${response.status} ${await response.text()}`)
56
  }
57

58
  return await response.json()
59
}
60