Functions/Get-SlackMessage.ps1

<#
.SYNOPSIS
    This function gets messages from a Slack channel.
.DESCRIPTION
    This function gets messages from a Slack channel.
    The scope required to call this function is "channels:history".
#>

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

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

    # Retrieve the ID for the specified channel and verify that the channel exists
    $channelID = (Get-SlackChannel -Token $token -ChannelName $channelName).id
    if (!$channelID) {
        throw "The channel '$($channelName)' cannot be found in Slack."
    }

    # Prepare the API call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://slack.com/api/channels.history"
        Method  = "GET"
        Headers = @{
            "Content-Type" = "application/x-www-form-urlencoded"
            Authorization  = "Bearer $($token)"
        }
        Body    = @{
            channel = "$($channelID)"
        }
    }

    # Invoke the call
    $response = Invoke-RestMethod @invokeRestMethodParams

    # Verify that the retrieval is successful
    if ($response.ok) {
        return $response.messages
    }
    else {
        throw "Failed to get messages from channel '$($channelName)' with the error message:`r`n$($response.error)."
    }
}