AnyEvent-SparkBot
view release on metacpan or search on metacpan
lib/AnyEvent/SparkBot.pm view on Meta::CPAN
package AnyEvent::SparkBot;
our $VERSION='1.017';
use Modern::Perl;
use Moo;
use MooX::Types::MooseLike::Base qw(:all);
use Scalar::Util qw( looks_like_number);
use Data::Dumper;
use namespace::clean;
use AnyEvent::HTTP::MultiGet;
use AnyEvent::WebSocket::Client;
use MIME::Base64;
use JSON;
use AnyEvent::HTTP::Spark;
BEGIN {
no namespace::clean;
with 'HTTP::MultiGet::Role', 'AnyEvent::SparkBot::SharedRole';
}
=head1 NAME
AnyEvent::SparkBot - Cisco Spark WebSocket Client for the AnyEvent Loop
=head1 SYNOPSIS
use Modern::Perl;
use Data::Dumper;
use AnyEvent::SparkBot;
use AnyEvent::Loop;
$|=1;
our $obj=new AnyEvent::SparkBot(token=>$ENV{SPARK_TOKEN},on_message=>\&cb);
$obj->que_getWsUrl(sub {
my ($agent,$id,$result)=@_;
# start here if we got a valid connection
return $obj->start_connection if $result;
$obj->handle_reconnect;
});
$obj->agent->run_next;
AnyEvent::Loop::run;
sub cb {
my ($sb,$result,$eventType,$verb,$json)=@_;
return unless $eventType eq 'conversation.activity' and $verb eq 'post';
# Data::Result Object is False when combination of EvenType and Verb are unsupprted
if($result) {
my $data=$result->get_data;
my $response={
roomId=>$data->{roomId},
personId=>$data->{personId},
text=>"ya.. ya ya.. I'm on it!"
};
# Proxy our lookup in a Retry-After ( prevents a lot of errors )
$obj->run_lookup('que_createMessage',(sub {},$response);
} else {
print "Error: $result\n";
}
}
=head1 DESCRIPTION
Connects to cisco spark via a websocket. By itself this class only provides connectivty to Spark, the on_message callback is used to handle events that come in. By default No hanlder is provided.
=head1 Moo Role(s)
This module uses the following Moo role(s)
HTTP::MultiGet::Role
AnyEvent::SparkBot::SharedRole
=cut
has retryTimeout=>(
is=>'ro',
isa=>Int,
default=>10,
lazy=>1,
);
has retryCount=>(
is=>'ro',
isa=>Int,
default=>1,
lazy=>1,
( run in 1.272 second using v1.01-cache-2.11-cpan-39bf76dae61 )