Edits history of script submission #23637 for ' List Chats (ms_teams)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Chats
     * 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).
     */
    export async function main(
      auth: RT.MsTeams,
      top: number = 50,
      expand_members: boolean = false,
      next_link: string | undefined
    ) {
      if (
        next_link != null &&
        next_link !== "" &&
        !next_link.startsWith("https://graph.microsoft.com/")
      ) {
        throw new Error(
          "next_link must be an @odata.nextLink returned by Microsoft Graph"
        )
      }
      let url: URL
      if (next_link != null && next_link !== "") {
        url = new URL(next_link)
      } else {
        url = new URL(`https://graph.microsoft.com/v1.0/me/chats`)
        url.searchParams.append("$top", String(top))
        if (expand_members) {
          url.searchParams.append("$expand", "members")
        }
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago