Changeset 8618438


Ignore:
Timestamp:
Nov 28, 2008, 12:29:38 PM (12 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.7, release-1.8, release-1.9
Children:
affa874
Parents:
104f7d9
Message:
Implement receiving messages
Location:
lib/BarnOwl
Files:
1 added
1 edited

Legend:

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

    r104f7d9 r8618438  
    1919use BarnOwl;
    2020use BarnOwl::Hooks;
     21use BarnOwl::Message::Twitter;
     22use HTML::Entities;
    2123
    2224my $twitter;
     
    102104}
    103105
     106my $last_poll = 0;
     107my $last_id   = undef;
     108unless(defined($last_id)) {
     109    $last_id = $twitter->friends_timeline({count => 1})->[0]{id};
     110}
     111
     112sub poll_messages {
     113    return unless ( time - $last_poll ) >= 45;
     114    $last_poll = time;
     115    my $timeline = $twitter->friends_timeline( { since_id => $last_id } );
     116    unless(defined($timeline)) {
     117        BarnOwl::error("Twitter returned error ... rate-limited?");
     118        # Sleep for 15 minutes
     119        $last_poll = time + 60*15;
     120        return;
     121    };
     122    if ( scalar @$timeline ) {
     123        for my $tweet ( reverse @$timeline ) {
     124            if ( $tweet->{id} <= $last_id ) {
     125                next;
     126            }
     127            my $msg = BarnOwl::Message->new(
     128                type      => 'Twitter',
     129                sender    => $tweet->{user}{screen_name},
     130                recipient => $cfg->{user} || $user,
     131                direction => 'in',
     132                source    => decode_entities($tweet->{source}),
     133                location  => decode_entities($tweet->{user}{location}),
     134                body      => decode_entities($tweet->{text})
     135               );
     136            BarnOwl::queue_message($msg);
     137        }
     138        $last_id = $timeline->[0]{id};
     139    } else {
     140        # BarnOwl::message("No new tweets...");
     141    }
     142}
     143
    104144sub twitter {
    105145    my $msg = shift;
     
    128168eval {
    129169    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
     170    $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages");
    130171};
    131172if($@) {
    132173    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
     174    $BarnOwl::Hooks::mainLoop->add(\&poll_messages);
    133175}
    134176
     177BarnOwl::command(qw(filter twitter type ^twitter$));
     178
    1351791;
Note: See TracChangeset for help on using the changeset viewer.