NN.Zisson.psm1

#Region './Private/Get-ZIAccessToken.ps1' 0
function Get-ZiAccessToken {
    param (
        [string]$accessTokenPath = "$env:USERPROFILE\.creds\Zisson\zissonAccessToken.xml"
    )

    if (!(Test-Path $accessTokenPath)) {
        New-ZiAccessToken
    }
    
    Import-Clixml $accessTokenPath | ConvertFrom-SecureString -AsPlainText
}
#EndRegion './Private/Get-ZIAccessToken.ps1' 12
#Region './Private/Get-ZiEndpoint.ps1' 0
function Get-ZiEndpoint {
    [CmdletBinding()]
    param (
        [string]$EndpointPath = "$env:USERPROFILE\.creds\Zisson\zissonEndpoint.xml"
    )

    process {
        if (!(Test-Path $EndpointPath)) {
            New-ZiEndpoint
        }

        Import-Clixml $EndpointPath
    }
}
#EndRegion './Private/Get-ZiEndpoint.ps1' 15
#Region './Public/Get-ZIQueueCalls.ps1' 0
function Get-ZIQueueCalls {
    param (
        [Parameter(Mandatory)][datetime]$startDate,
        [Parameter(Mandatory)][datetime]$endDate,
        [int]$queueList
    )

    $splat = @{
        "Method" = "GET"
        "Uri" = "$(Get-ZiEndpoint)/api/simple/QueueCallsAll"
        "Headers" = @{
            "Authorization" = "Basic $(Get-ZiAccessToken)"
        }
        "Body" = @{
            "start_date" = $startDate
            "end_date"  = $endDate
            "queue_list" = $queueList
        }
    }
    Invoke-RestMethod @splat
}
#EndRegion './Public/Get-ZIQueueCalls.ps1' 22
#Region './Public/Get-ZIQueueChats.ps1' 0
function Get-ZIQueueChats {
    param (
        [Parameter(Mandatory)][datetime]$startDate,
        [Parameter(Mandatory)][datetime]$endDate
    )

    $splat = @{
        "Method" = "GET"
        "Uri" = "$(Get-ZiEndpoint)/api/simple/QueueChats"
        "Headers" = @{
            "Authorization" = "Basic $(Get-ZiAccessToken)"
        }
        "Body" = @{
            "start_date" = $startDate
            "end_date"  = $endDate
        }
    }
    Invoke-RestMethod @splat
}
#EndRegion './Public/Get-ZIQueueChats.ps1' 20
#Region './Public/Get-ZIQueueStatus.ps1' 0
function Get-ZIQueueStatus {
    param (
        [string]$queueName,
        [string]$queueType
    )

    $splat = @{
        "Method" = "GET"
        "Uri" = "$(Get-ZiEndpoint)/api/simple/QueueStatus"
        "Headers" = @{
            "Authorization" = "Basic $(Get-ZiAccessToken)"
        }
        "Body" = @{
            "queue_name" = $queueName
            "queue_type"  = $queueType
        }
    }
    Invoke-RestMethod @splat
}
#EndRegion './Public/Get-ZIQueueStatus.ps1' 20
#Region './Public/New-ZiAccessToken.ps1' 0
function New-ZIAccessToken {
    param (
        [string]$accessTokenPath = "$env:USERPROFILE\.creds\Zisson\zissonAccessToken.xml"

    )

    $username = Read-Host "Enter Zisson API username"
    if (!$username) {
        Write-Error "Please provide a Zisson API username." -ErrorAction Stop
    }

    $passwd = Read-Host "Enter Zisson API password"
    if (!$passwd) {
        Write-Error "Please provide a Zisson API password." -ErrorAction Stop
    }

    $accessTokenDir = $accessTokenPath.Substring(0, $accessTokenPath.lastIndexOf('\'))
    if (!(Test-Path $accessTokenDir)) {
        #Create parent folders of the access token file
        $null = New-Item -ItemType Directory $accessTokenDir
    }
    
    #Encode credentials to Base64
    $text = "$($username):$($passwd)"
    $bytes = [Text.Encoding]::UTF8.GetBytes($text)
    $accessToken = [Convert]::ToBase64String($bytes)

    #Create access token file
    $accessToken | ConvertTo-SecureString -AsPlainText | Export-Clixml $accessTokenPath
}
#EndRegion './Public/New-ZiAccessToken.ps1' 31
#Region './Public/New-ZiEndpoint.ps1' 0
function New-ZiEndpoint {
    [CmdletBinding()]
    param (
        [string]$EndpointPath = "$env:USERPROFILE\.creds\Zisson\zissonEndpoint.xml"
    )

    process {
        $DomainSuffix = Read-Host "Enter your Zisson domain suffix (no/com)"
        switch ($DomainSuffix) {
            no {
                $Endpoint = "https://api.zisson.no"
            }
            com {
                $Endpoint = "https://api.zisson.com"
            }
            Default {
                Write-Error "Domain suffix was set to `"$DomainSuffix`". Please enter either `"no`" or `"com`"" -ErrorAction Stop
            }
        }

        #Create parent folders of the access token file
        $EndpointDir = $EndpointPath.Substring(0, $EndpointPath.lastIndexOf('\'))
        if (!(Test-Path $EndpointDir)) {
            $null = New-Item -ItemType Directory $EndpointDir
        }

        $Endpoint | Export-Clixml $EndpointPath
    }
}
#EndRegion './Public/New-ZiEndpoint.ps1' 30
#Region './Public/Send-ZISMS.ps1' 0
function Send-ZISMS {
    param (
        [Parameter(Mandatory)][string]$message,
        [Parameter(Mandatory)][string]$recipients,
        [Parameter(Mandatory)][string]$senderNumber,
        [Parameter(Mandatory)][string]$senderName
    )

    $splat = @{
        "Method" = "POST"
        "Uri" = "$(Get-ZiEndpoint)/api/simple/SendSms"
        "Headers" = @{
            "Authorization" = "Basic $(Get-ZiAccessToken)"
        }
        "Body" = @{
            "sender_number" = $senderNumber
            "sender_name" = $senderName
            "recipients" = $recipients
            "msg" = $message
        }
    }
    Invoke-RestMethod @splat
}
#EndRegion './Public/Send-ZISMS.ps1' 24