AnyEvent-Mattermost

 view release on metacpan or  search on metacpan

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


=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;

    my $team_id = $self->{'teamdata'}{'id'};
    my $user_id = $self->{'userdata'}{'id'};
    my $channel_id = $self->_get_channel_id($data->{'channel'});

    my $create_at = int(time() * 1000);

    my $res = $self->_post('api/v3/teams/' . $team_id . '/channels/' . $channel_id . '/posts/create', {
        user_id         => $user_id,
        channel_id      => $channel_id,
        message         => $data->{'message'},
        create_at       => $create_at+0,
        filenames       => [],
        pending_post_id => $user_id . ':' . $create_at,
    });
}


=head1 INTERNAL METHODS

The following methods are not intended to be used by code outside this module,
and their signatures (even their very existence) are not guaranteed to remain
stable between versions. However, if you're the adventurous type ...

=cut

=head2 ping

    $mconn->ping();

Pings the Mattermost server over the WebSocket connection to maintain online
status and ensure the connection remains alive. You should not have to call
this method yourself, as start() sets up a ping callback on a timer for you.

=cut

sub ping {
    my ($self) = @_;

    $self->{'conn'}->send("ping");
}

=head2 started

    $mconn->started();

Returns a boolean status indicating whether the Mattermost WebSockets API
connection has started yet.

=cut

sub started {
    my ($self) = @_;

    return $self->{'started'} // 0;
}

=head1 LIMITATIONS

=over 4

=item * Only basic message sending and receiving is currently supported.

=back

=head1 CONTRIBUTING

If you would like to contribute to this module, report bugs, or request new
features, please visit the module's official GitHub project:

L<https://github.com/jsime/anyevent-mattermost>

=head1 AUTHOR

Jon Sime <jonsime@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Jon Sime.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut

sub _do {
    my ($self, $type, @args) = @_;

    if (defined $self->{'registry'}{$type}) {
        $self->{'registry'}{$type}->($self, @args);
    }
}



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