Commit b48162cc authored by Mike Jones's avatar Mike Jones 🌶

Add Mattermost bot first part

parent 367a1149
Pipeline #153 failed with stage
in 15 seconds
title: "Building a Mattermost Chatbot in Perl"
date: 2019-01-09T17:53:11Z
draft: false
type: post
authors: [ 'Mike Jones' ]
tags: [ 'Perl 5', 'Chatops', 'Bot', 'WebService::Mattermost', 'Tutorial' ]
description: |
This article provides a brief overview of how to set up a Mattermost chatbot
in Perl 5.
## Introduction
[WebService::Mattermost]( is an
alternative [Mattermost]( chatbot library written in
Perl. It provides two interfaces to the Mattermost API and WebSocket gateway:
from a script, and as a base for [Moo]( and
[Moose]( classes. In this article, I will cover
how to use it in a basic script, and demonstrate with a bot which greets users.
## Requirements
Installable from [CPAN](
* [`WebService::Mattermost`](
## Final code
For anyone who doesn't want to read, here is the final product. You will need to
change the authentication values in the constructor for
to your own Mattermost server and bot user.
{{< gist n7st d73a03a0aa17b731fd31e2b4fa219e6c "" >}}
## Breakdown
* First, as with all good Perl scripts, we need to enable strictures and
* Then, we instantiate the bot class with three required configuration values:
- `username`: your Mattermost username (e.g. "")
- `password`: your Mattermost password (e.g. "hunter2")
- `base_url`: the base URL of your Mattermost server's API (e.g.
{{< gist n7st d73a03a0aa17b731fd31e2b4fa219e6c "" >}}
* Next, we add a non-blocking loop which watches for messages from the gateway.
* Received messages include two arguments: `$bot`, which contains our instance
of `WebService::Mattermost::V4::Client`, and `$args`, which contains the
message from the gateway.
{{< gist n7st d73a03a0aa17b731fd31e2b4fa219e6c "" >}}
* Finally, we start the bot.
{{< gist n7st d73a03a0aa17b731fd31e2b4fa219e6c "" >}}
## Extending
In the bot's event loops, there are some additional utilites, accessible through
the `bot` argument:
- `$bot->api`: a full Mattermost API integration
- `$bot->ua`: an instance of [Mojo::UserAgent](
- `$bot->logger`: an instance of [Mojo::Log](
......@@ -4,10 +4,15 @@
{{ partial "post_meta.html" . }}
{{ if .Description }}<p>{{ .Description }}</p><hr>{{ end }}
<div class="card-body">
{{ if .Description }}
<section class="post-content" id="post-description">
<p>{{ .Description }}</p>
{{ end }}
<section class="post-content">
{{ .Content }}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment