Skip to content

Instantly share code, notes, and snippets.

@artifactsauce
Last active August 29, 2017 13:50
Show Gist options
  • Save artifactsauce/4efa20c593f5604d2be8 to your computer and use it in GitHub Desktop.
Save artifactsauce/4efa20c593f5604d2be8 to your computer and use it in GitHub Desktop.
サーバー側での処理結果をPushbulletで通知する ref: http://qiita.com/artifactsauce/items/b4165ec1229d1ce3f1dd
requires 'LWP::Protocol';
requires 'LWP::Protocol::https';
requires 'JSON';
$ sudo yum install perl-IO-Socket-SSL
$ cpanm IO::Socket::SSL
$ some-command | PUSHBULLET_ACCESS_TOKEN="YOUR_ACCESS_TOKEN" /path/to/pushbullet-pushes.pl "Notification title"
PUSHBULLET_ACCESS_TOKEN="YOUR_ACCESS_TOKEN"
0 4 1 */2 * root /path/to/letsencrypt-update.sh | /path/to/pushbullet-pushes.pl "Lets' Encrypt Updated"
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use HTTP::Tiny;
use Encode;
use Encode::Guess qw/shift-jis euc-jp 7bit-jis/;
use JSON::PP qw/encode_json decode_json/;
my $config = load_config( "$ENV{HOME}/.pushbullet.json" );
my $access_token = $ENV{PUSHBULLET_ACCESS_TOKEN} // $config->{pushbullet_access_token} // "";
my $endpoint = "https://api.pushbullet.com/v2/pushes";
my $title = $ARGV[0];
my $body = "";
while ( <STDIN> ) {
chomp $_;
$body .= "$_\n";
}
my $decoder = Encode::Guess->guess( $body );
if ( ref $decoder ) {
$body = $decoder->decode( $body );
}
my $json_data = {
title => $title,
body => $body,
type => "note",
};
my $headers = {
'Access-Token' => $access_token,
'Content-Type' => 'application/json',
};
my $ua = HTTP::Tiny->new;
my $response = $ua->post( $endpoint, {
'headers' => \%$headers,
'content' => encode_json( $json_data ),
} );
unless ( $response->{success} ) {
print STDERR Dumper $response;
exit 1;
}
exit 0;
sub load_config {
my $file = shift;
return {} unless -f $file;
open my $fh, '<', $file or die $!;
$config = decode_json( join '', <$fh> );
close $fh;
return $config;
}
PUSHBULLET_ACCESS_TOKEN="YOUR_ACCESS_TOKEN"
0 4 1 */2 * root /path/to/something-update.sh | /path/to/pushbullet-pushes.pl "Updated something"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment