AnyEvent-SparkBot

 view release on metacpan or  search on metacpan

lib/AnyEvent/SparkBot.pm  view on Meta::CPAN

  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,
);

has retries=>(
  is=>'ro',
  isa=>HashRef,
  lazy=>1,
  default=>sub { {} },
  required=>0,
);

has reconnect_sleep=>(
  is=>'ro',
  isa=>Int,
  default=>10,
  required=>1,
);

has reconnect=>(
  is=>'ro',
  isa=>Bool,
  default=>1,
  required=>1,
);

has pingEvery=>(
  is=>'ro',
  isa=>Int,
  default=>60,
);

has pingWait=>(
  is=>'ro',
  isa=>Int,
  default=>10,
);

has ping=>(
  is=>'rw',
);

has lastPing=>(
  is=>'rw',
  isa=>Str,
  lazy=>1,
);

has connInfo=>(
  is=>'rw',
  lazy=>1,

lib/AnyEvent/SparkBot.pm  view on Meta::CPAN


has defaultUrl=>(
  is=>'ro',
  isa=>Str,
	default=>'https://wdm-a.wbx2.com/wdm/api/v1/devices',
	#default=>'https://webexapis.com/wdm/api/v1/devices',
);

has lastConn=>(
  isa=>Str,
  is=>'ro',
  required=>1,
  default=>'/tmp/sparkBotLastConnect.json',
);

has connection=>(
  is=>'rw',
  isa=>Object,
  required=>0,
);

has on_message=>(
  is=>'ro',
  isa=>CodeRef,
  required=>1,
);

has spark=>(
  is=>'rw',
  isa=>Object,
  required=>0,
  lazy=>1,
);

has currentUser=>(
  is=>'rw',
  isa=>HashRef,
  required=>0,
  lazy=>1,
  default=>sub {return {}}
);

=head1 OO Arguments and accessors

Required Argument(s)

  token: The token used to authenticate the bot
  on_message: code ref used to handle incomming messages

Optional Arguments

  reconnect: default is true
  logger: null(default) or an instance of log4perl::logger
  lastConn: location to the last connection file
    # it may be a very good idea to set this value
    # default: /tmp/sparkBotLastConnect.json
  defaultUrl: https://wdm-a.wbx2.com/wdm/api/v1/devices
    # this is where we authenticate and pull the websocket url from
  deviceDesc: JSON hash, representing the client description
  agent: an instance of AnyEvent::HTTP::MultiGet
  retryTimeout: default 10, sets how long to wait afer getting a 429 error
  retryCount: default 1, sets how many retries when we get a 429 error

Timout and retry values:

  pingEvery: 60 # used to check how often we run a ping
    # pings only happen if no inbound request has come in for 
    # the interval
  pingWait: 10 
    # how long to wait for a ping response
  reconnect_sleep: 10
    # how long to wait before we try to reconnect

Objects set at runtime:

  lastConn: sets the location of the last connection file
  ping: sets an object that will wake up and do something
  lastPing: contains the last ping string value
  connection: contains the current websocket connection if any
  spark: Instance of AnyEvent::HTTP::Spark
  currentUser: Hash ref representing the current bot user

=cut

# This method runs after the new constructor
sub BUILD {
  my ($self)=@_;

  my $sb=new AnyEvent::HTTP::Spark(agent=>$self->agent,token=>$self->token);
  $self->spark($sb);
}

# this method runs before the new constructor, and can be used to change the arguments passed to the module
around BUILDARGS => sub {
  my ($org,$class,@args)=@_;
  
  return $class->$org(@args);
};

=head1 OO Methods

=over 4

=item * my $result=$self->new_true({qw( some data )});

Returns a new true Data::Result object.

=item * my $result=$self->new_false("why this failed")

Returns a new false Data::Result object

=item * my $self->start_connection() 

Starts the bot up.

=cut

sub start_connection : BENCHMARK_DEBUG {
  my ($self)=@_;

  my $url=$self->connInfo->{webSocketUrl};



( run in 0.524 second using v1.01-cache-2.11-cpan-39bf76dae61 )