forked from abramhindle/smeggdrop
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thaddeus Wooster
committed
May 21, 2012
1 parent
4110b64
commit ff4b347
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package Shittybot::TCL::Trait::Twitter; | ||
|
||
use Moose::Role; | ||
|
||
use AnyEvent::Twitter; | ||
|
||
has 'twitter_client' => ( | ||
is => 'ro', | ||
isa => 'AnyEvent::Twitter', | ||
lazy_build => 1, | ||
); | ||
|
||
sub _build_twitter_client { | ||
my ($self) = @_; | ||
|
||
my $config = $self->irc->config->{twitter} | ||
or die "Trying to use Twitter trait but twitter config is missing"; | ||
|
||
my $client = AnyEvent::Twitter->new(%$config); | ||
|
||
$client->get('account/verify_credentials', sub { | ||
my ($header, $response, $reason) = @_; | ||
|
||
print "Authenticated to twitter as $response->{screen_name}\n"; | ||
}); | ||
|
||
return $client; | ||
} | ||
|
||
sub BUILD{}; after 'BUILD' => sub { | ||
my ($self) = @_; | ||
|
||
$self->register_callbacks( | ||
post_twat => \&post_to_twitter, | ||
); | ||
}; | ||
|
||
sub post_to_twitter { | ||
my ($self, $nick, $mask, $handle, $channel, $proc, $args, $loglines) = @_; | ||
|
||
my $twit = $self->twitter_client; | ||
|
||
warn "$nick posting to twitter: '$args'\n"; | ||
|
||
$twit->post('statuses/update', { | ||
status => $args, | ||
}, sub { | ||
my ($header, $response, $reason) = @_; | ||
$self->irc->send_to_channel($channel, "posted to twitter: $response/$reason"); | ||
}); | ||
} | ||
|
||
1; |