LaraSlack By Tom Mountain

Introduction

Occasionally i come across packages that are small but incredibly helpful. So this package I'm going to go over today was built by my work mate @tommountain_ the lead developer at the company we work at. The package itself is a simple Slack integration four your Laravel Project.

Lets get started!

First off, we need to ensure we have Slack setup with the web hook ready to go, i wont go into the full detail of it but you can find the information here: Slack Webhook API

Essentially you need to:

  • Create a Channel
  • Enable WebHooks
  • Create a WebHook
  • Use webhook (were going to be adding ours to the .env as per LaraSlack Docs)

Next, as with most packages, it can be installed with composer. The docs are nice and simple for the package also and can be checked out here: LaraSlack

composer require mountain/laraslack

If were following the doc's we then move onto adding the helper to our project. If your using the standard Laravel helpers.php just add the following lines to it.

function laraslack($content)
{
    return new \ThomasMountain\LaraSlack\LaraSlack($content);
}

Installation of package done! not to add the config items to our .env

.ENV Setup

We can override some of the defaults but for the purpose of the tutorial were going to stick with the defaults:

SLACK_WEB_HOOK_URL="<Your Webhook Here>"
SLACK_CHANNEL=@raspada-contact-form
SLACK_USERNAME="Raspada Contact Form"
SLACK_ICON=:slightly_smiling_face:
SLACK_SEND=true

These are the minimum of what is required in the env.

So how do we use it?

This is where it gets really fun, as you can be creative :D, for the standard integration you can simple do something like this (I'm adding it to my contact form for when a message is sent).

$slack_message = "You have had a message on the contact form. The sender address is " . $this->email . " with a Subject of " . $this->subject . " and the message is: " . $this->comment;

\laraslack($slack_message);

No when the contact form is submitted, a slack message will be sent to my raspada-blog-contact channel like below:

And there you have it, few mins and you have a basic Slack integration setup!

Final Words

I am going to be writing one up for Hashnode but I'm still ironing out a kink with the Tags submissions, The rest of the submission works perfectly... just not the tags. Also there may be cleaner ways to make these Functions but im still on my learning Journey.

if you found this post useful please consider following me on twitter @skino2020 and if you found it really helpful consider buying me a coffee here.