| 1 | package WebService::Mattermost::V4::Example::Bot; | ||
| 2 | |||
| 3 | # ABSTRACT: An example Perl Mattermost bot. | ||
| 4 | |||
| 5 | use Moo; # or Moose | ||
| 6 | |||
| 7 | extends 'WebService::Mattermost::V4::Client'; | ||
| 8 | |||
| 9 | ################################################################################ | ||
| 10 | |||
| 11 | sub gw_ws_started { | ||
| 12 | my $self = shift; | ||
| 13 | my $args = shift; | ||
| 14 | |||
| 15 | if ($self->debug) { | ||
| 16 | $self->logger->debug('Pingbot is alive'); | ||
| 17 | } | ||
| 18 | |||
| 19 | return 1; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub gw_ws_finished { | ||
| 23 | my $self = shift; | ||
| 24 | my $args = shift; | ||
| 25 | |||
| 26 | if ($self->debug) { | ||
| 27 | $self->logger->debugf('Pingbot disconnected: [%d] %s', $args->{code}, $args->{reason}); | ||
| 28 | } | ||
| 29 | |||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub gw_message { | ||
| 34 | my $self = shift; | ||
| 35 | my $args = shift; | ||
| 36 | |||
| 37 | # message_has_data() checks whether we have the "message" item in post_data | ||
| 38 | if ($self->message_has_content($args) && $args->{post_data}->{message} =~ /ping/i) { | ||
| 39 | # Use the "posts" resource from the API integration to send a message | ||
| 40 | # back to the channel | ||
| 41 | $self->api->posts->create({ | ||
| 42 | channel_id => $args->{post_data}->{channel_id}, | ||
| 43 | message => 'Pong', | ||
| 44 | }); | ||
| 45 | } | ||
| 46 | |||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub gw_ws_error { | ||
| 51 | my $self = shift; | ||
| 52 | my $args = shift; | ||
| 53 | |||
| 54 | if ($self->debug) { | ||
| 55 | $self->logger->debugf('Oh no!: %s', $args->{message}); | ||
| 56 | } | ||
| 57 | |||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub gw_message_no_event { | ||
| 62 | my $self = shift; | ||
| 63 | my $args = shift; | ||
| 64 | |||
| 65 | if ($self->debug) { | ||
| 66 | $self->logger->debug('Received a message without an event. Probably a ping.'); | ||
| 67 | } | ||
| 68 | |||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | ################################################################################ | ||
| 73 | |||
| 74 | 1; | ||
| 75 | |||
| 76 | __END__ | ||
| 77 | |||
| 78 | =pod | ||
| 79 | |||
| 80 | =encoding UTF-8 | ||
| 81 | |||
| 82 | =head1 NAME | ||
| 83 | |||
| 84 | WebService::Mattermost::V4::Example::Bot - An example Perl Mattermost bot. | ||
| 85 | |||
| 86 | =head1 VERSION | ||
| 87 | |||
| 88 | version 0.28 | ||
| 89 | |||
| 90 | =head1 DESCRIPTION | ||
| 91 | |||
| 92 | An example Mattermost WebSocket bot using L<WebService::Mattermost>. The bot connects | ||
| 93 | to the network, and responds to "Ping" messages with "Pong". | ||
| 94 | |||
| 95 | =head2 USAGE | ||
| 96 | |||
| 97 | #!/usr/bin/env perl | ||
| 98 | |||
| 99 | use strict; | ||
| 100 | use warnings; | ||
| 101 | |||
| 102 | use WebService::Mattermost::V4::Example::Bot; | ||
| 103 | |||
| 104 | my $bot = WebService::Mattermost::V4::Example::Bot->new({ | ||
| 105 | username => 'usernamehere', | ||
| 106 | password => 'passwordhere', | ||
| 107 | base_url => 'https://my.mattermost.server.com/api/v4/' | ||
| 108 | debug => 1, # optional | ||
| 109 | })->start(); | ||
| 110 | |||
| 111 | The C<gw_> methods in this class are events emitted by | ||
| 112 | L<WebService::Mattermost::V4::Client>. Each one has one argument passed which is a HashRef | ||
| 113 | of decoded data from the Mattermost server. | ||
| 114 | |||
| 115 | =head2 METHODS | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item C<gw_ws_started()> | ||
| 120 | |||
| 121 | Triggered when the bot connects to the Mattermost gateway. | ||
| 122 | |||
| 123 | =item C<gw_ws_finished()> | ||
| 124 | |||
| 125 | Triggered when the connection to the gateway closes. | ||
| 126 | |||
| 127 | =item C<gw_message()> | ||
| 128 | |||
| 129 | Triggered when a message with an event is received from the gateway. | ||
| 130 | |||
| 131 | =item C<gw_ws_error()> | ||
| 132 | |||
| 133 | Triggered when an error is received from the gateway. | ||
| 134 | |||
| 135 | =item C<gw_message_no_event()> | ||
| 136 | |||
| 137 | Triggered when a message without an event is received from the gateway. | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =head1 AUTHOR | ||
| 142 | |||
| 143 | Mike Jones <mike@netsplit.org.uk> | ||
| 144 | |||
| 145 | =head1 COPYRIGHT AND LICENSE | ||
| 146 | |||
| 147 | This software is Copyright (c) 2020 by Mike Jones. | ||
| 148 | |||
| 149 | This is free software, licensed under: | ||
| 150 | |||
| 151 | The MIT (X11) License | ||
| 152 | |||
| 153 | =cut |