Created
April 24, 2018 09:25
-
-
Save EitanBlumin/686053c557fd6a149a8cba4af0de0ca6 to your computer and use it in GitHub Desktop.
Zendesk API - Set Primary and Secondary Talk Agents and Availability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param | |
( | |
[string] $PrimaryNinja = "Jane Doe", | |
[string] $SecondaryNinja = "John Smith" | |
) | |
$global:zendesk_user_name = "[email protected]/token" # The /token part is obligatory when using Zendesk's API | |
$global:zendesk_password = "put_your_zendesk_API_token_here" | |
$global:zendesk_address = "https://your_zendesk_subdomain_here.zendesk.com" | |
$global:primarySLAgroupname = "SLA Primary" | |
$global:secondarySLAgroupname = "SLA Secondary" | |
function CreateAuthorizationHeader() | |
{ | |
$pair = "$($zendesk_user_name):$($zendesk_password)" | |
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) | |
$basicAuthValue = "Basic $encodedCreds" | |
$Headers = @{ | |
Authorization = $basicAuthValue | |
} | |
return $Headers | |
} | |
$header = CreateAuthorizationHeader | |
# note: can probably optimize the querying of users using Zendesk's Search API and filtering by role and/or name | |
$global:users = Invoke-RestMethod -UseBasicParsing -ContentType "application/json" -Uri "$zendesk_address/api/v2/users.json" -Headers $header | |
$global:groups = Invoke-RestMethod -UseBasicParsing -ContentType "application/json" -Uri "$zendesk_address/api/v2/groups.json" -Headers $header | |
$global:memberships = Invoke-RestMethod -UseBasicParsing -ContentType "application/json" -Uri "$zendesk_address/api/v2/group_memberships.json" -Headers $header | |
function SetExclusiveNinjaMembership([string] $NinjaName,[string] $GroupName) | |
{ | |
$agentObject = $global:users.users | Select id, name, role | Where name -eq $NinjaName | |
$groupObject = $global:groups.groups | Select id, name | Where name -eq $GroupName | |
$needToAdd = "True" | |
foreach ($membershipsObjectToDelete in ($memberships.group_memberships | Select id, user_id, group_id | Where group_id -eq $groupObject.id)) | |
{ | |
if ($membershipsObjectToDelete.user_id -ne $agentObject.id) | |
{ | |
"Delete membership " + $membershipsObjectToDelete.id # informational message | |
$uri = "$zendesk_address/api/v2/group_memberships/" + $membershipsObjectToDelete.id + ".json" | |
$deleted = Invoke-RestMethod -Method Delete -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header | |
} else { | |
$needToAdd = "False" | |
} | |
} | |
if ($needToAdd -eq "True") | |
{ | |
"Add membership for user_id " + $agentObject.id + " group_id " + $groupObject.id # informational message | |
$body = '{"group_membership": {"user_id": ' + $agentObject.id + ', "group_id": ' + $groupObject.id + '}}' | |
$newmembership = Invoke-RestMethod -Method Post -UseBasicParsing -ContentType "application/json" -Uri "$zendesk_address/api/v2/group_memberships.json" -Headers $header -Body $body | |
} else { | |
"Agent is already in group." # informational message | |
} | |
"Making sure agent is available in Talk..." # informational message | |
$uri = "$zendesk_address/api/v2/channels/voice/availabilities/" + $agentObject.id + ".json" | |
$agentAvailability = Invoke-RestMethod -Method Get -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header | |
if ($agentAvailability.availability.available -and $agentAvailability.availability.via -eq "phone") | |
{ | |
"Agent is already available. No need to update." # informational message | |
} else { | |
"Agent is not currently available. Updating..." # informational message | |
$body = '{"availability": { "via": "phone", "available": true }}' | |
$agentAvailability = Invoke-RestMethod -Method Put -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header -Body $body | |
} | |
} | |
SetExclusiveNinjaMembership -NinjaName $PrimaryNinja -GroupName $global:primarySLAgroupname | |
SetExclusiveNinjaMembership -NinjaName $SecondaryNinja -GroupName $global:secondarySLAgroupname |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line #28 could be optimized by using the search.json API of Zendesk in order to filter the queried users (i.e. by role and/or by name/email) instead of the users.json API which retrieves all users (including end-users).