Public/Send-SlackFile.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
function Send-SlackFile {
    <#
    .SYNOPSIS
        Send a Slack file
 
    .DESCRIPTION
        Send a Slack file
 
        We currently only support the 'Content' option for uploads. PRs for 'File' welcome :)
        https://api.slack.com/methods/files.upload
 
    .PARAMETER Token
        Token to use for the Slack API
 
        Default value is the value set by Set-PSSlackConfig
 
    .PARAMETER Content
        Content of the file to send. Text is editable after sending.
 
    .Parameter FileType
        If specified, override the FileType determined by the filename.
         
        List of types: https://api.slack.com/types/file#file_types
 
    .PARAMETER Channel
        Optional channel, private group, or IM channel to send file to. Can be an encoded ID, or a name.
 
    .PARAMETER FileName
        Required filename for this file. Used to determine syntax highlighting and other functionality.
 
    .PARAMETER Title
        Optional title of the file
 
    .PARAMETER Comment
        Optional initial comment for the file
 
    .EXAMPLE
        Send-SlackFile -Token $Token `
                       -Channel general `
                       -Content 'get-help about_* | get-random' `
                       -FileType perl `
                       -filename example.ps1
 
        # Send a slack file that turns into a snippet.
        # Use perl, because PowerShell syntax highlighting is sad
 
    .FUNCTIONALITY
        Slack
    #>


    [cmdletbinding(DefaultParameterSetName = 'Content')]
    param (
        [string]$Token = $Script:PSSlack.Token,
        
        [parameter(ParameterSetName = 'Content',
                   Mandatory = $True)]
        [string]$Content,
        
        #[validatescript({Test-Path -PathType Leaf -Path $_})]
        #[parameter(ParameterSetName = 'File',
        # Mandatory = $True)]
        #[string]$Path,

        [string]$FileType,
        [string[]]$Channel,
        [string]$FileName,
        [String]$Title,
        [String]$Comment
    )
    process
    {

        $body = @{}

        switch ($psboundparameters.keys) {
            'Content'     {$body.content     = $content}
            'Channel'     {$body.channels = $Channel -join ", " }
            'FileName'    {$body.filename = $FileName}
            'Title'       {$body.Title = $Title}
            'Comment'     {$body.comment = $Comment}
            'FileType'    {$body.filetype = $FileType}
        }

        Write-Verbose "Send-SlackApi -Body $($body | Format-List | Out-String)"
        $response = Send-SlackApi -Method files.upload -Body $body -Token $Token
        $response
    }
}