Perl入学式に関するツイートをIdobataに通知するボットを作った

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env perl
use utf8;
use v5.12;

use AnyEvent::Twitter::Stream;
use Encode;
use Furl;

use Config::Pit;
my $config = pit_get(
    'nqounet@twitter.com',
    require => {
        consumer_key        => 'consumer_key',
        consumer_secret     => 'consumer_secret',
        token        => 'access_token',
        token_secret => 'access_token_secret',
    }
);

my $furl = Furl->new;
my $done = AE::cv;

my $listener = AnyEvent::Twitter::Stream->new(
    method   => 'filter',
    track    => 'Perl入学式',
    on_tweet => sub {
        my $tweet = shift;
        $furl->post(
            'https://idobata.io/hook/generic/xxxxxxxxxxxxxxx',
            ['Content-Type' => 'application/x-www-form-urlencoded'],
            [source         => encode_utf8(qq{$tweet->{user}{name} : $tweet->{text}})]
        );
    },
    %{$config}
);

$done->recv;
1
2
use App::Daemon qw(daemonize);
daemonize();
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env perl
use utf8;
use v5.12;

use Proclet;
use Path::Tiny qw(path cwd);
use File::RotateLogs;

use App::Daemon qw(daemonize);
daemonize();

my $logfile = join '/', cwd->absolute, 'log', 'app.log';
path($logfile)->parent->mkpath;

my $logger = File::RotateLogs->new(
    logfile      => qq{$logfile-%Y%m%d},
    linkname     => $logfile,
    rotationtime => 86400,
    maxage       => 86400 * 14,
);

my $proclet = Proclet->new(logger => sub { $logger->print(@_); });
$proclet->service(
    code => [qw(perl main.pl)],
    tag  => 'bot',
);
$proclet->run;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
requires 'perl', '5.012001';
requires 'AnyEvent::Twitter::Stream';
requires 'Furl';
requires 'Config::Pit';
requires 'Net::OAuth';
requires 'Net::SSLeay';
requires 'IO::Socket::SSL';
requires 'App::Daemon';
requires 'Proclet';
requires 'Path::Tiny';
requires 'File::RotateLogs';
requires 'Data::Printer';
comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。