| 1 | package Hubot::Adapter::Campfire; | ||||||||||
| 2 | 1 | $Hubot::Adapter::Campfire::VERSION = '0.2.8'; | |||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | extends 'Hubot::Adapter'; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use AnyEvent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use AnyEvent::HTTP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use AnyEvent::Campfire::Client; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Encode 'encode_utf8'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | use Hubot::Message; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | has 'robot' => ( is => 'ro', isa => 'Hubot::Robot', ); | |||||||||
| 20 | |||||||||||
| 21 | 1 | has 'cv' => ( is => 'ro', lazy_build => 1, ); | |||||||||
| 22 | |||||||||||
| 23 | 1 | has 'bot' => ( is => 'rw', ); | |||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | has 'httpClient' => ( is => 'ro', default => sub { LWP::UserAgent->new }, ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub _build_cv { AnyEvent->condvar } | ||||||||
|
Subroutine
_build_cv: not called CC=1 SCAR=6.9
| |||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub send { | ||||||||
|
Subroutine
send: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 30 | 0 | my ( $self, $user, @strings ) = @_; | |||||||||
| 31 | 0 | $self->bot->speak( $user->{room}, encode_utf8($_) ) for @strings; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | sub reply { | ||||||||
|
Subroutine
reply: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 35 | 0 | my ( $self, $user, @strings ) = @_; | |||||||||
| 36 | 0 | @strings = map { $user->{name} . ": $_" } @strings; | |||||||||
| 37 | 0 | $self->send( $user, @strings ); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 41 | 0 | my $self = shift; | |||||||||
| 42 | |||||||||||
| 43 | my %options = ( | ||||||||||
| 44 | token => $ENV{HUBOT_CAMPFIRE_TOKEN}, | ||||||||||
| 45 | rooms => $ENV{HUBOT_CAMPFIRE_ROOMS}, | ||||||||||
| 46 | account => $ENV{HUBOT_CAMPFIRE_ACCOUNT}, | ||||||||||
| 47 | 0 | ); | |||||||||
| 48 | |||||||||||
| 49 | 0 | $self->cv->begin; | |||||||||
| 50 | |||||||||||
| 51 | 0 | my $bot = AnyEvent::Campfire::Client->new(%options); | |||||||||
| 52 | $bot->on( | ||||||||||
| 53 | 'join', | ||||||||||
| 54 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 55 | 0 | my ( $e, $data ) = @_; | |||||||||
| 56 | 0 | $self->receive( new Hubot::EnterMessage ); | |||||||||
| 57 | } | ||||||||||
| 58 | 0 | ); | |||||||||
| 59 | |||||||||||
| 60 | $bot->on( | ||||||||||
| 61 | 'message', | ||||||||||
| 62 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 63 | 0 | my ( $e, $data ) = @_; | |||||||||
| 64 | my $user = $self->userForId( $data->{user_id}, | ||||||||||
| 65 | 0 | { room => $data->{room_id}, } ); | |||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | if ( $user->{name} eq $user->{id} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | my $req = HTTP::Request->new( | ||||||||||
| 69 | GET => sprintf( | ||||||||||
| 70 | "https://%s.campfirenow.com/users/%s", | ||||||||||
| 71 | $options{account}, $user->{id} | ||||||||||
| 72 | 0 | ), | |||||||||
| 73 | ); | ||||||||||
| 74 | 0 | $req->header( 'Accept', 'application/json' ); | |||||||||
| 75 | 0 | $req->header( 'Authorization', $bot->authorization ); | |||||||||
| 76 | 0 | my $res = $self->httpClient->request($req); # non-async | |||||||||
| 77 | 0 | ▶ | return unless $res->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 0 | my $userData; | |||||||||
| 80 | 0 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 81 | 0 | $userData = decode_json( $res->content ); | |||||||||
| 82 | } | ||||||||||
| 83 | 0 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 84 | 0 | $bot->emit( 'error', $_ ); | |||||||||
| 85 | 0 | }; | |||||||||
| 86 | |||||||||||
| 87 | 0 | $user->{name} = $userData->{user}{name}; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | ## TODO: support EnterMessage, LeaveMessage | ||||||||||
| 91 | $self->receive( | ||||||||||
| 92 | new Hubot::TextMessage( | ||||||||||
| 93 | user => $user, | ||||||||||
| 94 | text => $data->{body}, | ||||||||||
| 95 | ) | ||||||||||
| 96 | 0 | ); | |||||||||
| 97 | } | ||||||||||
| 98 | 0 | ); | |||||||||
| 99 | |||||||||||
| 100 | $bot->on( | ||||||||||
| 101 | 'error', | ||||||||||
| 102 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 103 | 0 | my ( $e, $error ) = @_; | |||||||||
| 104 | 0 | print STDERR "$error\n"; | |||||||||
| 105 | } | ||||||||||
| 106 | 0 | ); | |||||||||
| 107 | |||||||||||
| 108 | $bot->on( | ||||||||||
| 109 | 'leave', | ||||||||||
| 110 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 111 | 0 | $self->cv->send; # TODO: support multiple rooms `leave` | |||||||||
| 112 | } | ||||||||||
| 113 | 0 | ); | |||||||||
| 114 | |||||||||||
| 115 | 0 | $self->bot($bot); | |||||||||
| 116 | 0 | $self->emit('connected'); | |||||||||
| 117 | 0 | $self->cv->recv; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 0 | ▶ | sub close { | ||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 121 | 0 | my $self = shift; | |||||||||
| 122 | 0 | $self->bot->exit; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 126 | |||||||||||
| 127 | 1 | 1; | |||||||||
| 128 | |||||||||||
| 129 | =pod | ||||||||||
| 130 | |||||||||||
| 131 | =encoding utf-8 | ||||||||||
| 132 | |||||||||||
| 133 | =head1 NAME | ||||||||||
| 134 | |||||||||||
| 135 | Hubot::Adapter::Campfire - Campfire adapter for L<Hubot> | ||||||||||
| 136 | |||||||||||
| 137 | =head1 VERSION | ||||||||||
| 138 | |||||||||||
| 139 | version 0.2.8 | ||||||||||
| 140 | |||||||||||
| 141 | =head1 SYNOPSIS | ||||||||||
| 142 | |||||||||||
| 143 | $ export HUBOT_CAMPFIRE_TOKEN='xxxx' | ||||||||||
| 144 | $ export HUBOT_CAMPFIRE_ROOMS='1234' | ||||||||||
| 145 | $ export HUBOT_CAMPFIRE_ACCOUNT='aanoaa' | ||||||||||
| 146 | $ hubot -a campfire | ||||||||||
| 147 | |||||||||||
| 148 | =head1 DESCRIPTION | ||||||||||
| 149 | |||||||||||
| 150 | Campfire is the web based chat application built by L<37 Signals|http://37signals.com/>. | ||||||||||
| 151 | The Campfire adapter is one of the original adapters in Hubot. | ||||||||||
| 152 | |||||||||||
| 153 | =head1 CONFIGURATION | ||||||||||
| 154 | |||||||||||
| 155 | =over | ||||||||||
| 156 | |||||||||||
| 157 | =item HUBOT_CAMPFIRE_TOKEN | ||||||||||
| 158 | |||||||||||
| 159 | This can be found by logging in with your hubot's account click the My Info link and make a note of the API token. | ||||||||||
| 160 | |||||||||||
| 161 | =item HUBOT_CAMPFIRE_ROOMS | ||||||||||
| 162 | |||||||||||
| 163 | If you join the rooms you want your hubot to join will see notice a numerical ID for the room in the URL. Make a note of each ID for the rooms you want your hubot to join. | ||||||||||
| 164 | |||||||||||
| 165 | =item HUBOT_CAMPFIRE_ACCOUNT | ||||||||||
| 166 | |||||||||||
| 167 | This is simply the first part of the domain you visit for your Campfire account. For example if your Campfire was at C<hubot.campfirenow.com> your subdomain is hubot. Make a note of the subdomain. | ||||||||||
| 168 | |||||||||||
| 169 | =back | ||||||||||
| 170 | |||||||||||
| 171 | =head1 SEE ALSO | ||||||||||
| 172 | |||||||||||
| 173 | =over | ||||||||||
| 174 | |||||||||||
| 175 | =item L<https://github.com/37signals/campfire-api#authentication> | ||||||||||
| 176 | |||||||||||
| 177 | =item L<https://github.com/github/hubot/wiki/Adapter:-Campfire> | ||||||||||
| 178 | |||||||||||
| 179 | =back | ||||||||||
| 180 | |||||||||||
| 181 | =head1 AUTHOR | ||||||||||
| 182 | |||||||||||
| 183 | Hyungsuk Hong <hshong@perl.kr> | ||||||||||
| 184 | |||||||||||
| 185 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 186 | |||||||||||
| 187 | This software is copyright (c) 2012 by Hyungsuk Hong. | ||||||||||
| 188 | |||||||||||
| 189 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 190 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 191 | |||||||||||
| 192 | =cut | ||||||||||