Last active
January 9, 2019 20:04
-
-
Save n7st/d73a03a0aa17b731fd31e2b4fa219e6c to your computer and use it in GitHub Desktop.
WebService::Mattermost - example greeting bot for Mattermost (Perl)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use WebService::Mattermost::V4::Client; | |
my $bot = WebService::Mattermost::V4::Client->new({ | |
username => 'MATTERMOST USERNAME HERE', | |
password => 'MATTERMOST PASSWORD HERE', | |
base_url => 'https://my.mattermost-server.com/api/v4/', | |
debug => 1, # Show extra connection information | |
}); | |
# Triggered on every message received by the gateway | |
$bot->on(gw_message => sub { | |
my ($bot, $args) = @_; | |
# Only post-like messages contain post_data | |
if ($args->{post_data}->{message} && lc $args->{post_data}->{message} eq 'hello') { | |
# Respond with a friendly greeting | |
$bot->api->posts->create({ | |
message => 'Hi, @'.$args->{message}->{data}->{sender_name}, | |
channel_id => $args->{post_data}->{channel_id}, | |
}); | |
} | |
}); | |
# Start the bot | |
$bot->start(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Triggered on every message received by the gateway | |
$bot->on(gw_message => sub { | |
my ($bot, $args) = @_; | |
# Only post-like messages contain post_data | |
if ($args->{post_data}->{message} && lc $args->{post_data}->{message} eq 'hello') { | |
# Respond with a friendly greeting | |
$bot->api->posts->create({ | |
message => 'Hi, @'.$args->{message}->{data}->{sender_name}, | |
channel_id => $args->{post_data}->{channel_id}, | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$bot->start(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use WebService::Mattermost::V4::Client; | |
my $bot = WebService::Mattermost::V4::Client->new({ | |
username => 'MATTERMOST USERNAME HERE', | |
password => 'MATTERMOST PASSWORD HERE', | |
base_url => 'https://my.mattermost-server.com/api/v4/', | |
debug => 1, # Show extra connection information | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WebService::Mattermost source code