Nasrul Hazim Bin Mohamad

Laravel: Notification to Slack

Feb
02

Assalamualaikum / Hi,

Today I will show you how to make a notification to Slack channel.

The approach slightly different you may came across on the internet, but they key point, still rely on Laravel Notification. It’s just how I manage my Slack service.

Configuration

First, we going to start with setting up our environment for Slack Notification by adding the Slack service configuration. Add new services in config/services.php:

Then create a webhook URL in Incoming Webhooks.

In your .env, add SLACK_WEBHOOK_URL, and add the webhook you just created above.

Once you’re done, in your terminal, don’t forget to run php artisan config:cache.

Service

Next step is to setup our Slack service and helper.

The service basically it’s just a simple class to have a webhook URL store in it, and a helper make it simpler to call the Slack service.

We going to create a service class in app/Services/Slack.php:

Then create a helper in app/Support, call it helpers.php. Then add the following and then make sure in composer.json, to load the app/Support/helpers.php:

Then composer dumpautoload -o once you have added the helper.

Notifiable Trait

Laravel provide a trait Illuminate\Notifications\Notifiable, which you can use it anywhere in any classes you want.

In our case, I want to create a class called Notify.php in app/Models.

Did you notice how I use slack() helper to get the Slack Webhook URL? Much convenient. You may want to extend the Slack service class in future, but in our case, we don’t need any as for now.

But again, you can simplify that by using env() function to get the Slack Webhook URL instead of having configuration, helper and Slack service class.

This is just my practice when dealing with 3rd party service – configuration, service class and a helper.

Don’t forget to composer dumpautoload -o once created the class.

Do take note the routeNotificationForSlack(), that is required in order Slack to work. Next will see how to notify to Slack channel.

Notification

This it the last section – now go and create a notification class via artisan – php artisan make:notification NotifyToSlackChannel.

Open up the NotifyToSlackChannel class and do two things:

One, update the via() method to accept only slack in the array.

Two, add a new method called toSlack($notification) and use \Illuminate\Notifications\Messages\SlackMessage to send messages to Slack channel.

Both steps above in following gist:

Let’s try!

So, you are ready to send messages to your Slack channel!

The fastest way to test your Slack notification is using tinker.

Here how I did:

Save it to tinker/notify-to-slack.php.

Then run php artisan tinker tinker/notify-to-slack.php.

That’s it! Working great!

User Cases

Some of user cases might need this type of notification:

  1. Helpdesk system – quick response from support team when get notify on new issues raised.
  2. E-commerce – placing new order, successful payment, out of stock items.
  3. Attendance system – you want know what time your employee come to your office (of course involve some other integration)

And I believe, it’s quite a lot of user cases can be implement, especially those involved with support, request, management.

Feel free to share your idea how we can implement more with Laravel Notification and Slack. 🙂

Happy coding guys!

Laravel: Billplz Helper

Jan
12

Billplz is a simple cash solution to send bills and collect payments in real-time.

There’s already a package available for PHP.

We going to create a wrapper and helper for Laravel application for the Billplz.

First, create a config file for Billplz – config/billplz.php

Then create a utility class in app/Utilities directory:

Lastly, create a helper in app/Support directory named helpers.php, and add the following command.

Now you’re done setting up your Billplz helper. You just need to setup BILLPLZ_API_KEY and BILLPLZ_VERSION in your .env to start use the Billplz helper.

Following are some example of the usage:

Billplz also provide sandbox for you to test. Do refer Billplz API documentation for more details.

Laravel: MoneyPHP Wrapper and Helper

Dec
19

I’ve created a simple MoneyPHP wrapper to simplify my day to day work dealing with MoneyPHP.

and a currency configuration – where you can specify the label for display use, and swift_code for MoneyPHP usage:

And I’ve create a helper in my app/Support/helpers.php, to simplify the usage of the MoneyPHP Wrapper.

A simple usage as following:

money()->toHuman(10000) – use it for display it to end user.

money()->toMachine(10000) – use it for create the right format before use in database or calculation.

money()->toCommon(10000) – use if for common display at the frontend.

WordPress: Message Class

Sep
20

A simple class for displaying admin messages – error, notice and successful.


class Message {
	public static function error($message) {
		return '<div class="error"><p>'.$message.'</p></div>';
	}

	public static function success($message) {
		return '<div class="updated"><p>'.$message.'</p></div>';
	}

	public static function notice($message) {
		return '<div class="update-nag">'.$message.'</div>';
	}
}

/** 
 * Usage
 **/
Message::error('Hutang 2.6Bil! Bayar!');
Message::notice('Kacing! (PM: eh..cam bunyi duit masuk je');
Message::success('PM: Hasil derma orang bagi. Syukur lahhhh..boleh byr hutang');
wp-admin-message-error

wp-admin-message-error

wp-admin-message-notice

wp-admin-message-notice

wp-admin-message-success

wp-admin-message-success