Public/Find-SlackMessage.ps1

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
function Find-SlackMessage {
    <#
    .SYNOPSIS
        Search a Slack team for a message
 
    .DESCRIPTION
        Search a Slack team for a message
 
        Output will include details on the matching message, along with 'previous' and 'next' for context.
 
    .PARAMETER Query
        Search query. May contains booleans
 
        Messages are searched primarily inside the message text themselves,
            with a lower priority on the messages immediately before and after.
 
        If more than one search term is provided, user and channel are also matched at a lower priority.
 
        To specifically search within a channel, group, or DM, add
            in:channel_name,
            in:group_name, or
            in:username.
 
        To search for messages from a specific speaker, add
            from:username or
            from:botname.
 
        See https://api.slack.com/methods/search.messages for more information
 
    .PARAMETER Token
        Specify a token for authorization.
 
        See 'Authentication' section here for more information: https://api.slack.com/web
        Test tokens are a simple way to use this
 
    .PARAMETER SortDirection
        Sort asc[ending] or desc[ending]. Defaults to desc
 
    .PARAMETER SortBy
        Sort by score (relevance) or timestamp (date). Defaults to score
 
    .PARAMETER Count
        Return this many results per page. Defaults to 20
 
    .PARAMETER Page
        Which page to return. Defaults to 1
 
    .PARAMETER MaxPages
        Stop requesting search results once you hit this many pages. Defaults to the maximum value of int
 
    .PARAMETER Raw
        If specified, we provide raw output and do not parse any responses
 
    .FUNCTIONALITY
        Slack
    #>

    [cmdletbinding()]
    param (
        [string]$Query,
        $Token,
        [validateset('asc','desc')]
        $SortDirection = 'desc',
        [validateset('score','timestamp')]
        $SortBy = 'score',
        [int]$Count = 20,
        [int]$Page = 1,
        [int]$MaxPages = [int]::MaxValue,
        [switch]$Raw
    )
    end
    {
        Write-Verbose "$($PSBoundParameters | Out-String)"

        #Initial args
        $body = @{
            query = $Query
            sort_dir = $SortDirection
            sort = $SortBy
            count = $Count
            page = $Page
        }
        $params = @{
            Body = $Body
            Method = 'search.messages'
        }
        if($PSBoundParameters.ContainsKey('Token'))
        {
            $Params.Add('Token',$token)
        }

        #Pagination and parsing
        do
        {
            Write-Verbose "SendSlackApi -Params $($Params | Format-List | Out-String)"
            $response = Send-SlackApi @params
            if ($response.ok)
            {
                $link = "$($Script:PSSlack.ArchiveUri)/$($response.channel)/p$($response.ts -replace '\.')"
                $response | Add-Member -MemberType NoteProperty -Name link -Value $link
                $ResponsePage = $response.messages.paging.page
                $ResponsePageCount = $response.messages.paging.pages
                if($ResponsePage -lt $ResponsePageCount)
                {
                    $Page++
                    $Params.Body.page = $Page
                }

                if($Raw)
                {
                    $link = "$($Script:PSSlack.ArchiveUri)/$($response.channel)/p$($response.ts -replace '\.')"
                    $response | Add-Member -MemberType NoteProperty -Name link -Value $link
                    $response
                }
                else
                {
                    Parse-SlackMessage -InputObject $Response -Match
                }
            }
            else {
                $response
                break
            }

        } until ( $ResponsePage -eq $ResponsePageCount -or
                  $ResponsePage -eq $MaxPages)
    }
}