Public/Get-TriviaQuestion.ps1

<#
.SYNOPSIS
    Retrieves one or more trivia questions from opentdb.com, of the specified type, in the specified category and with the specified difficulty level.
#>

function Get-TriviaQuestion
{
    param
    (
        [Parameter(Position = 0)]
        [Category]$Category = [Category]::Any,

        [Difficulty]$Difficulty = [Difficulty]::Any,

        [QuestionType]$Type = [QuestionType]::Any,

        [ValidateRange(1, 50)]
        [int]$Count = 1
    )

    if (-not $script:SessionToken)
    {
        $script:SessionToken = Invoke-RestMethod -Uri 'https://opentdb.com/api_token.php?command=request'
        | Select-Object -ExpandProperty token
    }

    $QueryString = @(
        if ($Category -ne [Category]::Any) { "category=$([int]$Category)" }
        if ($Difficulty -ne [Difficulty]::Any) { "difficulty=$($Difficulty.ToString().ToLowerInvariant())" }
        if ($Type -eq [QuestionType]::YesNo) { 'type=boolean' }
        if ($Type -eq [QuestionType]::MultipleChoice) { 'type=multiple' }
        "amount=$($Count)"
        "token=$($script:SessionToken)"
    ) -join '&'

    $Url = @(
        'https://opentdb.com/api.php',
        $QueryString
    ) -join '?'

    $Response = Invoke-RestMethod -Uri $Url

    switch ($Response.response_code)
    {
        0
        {
            $Response.Results.ForEach{
                [string]$CorrectAnswer = [System.Web.HttpUtility]::HtmlDecode($_.Correct_Answer)
                [string[]]$IncorrectAnswers = $_.Incorrect_Answers | ForEach-Object { [System.Web.HttpUtility]::HtmlDecode($_) }
                [string[]]$AllAnswers = $IncorrectAnswers + $CorrectAnswer
                [string[]]$ShuffledAnswers = $AllAnswers | Sort-Object -Property { Get-Random }

                [PSCustomObject]@{
                    PSTypeName       = 'UncommonSense.OpenTriviaDatabase.TriviaQuestion'
                    Type             = $_.Type
                    Difficulty       = [Difficulty]$_.Difficulty
                    Category         = $_.Category
                    Question         = [System.Web.HttpUtility]::HtmlDecode($_.Question)
                    CorrectAnswer    = $CorrectAnswer
                    IncorrectAnswers = $IncorrectAnswers
                    AllAnswers       = $AllAnswers
                    ShuffledAnswers  = $ShuffledAnswers
                }
            }
        }

        1
        {
            Write-Error 'Could not return results. The API doesn''t have enough questions for your query.'
        }

        2
        {
            Write-Error 'Contains an invalid parameter. Arguements passed in aren''t valid.'
        }

        3
        {
            $script:SessionToken = $null
            Get-TriviaQuestion -Category $Category -Difficulty $Difficulty -Type $Type -Count $Count
        }

        4
        {
            Write-Warning 'Session Token has returned all possible questions for the specified query.'
            $script:SessionToken = $null
            Get-TriviaQuestion -Category $Category -Difficulty $Difficulty -Type $Type -Count $Count
        }

        5
        {
            Write-Error 'Too many requests have occurred. Each IP can only access the API once every 5 seconds.'
        }
    }
}