PrivateFunctions/Set-SlackChannelTopic.ps1

<#
.SYNOPSIS
    This function sets the topic of a channel in Slack.
.DESCRIPTION
    This function sets the topic of a channel in Slack.
#>

function Set-SlackChannelTopic {
    param(
        [CmdletBinding(PositionalBinding=$false)]
        [OutputType([Bool])]
        # The authentication token for Slack
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$token,

        # The name of the channel to set
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$channelName,

        # The topic of the channel
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$topic,

        # Select the stream where the messages will be directed.
        [Parameter(Mandatory=$false)]
        [ValidateSet("Information", "Warning", "Error", "None")]
        [String]$outputStream = "Error"
    )

    # Convert channel name to channel ID verify that it exists
    $channelID = (Get-SlackChannel -Token $token -ChannelName $channelName).id
    if (!$channelID) {
        Write-OutputMessage "The channel '$($channelName)' cannot be found in Slack." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Prepare the API call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://slack.com/api/channels.setTopic"
        Method  = "POST"
        Headers = @{
            "Content-Type" = "application/json"
            Authorization  = "Bearer $($token)"
        }
        Body    = @{
            "channel" = "$($channelID)"
            "topic"   = "$($topic)"
        } | ConvertTo-Json
    }

    # Try to set the channel topic
    Write-Information "Setting the topic of the channel '$($channelName)'."
    try {
        $response = Invoke-RestMethod @invokeRestMethodParams
    }
    catch {
        Write-OutputMessage "Exception occurred while setting the topic of the channel '$($channelName)'.`r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Verify that the updating is successful
    if ($response.ok) {
        Write-Information "The topic of the channel '$($channelName)' was set to $($newChannelName)."
        return $true
    }
    else {
        Write-OutputMessage "Failed to setting the topic of channel '$($channelName)' with the error message:`r`n$($response.error)." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }
}