Jump to content

Help With PowerShell Teams Bot


Recommended Posts

I have a teams bot that is an Azure function written in PowerShell.

I have the following function:

function Update-TakeRequest {
    [CmdletBinding()]
    param(
        [string]$ID,
        [string]$Key,
        [string]$OwnerID,
        [string]$OwnerName,
        [string]$TeamID,
        [string]$TeamName,
        [string]$RequestRef
    )
    $Request = Get-RequestDetails $ID $Key $RequestRef
    $MessageBody = ""
    
    if($null -eq $Request.Params.primaryEntityData) {
        $MessageBody += "Request $($RequestRef) not found."      
    } else {
        $RD = $Request.Params.primaryEntityData.record
        if ($RD.h_fk_team_id -ne $TeamID) {
            $MessageBody += "Request $($RequestRef) isn't owned by $($TeamName), so can't be taken by a user in this Team."      
        } else {
            Set-HB-Instance -Instance $ID -Key $Key
            Add-HB-Param "inReference" $RequestRef
            Add-HB-Param "inAssignToId" $OwnerID
            Add-HB-Param "inAssignToGroupId" $TeamID
            
            $Timeline = @{
                "updateText" = "''Assigned by $($OwnerName) via Microsoft Teams''"
                "source" = "teams"
                "extra" = @{
                    "h_ownername" = $OwnerName
                    "h_fk_team_id" = $TeamID
                    "h_group_name" = $TeamName
                }
                "visibility" = "trustedGuest"
            }
            $TimelineJSON = $Timeline | ConvertTo-JSON
            Add-HB-Param "updateTimelineInputs" $TimelineJSON
            $APICall = Invoke-HB-XMLMC "apps/com.hornbill.servicemanager/Requests" "assign"
            if ($APICall.Status -eq "ok") {
                if ($null -ne $APICall.Params.assignError -and $APICall.Params.assignError -ne "") {
                    $MessageBody = "An error occurred when attempting to assign the request."
                } else {
                    $MessageBody = "$($RequestRef) has been assigned to you in $($TeamName)"
                }
            } else{
                $MessageBody = "An error occurred when attempting to assign the request."
            }
        }
    }    
    $bodyObj = @{
        "type" = "message"
        "text" = $MessageBody
    }
    return $bodyObj
}

run.ps1:

if ($Request.Body.text -like '*update request*') {
    $StringArray = $Request.Body.text -split "update request",2
    $ContentString = $StringArray[$StringArray.Count - 1].TrimStart()
    $ContentArray = $ContentString -split " ",2
    $bodyObj = Update-Request $InstanceName $InstanceKey $User.displayName $ContentArray[0] $ContentArray[1] $TeamDetails.Params.id $TeamDetails.Params.name
    $body = $bodyObj | ConvertTo-Json -Depth 4
}

If a call is assigned to a colleague in my team and I invoke @Hornbill take request IN0012345 for example, I expect that call to be taken from them and assigned to me. What happens instead is the the call gets taken from the owner and assigned back into the main queue for my team, rather than being directly assigned to me.

How do I take from one owner and assign to another?

Link to comment
Share on other sites

Hi @Rob Gething,

The above code seems to be assigning a call.

Are you sure that the analyst you are assigning the call to is allowed to have the call in their queue? IF the assignment fails, then you should get an error message back that sheds some light. Is your script showing the error message? If so, how about outputting assignError instead of the hard-coded text?

If a call is correctly assigned to a team, but not to the correct analyst, then the Analyst ID must not be correct. The assignment, as a minimum, needs a correct call reference and team id - the analyst id is optional, so if you are not failing, then the analyst id is being overlooked for some reason.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...