Building a Mattermost Chatbot in Perl

By Mike Jones 09 Jan 2019, 17:53

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:

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 WebService::Mattermost::V4::Client to your own Mattermost server and bot user.

Breakdown

  • First, as with all good Perl scripts, we need to enable strictures and warnings.
  • Then, we instantiate the bot class with three required configuration values:
  • 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.
  • Finally, we start the bot.

Extending

In the bot’s event loops, there are some additional utilites, accessible through the bot argument: