| 1 | package AnySan::Provider::Twitter; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'AnySan::Provider'; | ||
| 5 | our @EXPORT = qw(twitter); | ||
| 6 | use AnySan; | ||
| 7 | use AnySan::Receive; | ||
| 8 | use AnyEvent::Twitter; | ||
| 9 | use AnyEvent::Twitter::Stream; | ||
| 10 | |||
| 11 | sub twitter { | ||
| 12 | my(%config) = @_; | ||
| 13 | |||
| 14 | my $self = __PACKAGE__->new( | ||
| 15 | client => undef, | ||
| 16 | config => \%config, | ||
| 17 | ); | ||
| 18 | |||
| 19 | my $poster = AnyEvent::Twitter->new( | ||
| 20 | consumer_key => $config{consumer_key}, | ||
| 21 | consumer_secret => $config{consumer_secret}, | ||
| 22 | access_token => $config{token}, | ||
| 23 | access_token_secret => $config{token_secret}, | ||
| 24 | ); | ||
| 25 | $self->{poster} = $poster; | ||
| 26 | |||
| 27 | if ($config{method} ne 'none') { | ||
| 28 | my %opts = ( | ||
| 29 | consumer_key => $config{consumer_key}, | ||
| 30 | consumer_secret => $config{consumer_secret}, | ||
| 31 | token => $config{token}, | ||
| 32 | token_secret => $config{token_secret}, | ||
| 33 | method => $config{method} || 'userstream', | ||
| 34 | ); | ||
| 35 | for my $param (qw/track follow locations/) { | ||
| 36 | $opts{$param} = $config{$param} if defined $config{$param}; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $listener = AnyEvent::Twitter::Stream->new( | ||
| 40 | %opts, | ||
| 41 | on_tweet => sub { | ||
| 42 | my $tweet = shift; | ||
| 43 | my $receive; $receive = AnySan::Receive->new( | ||
| 44 | provider => 'twitter', | ||
| 45 | event => 'timeline', | ||
| 46 | message => $tweet->{text}, | ||
| 47 | nickname => $config{nickname}, | ||
| 48 | from_nickname => $tweet->{user}->{screen_name}, | ||
| 49 | attribute => { | ||
| 50 | geo => $tweet->{geo}, | ||
| 51 | icon_url => $tweet->{user}->{profile_image_url}, | ||
| 52 | created_at => $tweet->{created_at}, | ||
| 53 | obj => $tweet, | ||
| 54 | }, | ||
| 55 | cb => sub { $self->event_callback($receive, @_) }, | ||
| 56 | ); | ||
| 57 | AnySan->broadcast_message($receive); | ||
| 58 | }, | ||
| 59 | timeout => $config{timeout}, | ||
| 60 | ); | ||
| 61 | $self->{listener} = $listener; | ||
| 62 | } | ||
| 63 | |||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub event_callback { | ||
| 68 | my($self, $receive, $type, @args) = @_; | ||
| 69 | |||
| 70 | if ($type eq 'reply') { | ||
| 71 | $self->{poster}->request( | ||
| 72 | api => 'statuses/update', | ||
| 73 | method => 'POST', | ||
| 74 | params => { | ||
| 75 | status => $args[0], | ||
| 76 | }, | ||
| 77 | sub {} | ||
| 78 | ); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub send_message { | ||
| 83 | my($self, $message, %args) = @_; | ||
| 84 | |||
| 85 | $self->{poster}->request( | ||
| 86 | api => 'statuses/update', | ||
| 87 | method => 'POST', | ||
| 88 | params => { | ||
| 89 | status => $message, | ||
| 90 | %{ $args{params} || +{} }, | ||
| 91 | }, | ||
| 92 | sub {} | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | __END__ |