Changeset 927c186


Ignore:
Timestamp:
Jan 9, 2009, 2:58:18 PM (16 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.7, release-1.8, release-1.9
Children:
b4b5a7e
Parents:
da3596f
Message:
Support sending and receiving direct messages.
Location:
lib/BarnOwl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/BarnOwl/Message/Twitter.pm

    r8618438 r927c186  
    1717sub long_sender {"http://twitter.com/" . shift->sender}
    1818
    19 sub replycmd {'twitter'}
     19sub replycmd {
     20    my $self = shift;
     21    if($self->isprivate) {
     22        return $self->replysendercmd;
     23    } else {
     24        return 'twitter';
     25    }
     26}
     27
     28sub replysendercmd {
     29    my $self = shift;
     30    return 'twitter-direct ' . $self->sender;
     31}
    2032
    2133sub smartfilter {
  • lib/BarnOwl/Module/Twitter.pm

    r1f82df9 r927c186  
    6767    }
    6868);
     69
     70BarnOwl::new_variable_bool(
     71    'twitter:poll',
     72    {
     73        default => 1,
     74        summary => 'Poll Twitter for incoming messages',
     75        description => "If set, will poll Twitter every minute for normal updates,\n"
     76        . 'and every two minutes for direct message'
     77     }
     78 );
    6979
    7080my $conffile = BarnOwl::get_config_dir() . "/twitter";
     
    109119}
    110120
    111 my $last_poll = 0;
    112 my $last_id   = undef;
     121our $last_poll        = 0;
     122our $last_direct_poll = 0;
     123our $last_id          = undef;
     124our $last_direct      = undef;
     125
    113126unless(defined($last_id)) {
    114     $last_id = $twitter->friends_timeline({count => 1})->[0]{id};
     127    eval {
     128        $last_id = $twitter->friends_timeline({count => 1})->[0]{id};
     129    };
     130}
     131
     132unless(defined($last_direct)) {
     133    eval {
     134        $last_direct = $twitter->direct_messages()->[0]{id};
     135    };
    115136}
    116137
    117138sub poll_messages {
    118     return unless ( time - $last_poll ) >= 45;
     139    poll_twitter();
     140    poll_direct();
     141}
     142
     143sub twitter_error {
     144    my $ratelimit = $twitter->rate_limit_status;
     145    unless(defined($ratelimit)) {
     146        # Twitter's just sucking, sleep for 5 minutes
     147        $last_direct_poll = $last_poll = time + 60*5;
     148        die("Twitter seems to be having problems.\n");
     149    }
     150    if($ratelimit->{remaining_hits} <= 0) {
     151        $last_direct_poll = $last_poll = $ratelimit->{reset_time_in_seconds};
     152        die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n");
     153    }
     154}
     155
     156sub poll_twitter {
     157    return unless ( time - $last_poll ) >= 60;
    119158    $last_poll = time;
     159    return unless BarnOwl::getvar('twitter:poll') eq 'on';
     160
    120161    my $timeline = $twitter->friends_timeline( { since_id => $last_id } );
    121162    unless(defined($timeline)) {
    122         BarnOwl::error("Twitter returned error ... rate-limited?");
    123         # Sleep for 15 minutes
    124         $last_poll = time + 60*15;
     163        twitter_error();
    125164        return;
    126165    };
     
    147186}
    148187
     188sub poll_direct {
     189    return unless ( time - $last_direct_poll) >= 120;
     190    $last_direct_poll = time;
     191    return unless BarnOwl::getvar('twitter:poll') eq 'on';
     192
     193    my $direct = $twitter->direct_messages( { since_id => $last_direct } );
     194    unless(defined($direct)) {
     195        twitter_error();
     196        return;
     197    };
     198    if ( scalar @$direct ) {
     199        for my $tweet ( reverse @$direct ) {
     200            if ( $tweet->{id} <= $last_direct ) {
     201                next;
     202            }
     203            my $msg = BarnOwl::Message->new(
     204                type      => 'Twitter',
     205                sender    => $tweet->{sender}{screen_name},
     206                recipient => $cfg->{user} || $user,
     207                direction => 'in',
     208                location  => decode_entities($tweet->{sender}{location}||""),
     209                body      => decode_entities($tweet->{text}),
     210                isprivate => 'true'
     211               );
     212            BarnOwl::queue_message($msg);
     213        }
     214        $last_direct = $direct->[0]{id};
     215    } else {
     216        # BarnOwl::message("No new tweets...");
     217    }
     218}
     219
    149220sub twitter {
    150221    my $msg = shift;
     222    if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) {
     223        twitter_direct($1, $2);
     224    } elsif(defined $twitter) {
     225        $twitter->update($msg);
     226    }
     227}
     228
     229sub twitter_direct {
     230    my $who = shift;
     231    my $msg = shift;
    151232    if(defined $twitter) {
    152         $twitter->update($msg);
     233        $twitter->new_direct_message({
     234            user => $who,
     235            text => $msg
     236           });
    153237    }
    154238}
     
    159243    description => 'Update Twitter. If MESSAGE is provided, use it as your status.'
    160244    . "\nOtherwise, prompt for a status message to use."
     245   });
     246
     247BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, {
     248    summary     => 'Send a Twitter direct message',
     249    usage       => 'twitter-direct USER',
     250    description => 'Send a Twitter Direct Message to USER'
    161251   });
    162252
     
    171261}
    172262
     263sub cmd_twitter_direct {
     264    my $cmd = shift;
     265    my $user = shift;
     266    die("Usage: $cmd USER\n") unless $user;
     267    BarnOwl::start_edit_win("$cmd $user", sub{twitter_direct($user, shift)});
     268}
     269
    173270eval {
    174271    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
Note: See TracChangeset for help on using the changeset viewer.