Skip to content

Instantly share code, notes, and snippets.

@areyoutoo
Created April 21, 2017 18:57
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 areyoutoo/8e7e8fe7416f1e9666833011d926695b to your computer and use it in GitHub Desktop.
Save areyoutoo/8e7e8fe7416f1e9666833011d926695b to your computer and use it in GitHub Desktop.
An angry intern abused HipChat and gave Sassy about -16,000 karma. This script gradually resets Sassy's karma back to zero... also a fun excuse to try HipChat's API.
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use URI;
use POSIX;
use Data::Dumper;
my $VERSION = 0.01;
my $room = 'your_room_encodedname';
my $auth_token = 'your_secret_code';
my $base_url = 'https://api.hipchat.com/v2';
my $ua = LWP::UserAgent->new();
$ua->agent("SaveSassy/$VERSION ");
$ua->default_header('Authorization' => "Bearer $auth_token");
$ua->default_header('content-type' => 'application/json');
my $start_karma = 7140;
my $karma = $start_karma;
my $start_epoch = time();
while ($karma > 4) {
my $epoch = time();
my $seconds_diff = $epoch - $start_epoch;
$seconds_diff = 1 if $seconds_diff < 1;
my $karma_diff = $start_karma - $karma;
$karma_diff = 1 if $karma_diff < 1;
my $karma_per_second = $karma_diff / $seconds_diff;
my $karma_per_minute = $karma_per_second * 60;
my $minutes_remaining = POSIX::floor($karma / $karma_per_minute);
my $time_msg = "about $minutes_remaining minutes to go";
if ($seconds_diff < 100) {
$time_msg = "estimating time left";
} elsif ($minutes_remaining == 0) {
$time_msg = "almost there!";
}
my $commands_remaining = POSIX::floor($karma / 5);
my $result = post_message($room, "Sassy recovery project | sassy++++++ | $commands_remaining msgs left | $time_msg");
$karma -= 5;
if ($result->{limit_remaining} <= 2 && $result->{limit_reset} > time()) {
my $wait = $result->{limit_reset} - time();
post_message($room, "Hit rate limit. Waiting $wait seconds.") if $result->{limit_remaining} > 1 && $result->{flood_remaining} > 1;
sleep $wait;
}
if ($result->{flood_remaining} <= 2 && $result->{flood_reset} > time()) {
my $wait = $result->{flood_reset} - time();
post_message($room, "Hit flood limit. Waiting $wait seconds.") if $result->{limit_remaining} > 1 && $result->{flood_remaining} > 1;
sleep $wait;
}
sleep 1;
}
sub post_message {
my $room = shift or die "missing arg: room";
my $message = shift or die "missing arg: message";
print "$message\n";
my $req = HTTP::Request->new(
'POST',
URI->new("$base_url/room/$room/message"),
);
my $content = encode_json({
message => $message,
});
$req->content($content);
my $res = $ua->request($req);
die $res->content unless $res->is_success;
my $val = {};
$val->{limit_remaining} = $res->header('X-RateLimit-Remaining');
$val->{limit_reset} = $res->header('X-RateLimit-Reset');
$val->{flood_remaining} = $res->header('X-FloodControl-Remaining');
$val->{flood_reset} = $res->header('X-FloodControl-Reset');
return $val;
}
@arcones
Copy link

arcones commented Sep 3, 2017

We used this script in our office and works fine, thank you very much!!
As we have the hipchat server on our premises, we have had to replace the:

my $base_url = 'https://api.hipchat.com/v2';

By the url of our server (keeping the v2 at the end).
The token is easy to generate via hipchat interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment