PoshBot.Joker.psm1
function Get-Advice { $Header = @{'Accept' = 'application/json'} (Invoke-RestMethod -Method Get -Uri 'https://api.adviceslip.com/advice' -Headers $Header -UseBasicParsing).slip.advice } function Get-CNorrisJoke { $Header = @{'Accept' = 'application/json'} (Invoke-RestMethod -Method Get -Uri 'https://api.chucknorris.io/jokes/random' -Headers $Header -UseBasicParsing).value } function Get-CorpBuzzword { $Header = @{'Accept' = 'application/json'} (Invoke-RestMethod -Method Get -Uri 'https://corporatebs-generator.sameerkumar.website/' -Headers $Header -UseBasicParsing).phrase } function Get-DadJoke { $Header = @{'Accept' = 'application/json'} (Invoke-RestMethod -Method Get -Uri 'https://icanhazdadjoke.com/' -Headers $Header -UseBasicParsing).joke } function Get-Fortune { (Invoke-RestMethod -Uri 'http://yerkee.com/api/fortune' -UseBasicParsing).fortune } function Get-GeekJoke { do { $Joke = Invoke-RestMethod -Uri 'https://sv443.net/jokeapi/category/Programming?blacklistFlags=nsfw,religious,political' -UseBasicParsing } while ($Joke.setup -like '*God*' -or $Joke.setup -like '*sex*' -or $Joke.joke -like '*sex*' -or $Joke.setup -like '*Gender*' -or $Joke.joke -like '*snuts*') if ($Joke.type -eq 'single') { $Joke.joke } else { $Joke.setup Start-Sleep -Seconds (Get-Random -Minimum 3 -Maximum 8) $Joke.delivery } } function Get-RSwansonQuote { $Header = @{'Accept' = 'application/json' } $quote = Invoke-RestMethod -Method Get -Uri 'https://ron-swanson-quotes.herokuapp.com/v2/quotes' -Headers $Header -UseBasicParsing Write-Output $quote } function Get-Joke { <# .SYNOPSIS Get a random joke. .DESCRIPTION Get a random joke, or fortune cookie message from a selection of APIs. .PARAMETER Genre The type of joke you're interested in. Genre can be chosen without specifying the parameter. Defaults to 'Dad' jokes. .PARAMETER Random Randomly selects a genre of joke to be retrieved. .EXAMPLE !joke Gets a random dad joke via default genre. .EXAMPLE !joke 'Dad' Gets a random dad joke by explictly selecting that genre. .EXAMPLE !joke -Genre 'Geek' Gets a random geek / programmer joke specifying the optional '-Genre' identifier. .EXAMPLE !joke -Random Gets a random joke from a random genre. #> [PoshBot.BotCommand(CommandName = 'joke')] [CmdletBinding(DefaultParameterSetName = 'Specific')] param ( [Parameter(Position = 0, ParameterSetName = 'Specific')] [ValidateSet('Dad', 'Geek', 'Fortune', 'ChuckNorris', 'RonSwanson', 'CorpBuzzword', 'Advice')] [string] $Genre = 'Dad', [Parameter(Mandatory, ParameterSetName = 'Random')] [switch] $Random ) if ($Random) { $Genre = (Get-Variable "Genre").Attributes.ValidValues | Get-Random } switch ($Genre) { Dad { Get-DadJoke } Geek { Get-GeekJoke } Fortune { Get-Fortune } ChuckNorris {Get-CNorrisJoke } RonSwanson { Get-RSwansonQuote } CorpBuzzword { Get-CorpBuzzword } Advice { Get-Advice } } } Export-ModuleMember -Function 'Get-Joke' |