Subscribed unsubscribe Subscribe Subscribe

"IRC に issue 番号に反応する bot がいると捗る" らしいので

IRC に issue 番号に反応する bot がいると捗る - handlename's blog

anysan お手軽!

use strict;
use warnings;
use AnySan;
use AnySan::Provider::IRC;
use AnyEvent::HTTP;
use JSON::XS;
use Try::Tiny;
use HTTP::Request::Common;

my $host;
my $port;
my $password;
my $nickname;
my $channel;
my $owner;
my $repos;

my $github_user;
my $github_password;

my $irc = irc
    $host,
    port => $port,
    enable_ssl => 1,
    password => $password,
    nickname => $nickname,
    channels => {
        "#$channel" => {},
    },
    ;

AnySan->register_listener(
    github_issue => {
        cb => sub {
            my $receive = shift;
            my @numbers = $receive->message =~ /#(\d+)/g;

            for my $number ( @numbers ) {
                my $req = GET sprintf(
                    "https://api.github.com/repos/%s/%s/issues/%d",
                    $owner,
                    $repos,
                    $number
                );
                $req->authorization_basic($github_user, $github_password);
                my %headers = map { $_ => $req->header($_) } $req->headers->header_field_names;

                my $g; $g = http_get $req->uri, headers => \%headers, sub {
                    my ( $body, $hdr) = @_;
                    return unless $hdr->{Status} =~ /^2/;

                    try {
                        my $json = decode_json $body;
                        $receive->send_reply(
                            sprintf "#%d %s %s", $json->{number}, $json->{title}, $json->{html_url}
                        );
                    }
                    catch {
                        warn $_;
                    }
                    finally {
                        undef $g;
                    };
                };
            }
        },
    },
);

AnySan->run;