Browse Source

Add initial commit of working script

This commit contains initial functionality to receive a piped MIME
message, parse out relevant parts, load and validate connection
details from a local rc file, then post to the Slack webhook.
master
Blaine Motsinger 11 months ago
commit
a6aa10c614
  1. 4
      .email-pipe-to-slack-hookrc.example
  2. 5
      .gitignore
  3. 49
      bin/hook.pl
  4. 65
      lib/EmailPipeToSlackHook/Config.pm

4
.email-pipe-to-slack-hookrc.example

@ -0,0 +1,4 @@
[slack]
uri = https://hooks.slack.com/services/exampleuri
channel = #examplechannel
username = exampleusername

5
.gitignore

@ -0,0 +1,5 @@
scratch
*.swp
*.back
tmp/
.email-pipe-to-slack-hookrc

49
bin/hook.pl

@ -0,0 +1,49 @@
#!/usr/bin/env perl
use strictures version => 2;
use MIME::Parser;
use HTTP::Tiny;
use Try::Tiny;
use JSON::Tiny;
use FindBin ();
use lib "$FindBin::RealBin/../lib";
use EmailPipeToSlackHook::Config;
my $config = EmailPipeToSlackHook::Config::get();
my $parser = MIME::Parser->new();
$parser->output_to_core(1);
my $ent = $parser->parse( \*STDIN )
or die "hook.pl: parse failed: $!\n";
chomp( my $date = $ent->head->get( 'Date' ) );
chomp( my $from = $ent->head->get( 'From' ) );
chomp( my $to = $ent->head->get( 'To' ) );
my $body = $ent->stringify_body;
my $webhook_uri = $config->{slack}{uri};
my $payload = {
channel => $config->{slack}{channel},
username => $config->{slack}{username},
text => $body,
};
my $json = try {
return JSON::Tiny::encode_json( $payload );
}
catch {
my $exception = $_;
die "encode JSON for request was not successful: $exception\n";
};
my $http = HTTP::Tiny->new();
my $response = $http->request( 'POST', $webhook_uri, { headers => { 'content-type' => 'application/json' }, content => $json } );
unless ( $response->{success} ) {
die 'request was not successful: ' . $response->{reason} . "\n";
}
exit 0;

65
lib/EmailPipeToSlackHook/Config.pm

@ -0,0 +1,65 @@
package EmailPipeToSlackHook::Config;
use strictures version => 2;
use Cwd ();
use Config::Tiny ();
use Data::Structure::Util ();
our $VERSION = '0.001';
sub get {
my $config = _load_config();
_validate($config);
return $config;
}
sub _load_config {
my $module_path = Cwd::realpath(__FILE__);
$module_path =~ s/\w+\.pm//;
my $rc = Cwd::realpath( $module_path . '/../../.email-pipe-to-slack-hookrc' );
unless ( -f $rc ) {
die "$rc is not present\n";
}
return Data::Structure::Util::unbless( Config::Tiny->read($rc) );
}
sub _validate {
my $config = shift;
# verify required config sections
foreach my $required (qw{ slack }) {
unless ( exists $config->{$required} ) {
die "config section $required is required\n";
}
}
foreach my $sub_required (qw{ uri channel username }) {
unless ( exists $config->{slack}{$sub_required} && defined $config->{slack}{$sub_required} ) {
die "config section slack $sub_required is required\n";
}
}
if ( $config->{slack}{uri} eq 'https://hooks.slack.com/services/exampleuri' ) {
die "config section slack uri is the default string and must be updated\n";
}
if ( $config->{slack}{channel} eq '#examplechannel' ) {
die "config section slack channel is the default string and must be updated\n";
}
if ( $config->{slack}{channel} !~ /^#/ ) {
die "config section slack channel is not properly formatted and must begin with a # symbol\n";
}
if ( $config->{slack}{username} eq 'exampleusername' ) {
die "config section slack channel is the default string and must be updated\n";
}
return 1;
}
1;
Loading…
Cancel
Save