Skip to content

Instantly share code, notes, and snippets.

@layer-acht
Last active January 31, 2016 14:25
Show Gist options
  • Save layer-acht/cfede5eebb871c75f8c0 to your computer and use it in GitHub Desktop.
Save layer-acht/cfede5eebb871c75f8c0 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw{ POST };
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $ret = 0;
my $quotelog = 'test.csv';
my $uri = 'https://hooks.slack.com/services/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';
########################
# send a POST to slack
# parameter: json payload "text"
sub post_slack
{
my($string) = @_;
my $json = '{"text":"'.$string.'", "response_type": "in_channel"}';
my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $lwp = LWP::UserAgent->new;
$lwp->request( $req );
}
########################
# write to quotelog
# parameter: string
# destination
sub log_add
{
my($quote, $destination) = @_;
open(my $FHOUT, '>>', $destination) or die "Could not open file '$quotelog' $!";
print $FHOUT $quote."\n";
close $FHOUT;
}
########################
# main
my $q = CGI->new;
if ( $q->param('text') eq "" )
{
open my $FHIN, '<', $quotelog;
chomp(my @lines = <$FHIN>);
close $FHIN;
post_slack($lines[rand @lines]);
}
else
{
log_add($q->param('text'), $quotelog);
post_slack($q->param('user_name')." added ".$q->param('text')."\n");
}
print "Status: 204 No Response\n\n";
exit $ret;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment