#!/home/jesse/perl5/perlbrew/perls/perl-5.16.3/bin/perl
use warnings;
use strict;
use CGI;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
use JSON;

my $peers = { asdf   => '...'
              stripe => '...' };

my $q = CGI->new();

if (($q->param('user_id') || '') =~ /SLACKBOT/) {
    print "Content-type: text/plain\n\nwe don't proxy bots here\n";
    exit;
}
if (($q->param('text') || '') eq '') {
    print "Content-type: text/plain\n\nskipping blank messages\n";
    exit;
}

my $payload;
$payload->{'channel'}  = '#slappyhour';
$payload->{'username'} = $q->param('user_name') . "@" . $q->param('team_domain');
$payload->{'text'}     = $q->param('text');

for my $peer (keys %$peers) {
    next if ($peer eq $q->param('team_domain'));
    eval { my $response = $mech->post($peers->{$peer}, Content => encode_json($payload)); };
}
print "Content-type: text/plain\n\nyeah, fine.\n";

