Skip to content

Post a message to Slack

Send a webhook payload with an optional channel and username.

18th August 2026

use strict;
use warnings;

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use JSON;

sub post_slack {
    my ($webhook_url, $channel, $username, $message) = @_;

    my $ua = LWP::UserAgent->new;
    $ua->timeout(15);

    my $json = encode_json({
        channel  => $channel,
        username => $username,
        text     => $message,
    });
    my $req  = POST($webhook_url, ['payload' => $json]);
    my $resp = $ua->request($req);
    die $resp->status_line unless $resp->is_success;
}

Caveats

Needs an incoming webhook URL. Requires LWP::UserAgent and JSON.