AnyEvent-SlackBot
view release on metacpan or search on metacpan
lib/AnyEvent/SlackBot.pm view on Meta::CPAN
isa=>Object,
required=>0,
);
has bot_id=>(
is=>'rw',
isa=>Str,
required=>0,
);
has keep_alive_timeout =>(
is=>'ro',
isa=>Int,
requried=>1,
default=>15,
);
# This method runs after the new constructor
sub BUILD {
my ($self)=@_;
lib/AnyEvent/SlackBot.pm view on Meta::CPAN
$self->connection($connection);
if($@) {
$self->log_error("Failed to cnnect to our web socket, error was: $@");
return $self->handle_reconnect;
}
$self->stats->{last_connected_on}=time;
$self->stats->{total_connections}++;
$self->stats->{last_msg_on}=time;
$self->{timer}=AnyEvent->timer(
interval=>$self->keep_alive_timeout,
after=>$self->keep_alive_timeout,
cb=>sub {
my $max_timeout=$self->stats->{last_msg_on} + 3 * $self->keep_alive_timeout;
if(time < $max_timeout) {
if(time > $self->stats->{last_msg_on} + $self->keep_alive_timeout) {
$self->log_info("sending keep alive to server");
$connection->send(to_json({
id=>$self->next_id,
type=>'ping',
timestamp=>time,
}));
%{$self->{ignore}}=();
$self->on_idle->($self);
$self->stats->{last_idle_on}=time;
}
} else {
lib/AnyEvent/SlackBot.pm view on Meta::CPAN
my ($connection,$message)=@_;
$self->stats->{last_msg_on}=time;
$self->stats->{total_messages_recived}++;
if($message->is_text) {
my $ref=eval { from_json($message->body) };
if($@) {
$self->log_error("Failed to parse json body, error was: $@");
return $self->handle_reconnect;
}
if(exists $ref->{type} and $ref->{type} eq 'pong') {
$self->log_info("got keep alive response from server");
} else {
if($self->stats->{running_posts}!=0) {
# Don't try to handle unknown commands while we are waiting on a post to go out!
push @{$self->unknown_que},[$self,$ref,$data];
$self->log_info("HTTP Post response pending.. will hold off on responding to commands until we know if we sent it or not");
return;
} else {
return if $self->we_sent_msg($ref,$data);
$self->log_info("real time response");
$self->debug('Inbound message: ',Dumper($ref));
lib/AnyEvent/SlackBot.pm view on Meta::CPAN
if($self->auto_reconnect) {
my $result=$self->connect_and_run;
if($result) {
$self->log_info("auto reconnected without an error, flushing backlog of outbound messages");
while(my $msg=shift @{$self->{backlog}}) {
$self->send($msg);
}
} else {
$self->log_error("Failed to reconnect will try again in 15 seconds, error was: $result");
$self->{timer}=AnyEvent->timer(
interval=>$self->keep_alive_timeout,
after=>$self->keep_alive_timeout,
cb=>sub { $self->handle_reconnect },
);
}
}
}
=back
=head1 See Also
( run in 0.806 second using v1.01-cache-2.11-cpan-df04353d9ac )