Edits history of script submission #23627 for ' Create Channel (ms_teams)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_team_id = string
    
    // Dropdown of the teams the connected user has joined.
    export async function team_id(auth: RT.MsTeams) {
      const response = await fetch(
        `https://graph.microsoft.com/v1.0/me/joinedTeams`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { value } = (await response.json()) as { value: any[] }
      return value.map((x) => ({ value: x.id, label: x.displayName }))
    }
    
    /**
     * Create Channel
     * Create a standard channel in a team. Requires Channel.Create, which needs admin consent and is not a connection default: tick it when connecting.
     */
    export async function main(
      auth: RT.MsTeams,
      team_id: DynSelect_team_id,
      display_name: string,
      description: string | undefined
    ) {
      const payload: { [key: string]: any } = {
        displayName: display_name,
        membershipType: "standard",
      }
      if (description != null && description !== "") {
        payload.description = description
      }
    
      const response = await fetch(
        `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify(payload),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago