Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs
My script connects fine and retrieves data, but I'm really struggling with the JSON to create the query I want. I've left in the example query, but I'm actually I'm trying to retrieve the agent timeline data to list agents and the number of calls they have taken each day. I think I've cracked the hard bit can anyone assist with the query?
# Define the API endpoint and authentication details
$URL = "https://api.interfaces.records.teams.microsoft.com/Teams.VoiceAnalytics/getanalytics?query="
$TennantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # Azure Tenant ID
$ClientID = "a672d62c-fc7b-4e81-a576-e60dc46e951d" # Azure Application (Client) ID
# Define the date range for the query
$FromDate = (Get-Date).AddDays(-7).ToString("yyyy-MM-dd") # Start date (7 days ago)
$ToDate = (Get-Date).ToString("yyyy-MM-dd") # End date (today)
# Requires the MSAL.PS module for authentication
#Requires -Module MSAL.PS
# Construct the JSON request payload as a PowerShell object
$jsonObject = @{
Filters = @(
@{
DataModelName = "Date"
Value = $FromDate
Operand = 4 # Operand 4: Greater than or equal to
},
@{
DataModelName = "Date"
Value = $ToDate
Operand = 6 # Operand 6: Less than or equal to
}
)
Dimensions = @(
@{ DataModelName = "AutoAttendantIdentity" }
@{ DataModelName = "AutoAttendantDirectorySearchMethod" }
)
Measurements = @(
@{ DataModelName = "PSTNTotalMinutes" }
@{ DataModelName = "TotalCallCount" }
)
Parameters = @{
UserAgent = "Power BI Desktop" # Identifies the request source
}
LimitResultRowsCount = 100000 # Maximum number of rows to return
}
# Convert the PowerShell object to a JSON string
$jsonString = $jsonObject | ConvertTo-Json -Depth 10 -Compress
# Compress the JSON string using Gzip
$Bytes = $jsonString.ToCharArray() | ForEach-Object { $_ -as [Byte] }
$MemoryStream = New-Object IO.MemoryStream
$CompressedStream = New-Object System.IO.Compression.GZipStream ($MemoryStream, [IO.Compression.CompressionMode]::Compress)
$CompressedStream.Write($Bytes, 0, $Bytes.Length)
$CompressedStream.Close()
$Gzip = [Convert]::ToBase64String($MemoryStream.ToArray()) # Convert compressed data to Base64
$MemoryStream.Close()
# URL-encode the Base64 Gzip string
$UrlEncoded = [System.Web.HttpUtility]::UrlEncode($Gzip)
# Authenticate and obtain an access token
$Token = (Get-MsalToken -ClientId $ClientID -TenantId $TennantId -Scopes "https://api.interfaces.records.teams.microsoft.com/.default").AccessToken
# Construct the full API URL with the encoded query
$URLwithQuery = $URL + $UrlEncoded
# Define the request headers
$Headers = @{
Authorization = "Bearer $Token" # Include the access token in the Authorization header
}
# Send the API request and capture the response
$response = Invoke-RestMethod -Uri $URLwithQuery -Headers $Headers -Method Get
# Output the response data
$response.dataResult