NAME

AnyEvent::Lingr - Asynchronous Lingr client.

SYNOPSIS

use AnyEvent;
use AnyEvent::Lingr;

my $lingr = AnyEvent::Lingr->new(
    user     => 'your lingr username',
    password => 'your lingr password',
    api_key  => 'your lingr api_key', # optional
);

# error handler
$lingr->on_error(sub {
    my ($msg) = @_;
    warn 'Lingr error: ', $msg;

    # reconnect after 5 seconds,
    my $t; $t = AnyEvent->timer(
        after => 5,
        cb    => sub {
            $lingr->start_session;
            undef $t;
        },
    );
});

# room info handler
$lingr->on_room_info(sub {
    my ($rooms) = @_;

    print "Joined rooms:\n";
    for my $room (@$rooms) {
        print "  $room->{id}\n";
    }
});

# event handler
$lingr->on_event(sub {
    my ($event) = @_;

    # print message
    if (my $msg = $event->{message}) {
        print sprintf "[%s] %s: %s\n",
            $msg->{room}, $msg->{nickname}, $msg->{text};
    }
});

# start lingr session
$lingr->start_session;

DESCRIPTION

AnyEvent::Lingr is asynchronous client interface for Lingr.

METHODS

new(%options)

Create AnyEvent::Lingr object. Available %options are:

  • user => 'Str' (required)

    Lingr username

  • password => 'Str' (required)

    Lingr password

  • api_key => 'Str' (optional)

    Lingr api_key.

  • session => 'Str' (optional)

    Lingr session key. If this parameter is passed, this module try to reuse this key for calling session/verify api, otherwise create new session.

my $lingr = AnyEvent::Lingr->new(
    user     => 'your lingr username',
    password => 'your lingr password',
    api_key  => 'your lingr api_key', # optional
);

start_session

Start lingr chat session.

This method runs following sequences:

1. Create session (or verify session if session parameter was passed)
2. Get joined room list, and then fire on_room_info callback.
3. Subscribe all joined room events, and wait events...
4. When some events is occurred, fire on_event callback
5. goto step 3.

For stopping this loop, you just destroy lingr object by doing:

undef $lingr;

For updating subscription list, you can use update_room_info method:

$lingr->update_room_info;

update_room_info

Update joined room info, and fire on_room_info callback. This method also update subscription rooms which is target room for on_event callback.

say($room, $message)

Say something to lingr room.

$lingr->say('perl_jp', 'hi!');

CALLBACKS

This module supports following three callbacks:

  • on_error->($msg)

  • on_room_info->($rooms)

  • on_event->($event)

All callbacks can be set by accessor:

$lingr->on_error(sub { ... });

Or by constructor:

my $lingr = AnyEvent::Lingr->new(
    ...
    on_error => sub { ... },
);

on_error->($msg)

Error callbacks.

$msg is error message. If this message is form of "\d\d\d: message" like:

596: Operation timed out

This is http level or connection level error. Otherwise $msg is error message returned from lingr api server.

Both case, lingr session was closed before this callback, so you can restart session in this callback:

$lingr->on_error(sub {
    my ($msg) = @_;
    warn 'Lingr error: ', $msg;

    # reconnect after 5 seconds,
    my $t; $t = AnyEvent->timer(
        after => 5,
        cb    => sub {
            $lingr->start_session;
            undef $t;
        },
    );
});

on_room_info->($rooms)

Fired when after start_session or after update_room_info method.

$rooms is ArrayRef of room information you joined.

on_event->($event)

Fired when some events is occurred in your subscribed rooms.

AUTHOR

Daisuke Murase <typester@cpan.org>

COPYRIGHT AND LICENSE

Copyright (c) 2013 Daisuke Murase All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.