PoshBot.Joker.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function Get-Advice {
    $Header = @{'Accept' = 'application/json'}

    (Invoke-RestMethod -Method Get -Uri 'https://api.adviceslip.com/advice' -Headers $Header -UseBasicParsing).slip.advice
}
function Get-AQuoteAboutNothing {
    $quoteUri = "https://seinfeld-quotes.herokuapp.com/random"

    try{
        # Are you master of your domain?
        $m = Invoke-RestMethod -Method Get -Uri $quoteUri

        Write-Output "$($m.quote) - $($m.author) [Season $($m.season), Episode $($m.episode)]"
    }
    catch{
        # [Slams money on the counter] I'm out!!
        Write-Output "No quote for you! Come back one year!!"
    }
}
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/v2/joke/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-GeneralJoke{
    $Header = @{'Accept' = 'application/json'}

 $joke = (Invoke-RestMethod -Method Get -Uri 'https://official-joke-api.appspot.com/random_joke' -Headers $Header -UseBasicParsing)
 Write-Output "[Question]:: $($joke.Setup)"
 Write-Output "[PunchLine]:: $($joke.punchline)"
}
Function Get-ProgrammingJoke{
    $Header = @{'Accept' = 'application/json'}

 $joke = (Invoke-RestMethod -Method Get -Uri 'https://official-joke-api.appspot.com/jokes/programming/random' -Headers $Header -UseBasicParsing)
 Write-Output "[Question]:: $($joke.Setup)"
 Write-Output "[PunchLine]:: $($joke.punchline)"
}
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','Programming','General', 'Seinfeld')]
        [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 }
        Programming {Get-ProgrammingJoke}
        General {Get-GeneralJoke}
        Seinfeld { Get-AQuoteAboutNothing }
    }
}
Export-ModuleMember -Function 'Get-Joke'