AnyEvent-SlackBot
view release on metacpan or search on metacpan
lib/AnyEvent/SlackBot.pm view on Meta::CPAN
);
has auto_reconnect=>(
is=>'rw',
isa=>Bool,
required=>1,
default=>1,
);
has connection=>(
is=>'rw',
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)=@_;
$self->{backlog}=[];
$self->{ignore}={};
$self->stats->{service_started_on}=time;
$self->stats->{running_posts}=0;
}
# 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 * $self->connect_and_run
COnnects and starts running
=cut
sub connect_and_run {
my ($self)=@_;
my $request=POST $self->rtm_start_url,[token=>$self->token];
my $ua=LWP::UserAgent->new;
$ua->ssl_opts(
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
SSL_hostname => '',
verify_hostname => 0
);
my $response=$ua->request($request);
$self->{timer}=undef;
if($response->code==200) {
my $data=eval { from_json($response->decoded_content) };
if($@) {
return $self->new_false("Failed to decode response, error was: $@");
}
unless(exists $data->{url} and $data->{self}) {
my $msg=exists $data->{error} ? $data->{error} : 'unknown slack error';
return $self->new_false("Failed to get valid connection info, error was: $msg");
}
$self->build_connection($data);
} else {
return $self->new_false("Failed to get conenction info from slack, error was: ".$response->status_line);
}
}
=item * my $id=$self->next_id
Provides an id for the next message.
=cut
sub next_id {
my ($self)=@_;
return ++$self->{next_id}
}
=item * if($self->is_connected) { ... }
Denotes if we are currently connected to slack
=cut
sub is_connected {
return defined($_[0]->connection)
}
=item * $self->send($ref)
Converts $ref to json and sends it on the session.
=cut
sub send {
my ($self,$ref)=@_;
my $json=to_json($ref);
if($self->connection) {
$self->connection->send($json);
++$self->stats->{total_messages_sent};
} else {
push @{$self->{backlog}},$json;
}
}
=item * $self->send_typing($json)
Given $json sends a currently typing reply
( run in 0.466 second using v1.01-cache-2.11-cpan-13bb782fe5a )