AnyEvent-Mattermost
view release on metacpan or search on metacpan
lib/AnyEvent/Mattermost.pm view on Meta::CPAN
);
$self->{'client'}->connect($wss_url)->cb(sub {
my $client = shift;
my $conn = try {
$client->recv;
}
catch {
die $_;
};
$self->{'started'}++;
$self->{'conn'} = $conn;
$conn->on(each_message => sub { $self->_handle_incoming(@_) });
});
}
=head2 stop
$mconn->stop();
Closes connection with Mattermost server and ceases processing messages.
Callbacks which have been registered are left in place in case you wish to
start() the connection again.
If you wish to remove callbacks, without disposing of the AnyEvent::Mattermost
object itself, you will need to call on() and pass C<undef> for each events'
callback value (rather than the anonymous subroutines you had provided when
registering them).
=cut
sub stop {
my ($self) = @_;
$self->{'conn'}->close;
}
=head2 on
$mconn->on( $event1 => sub {}, [ $event2 => sub {}, ... ] );
Registers a callback for the named event type. Multiple events may be registered
in a single call to on(), but only one callback may exist for any given event
type. Any subsequent callbacks registered to an existing event handler will
overwrite the previous callback.
Every callback will receive two arguments: the AnyEvent::Mattermost object and
the raw message data received over the Mattermost WebSockets connection. This
message payload will take different forms depending on the type of event which
occurred, but the top-level data structure is always a hash reference with at
least the key C<event> (with a value matching that which you used to register
the callback). Most event types include a C<data> key, whose value is a hash
reference containing the payload of the event. For channel messages this will
include things like the sender's name, the channel name and type, and of course
the message itself.
For more explanation of event types, hope that the Mattermost project documents
them at some point. For now, L<Data::Dumper> based callbacks are your best bet.
=cut
sub on {
my ($self, %registrations) = @_;
foreach my $type (keys %registrations) {
my $cb = $registrations{$type};
$self->{'registry'}{$type} = $cb;
}
}
=head2 send
$mconn->send( \%message );
Posts a message to the Mattermost server. This method is currently fairly
limited and supports only providing a channel name and a message body. There
are formatting, attachment, and other features that are planned to be
supported in future releases.
The C<\%message> hash reference should contain at bare minimum two keys:
=over 4
=item * channel
The name of the channel to which the message should be posted. This may be
either the short name (which appears in URLs in the web UI) or the display
name (which may contain spaces). In the case of conflicts, the display name
takes precedence, on the theory that it is the most enduser-visible name of
channels and thus the least surprising.
=item * message
The body of the message to be posted. This may include any markup options that
are supported by Mattermost, which includes a subset of the Markdown language
among other things.
=back
To announce your presence to the default Mattermost channel (Town Square, using
its short name), you might call the method like this:
$mconn->send({ channel => "town-square", message => "Hey everybody!" });
=cut
sub send {
my ($self, $data) = @_;
croak "cannot send message because connection has not yet started"
unless $self->started;
croak "send payload must be a hashref"
unless defined $data && ref($data) eq 'HASH';
croak "message must be a string of greater than zero bytes"
unless exists $data->{'message'} && !ref($data->{'message'}) && length($data->{'message'}) > 0;
croak "message must have a destination channel"
unless exists $data->{'channel'} && length($data->{'channel'}) > 0;
( run in 1.721 second using v1.01-cache-2.11-cpan-5837b0d9d2c )