Skip to content

Instantly share code, notes, and snippets.

@chizmw
Last active March 6, 2019 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chizmw/374e3257999964e6dfe7954511c1d7c2 to your computer and use it in GitHub Desktop.
Save chizmw/374e3257999964e6dfe7954511c1d7c2 to your computer and use it in GitHub Desktop.
rescuetime-highlights

cron entry

➔ crontab -l |grep highlights
  00   9  *   *  mon       /home/chisel/perl5/perlbrew/perls/perl-5.22.2/bin/perl /home/chisel/bin/rescuetime-highlights last friday
  00   9  *   *  tue-fri   /home/chisel/perl5/perlbrew/perls/perl-5.22.2/bin/perl /home/chisel/bin/rescuetime-highlights yesterday
#!/usr/bin/env perl
use strict; use warnings;
use feature 'say';
use utf8;
use Encode;
use HTTP::Tiny;
use JSON::XS;
use Date::Manip;
use DateTime::Format::Natural;
use WebService::Slack::WebApi;
# https://get.slack.help/hc/en-us/articles/215770388-Create-and-regenerate-API-tokens
my $ACCESSTOKEN = $ENV{RH_SLACKACCESSTOKEN} or dir "RH_SLACKACCESSTOKEN not set";
my $slack = WebService::Slack::WebApi->new(token => $ACCESSTOKEN)
or die $!;
my $parser = DateTime::Format::Natural->new;
my $dt = $parser->parse_datetime("@ARGV" || 'yesterday');
die $parser->error
if (!$parser->success);
# https://www.rescuetime.com/anapi/setup/overview
my $apiKey = $ENV{RH_APIKEY} or dir "RH_APIKEY not set";
my $http = HTTP::Tiny->new();
my $highlights_url = sprintf(
"https://www.rescuetime.com/anapi/highlights_feed?key=${apiKey}&format=json"
);
my $response = $http->get($highlights_url);
die "Failed!\n" unless $response->{success};
my $all_highlights = JSON::XS::decode_json($response->{content})
or die $!;
my $filtered_highlights = [
grep { $_->{date} eq $dt->ymd }
sort {$a->{id} <=> $b->{id}} @{$all_highlights}
];
my $output = sprintf(
"*%s, %s %s:*",
$dt->day_name,
$dt->month_name,
$dt->day,
);
if (@{$filtered_highlights}) {
map { $output .= "\n• $_->{description}" } @{$filtered_highlights};
}
else {
$output .= "\n • _No Activities Recorded_";
}
# make the output 'http-message friendly
# http://www.perlmonks.org/bare/?node_id=806426
# i.e. prevent "HTTP::Message content must be bytes"
$output = encode('UTF-8', $output);
# debug and exit
if (0) {
say $output;
_share_to_slack('sandbox', $output);
exit;
}
# send to the team
_share_to_slack('infrastructurestandup', $output);
sub _get_slack_channel_or_group {
my $name = shift or die "name required";
my $dest = _get_slack_channels()->{ $name } || _get_slack_groups()->{ $name };
die "unknown group, or channel" unless defined $dest;
return $dest;
}
sub _get_slack_groups {
my $slack_groups;
my $groups = $slack->groups->list;
my $group_data;
map { $group_data->{ $_->{name} } = $_ } @{ $groups->{groups}};
return $group_data;
}
sub _get_slack_channels {
my $slack_channels;
my $channels = $slack->channels->list;
my $channel_data;
map { $channel_data->{ $_->{name} } = $_ } @{ $channels->{channels}};
return $channel_data;
}
sub _get_slack_channel {
my $channel_name = shift;
my $channel = _get_slack_channels()->{ $channel_name };
die "unknown channel" unless defined $channel;
return $channel;
}
sub _get_slack_group {
my $group_name = shift;
my $group = _get_slack_groups()->{ $group_name };
die "unknown group" unless defined $group;
return $group;
}
sub _share_to_slack {
my $channel_name = shift || die 'channel name required';
my $message = shift || die 'message required';
my $channel = _get_slack_channel_or_group($channel_name);
# posting message to specified channel and getting message description
my $posted_message = $slack->chat->post_message(
channel => $channel->{id}, # required
text => $message, # required (not required if 'attachments' argument exists)
username => 'chizcw', # optional
as_user => 1,
# other optional parameters...
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment