Laravel Slack



Join 32,860 Laravel/PHP Pros on Slack today! Join our Slack Community. Weekly Newsletter. Domain oriented Laravel. Most principles in this course are based on best practices in domain driven design, and hexagonal architectures. These proven principles will be our guide throughout building large Laravel applications. Some of the theoretical topics we'll.

Laravel slack bot

Laravel has lots of built-in and advanced features to make any developer impress with. Sending Notifications are one of them. You can send email, SMS or Slack notification with its built-in functionality. In this article, we going to send the slack notification with Laravel.

I am going to use Laravel 5.8.x. The setup is pretty easy. If you haven’t setup Laravel yet then please following the Laravel Installation guide from the documentation.

Laravel Slack Integration

Requirements

Laravel Slack
  • Slack Notification Channel Package
  • Configure Incoming Webhooks

Slack Notification Channel

In the older version of Laravel, Slack notification was built-in configured with the framework. But on this 5.8.x version, they have created a separate package for Slack Notification. First of all, we have to install that package in our application.

Configure Incoming Webhooks

After installation of Slack Notification Channel package, we have to collect the Slack Webhook URL. So first of all, login to your slack application. Now on the main screen, navigate the App menu on the left sidebar at the very bottom.

Now click on the + icon to add the incoming-hook app to your slack application. Once you clicked on the + icon, then browse apps screen will pop up on your screen. Just search the incoming-webhook in the search area.

If you haven’t installed the incoming-webhook app on the slack app then please install it. After installation, the incoming-webhook app, navigate the settings tab on the incoming-webhook section and collect the Webhook URL.

Now copy the Webhook URL and paste it to the .env file at the very bottom or whatever you like.

This is what you need from the Slack app. Now it’s time to create a notification on Laravel. So let’s create our first notification.

The above command will create a Notifications folder on the app folder from where you can find the TestNotification file. So let’s open and start adding it.

Notice the SlackMessage package that we have included on our application.

use IlluminateNotificationsMessagesSlackMessage;

Add the slack value in the via method’s returned array and create your first slack notification in the new method named toSlack().

If you like to more customized notification then please follow the documentation link. Now its time to trigger this notification.

Laravel

Complete code for Notification

Paste the above code to send the notification to your slack channel and that’s all.

Related posts:

  1. Laravel On-Demand Notifications
  2. Custom Validation With Laravel
  3. REST API and Passport Authentication with Laravel
  4. Understanding Laravel Middleware
  5. Laravel Notification – Customize markdown email header and footer
  6. Laravel Mail – Sending Mailable, Markdown Mailable, and Raw HTML

Laravel is one of the most famous PHP MVC frameworks, largely due to its commitment to modern architecture and an active community. Notably, it provides all the features that are required to create your project, whether for personal or enterprise-level usage.

Slack is an online instant messaging and collaboration system that enables users to centralize notifications. From sales to tech support, social media and more, Slack becomes one searchable place where your team can discuss ideas and take action.

In this tutorial, we’ll be combining Slack, Twilio and Laravel to learn how to send an SMS from Slack. This would be a great feature for situations such as notifying users on the phone regarding an urgent meetup.

After we’re finished, you will have a running Laravel application that allows you to send SMS to tagged users in a Slack channel.

Requirements

  1. PHP development environment with Laravel
  2. Composer globally installed
  3. Slack Account
  4. Passion :D

Set Up a New Laravel Project

If you don’t have Laravel installed in your system, a quick download from the Laravel Official Documentation will get you started. A guide to getting started from Laravel can be found in the Laravel docs. Let’s start with setting up a new Laravel project named send-sms-from-laravel. Remember, you can give your project any name you want.

This will install Laravel v6.0 along with all the dependencies required by it. Composer post-create scripts automatically create the .env file for us and set the APP_KEY environment variable. Add the APP_KEY in the .env file by running this command, if it is not configured automatically.

Create a Slack App to Send SMS

Log in to your Slack account and navigate to the create Slack App page.

Enter the name for the app, select the workspace and click on Create App button.

Add the Slash Commands Feature

After clicking on the Slack app, we will be redirected to the settings page for the app. Let’s add additional functionalities to our newly created app. We want to send SMS to the user whenever someone tags them with the keyword in which our app listens/responds.

Keep in mind, Slash Commands are initiated from the message box in Slack, but they aren't messages. Upon submit, a Slash Command will transfer a payload of data from Slack to an app, allowing the app to automatically respond in whatever way it has been programmed. Read more here.

On the settings page, click on Slash Commands.

After clicking on the Slash Commands card, we will be redirected to the page to define the Commands which enable users to interact with your app from within Slack.

Laravel Slack

I have chosen the command /ping for my app. Upon execution, it will send an HTTP request to our Laravel project.

NOTE: Please note that the Request URL is required to save your command. Because our Laravel application is only accessible locally, we will need DNS software like ngrok to assign a publicly accessible URL over the Internet. Laravel is pre-loaded with ngrok and can be activated using the artisan serve command. Read more about ngrok here.

Laravel Slack Channel

Create a local PHP server to run our Laravel application using the following command.

We can expose the webserver running on our local machine to the internet with ngrok using following commands. This will provide the environment we need to define our Request URL.

Now, we can enter NGROK URL + /webhook as the requested URL. Be sure to add the short description and check the “Escape channels, users, and links sent to your app” setting so that we can capture user and channel id in the data from Slack. Once those values are updated, click on the Save button to save the configuration.

We have saved the app which will trigger our Laravel application every time we send a message with the /ping command. Click on the “Basic Information” link from the Sidebar settings to display options similar to the below image.

Install the App to Slack Workspace

Now that we have created our Slack App, let’s install it into our Slack workspace. Click the “Install App to Workspace” button and allow permission to the app. Once we install our Slack App, we will see something similar as shown in the image below.

We have created a new Slack app and successfully installed it in our Slack workspace. At this point, our application has no route with the name /webhook so it won’t work. Let’s still test whether or not we are receiving the request from the slack.

Ngrok comes with its own dashboard where we can inspect any request to our application. The ngrok dashboard is running at http://127.0.0.1:4040. Open the dashboard of your Slack workspace and type /ping helloworld in any channel.

Sending the message “helloworld” with /ping will throw this error in slack

Let’s inspect the request from the ngrok dashboard.

A POST to /api/webhook shows that our Slack app is triggering our Laravel application as expected. We’ll now add the functionality to send an SMS when the /ping command is triggered from Slack.

Create the Webhook in our Laravel Application

In Laravel, routes are defined in our route files, which are located in the routes directory. Open api.php file and add a new route /webhook as we defined in our Slack app.

NOTE: Routes defined in api.php are automatically prepended with the /api prefix and do not need to be added to the route.

Add the above code at the end of the api.php file. Because Slack app makes a POST request, we have created a route /api/webhook which accepts POST requests and passes the data to the associated IncomingController.

Create the Controller in our Laravel Application

In Laravel, controllers are defined in the app/Http/Controllers directory and work with routes to process the incoming request and return the response. Let’s create a listener class using the Artisan CLI:

A new file named IncomingController.php will be created in the app/Http/Controllers directory. We will create methods under the IncomingController class to add our required functionality. Let’s understand our requirement before writing the code directly.

First, we want to send an SMS to the user tagged in the slash command. To send an SMS, we need two things:

  1. User mobile number.
  2. Twilio Account for sending SMS
Slack

Setting up the Twilio SDK

To communicate with Twilio’s API, we need to install the Twilio SDK for PHP in our project. In your terminal, navigate to the project directory and run the following command:

Because each request to the Twilio API is authenticated, we will need Twilio Credentials in order to make use of Twilio SDK. Open the Twilio Dashboard (create an account if you don’t have one) and Grab Account SID and Auth token as shown in the image below.

As mentioned before, your application will need a phone number in order to send messages from. If you already purchased a Twilio Number, open the Phone Number section to get your SMS enabled phone number. If not, purchase a new phone number.

Once, we have the phone number, populate the .env file with the credentials as follows:

Finding the User’s Mobile Number

Slack has profiles for every user which includes basic information such as Name, Display name, and Mobile number as shown in the image below.

In order to expose our mobile number in the request, we have to enter it here with the country code. Whenever we tag a user with our /ping command, it sends the tagged user with their username along with the user_id. We can use the Slack API to fetch the user details.

Slack has some Methodsto get additional information using the Slack API. The users.infomethod specifically gets information about a user as shown in the image below.

For accessing user details using the above method, we need access to the workspacetoken.

Create Slack Workspace Token

Open the Legacy Tokens page, scroll down and create a token for the workspace in which we are installing the app. We can also re-issue the token any number of times.

Generate the token and save it in the .env file as follows:

Implement the Logic in our Controller

Open IncomingController.php in the app/Http/Controllers directory that we created in the previous steps.

We will create a function with the name index() which will get the response from the Slack POST request. Inside this function, we will first capture the user_id from the POST request and validate whether we have tagged any user or not. If we haven’t tagged any user, we will return the response back to the slack. Read more about Responding to Commands.

NOTE:The full code for the controller is at the end of this section.

If we have tagged the user, we will assign the user_id to the $user variable.

Next we will create another function named computeSlackUserInfo() to fetch the user mobile number using Slack API.

Notice that we are making a GET request to the Slack API https://slack.com/api/users.info with the SLACK_WORKSPACE_TOKEN and user_id parameters to fetch the user information and return the valid response back to the index function as shown in the image above. If the Slack user profile is incorrect or we don’t get a valid response, we will return the response back to the Slack channel with the message “Issue with Slack user profile”.

In the case we get a valid response of the user with his complete details, we can send the SMS to the user on their mobile number. We’ll create another function named sendSMS() to send the SMS to the user.

We are using the Twilio SDK to send SMS using Twilio. If the message is successfully sent, we will return the response back to Slack with a success message, else we will respond with the error message.

Our controller will require Guzzle in order to make HTTP requests to the Slack API. Install this dependency in your application by running the following command in your terminal:

Let’s see our final IncomingController.php file with all the logic.

Awesome! Let’s test our application and see whether it’s sending our messages or not.

Testing

Our application is already running and ngrok is also configured previously. Let’s open the ngrok dashboard and Slack workspace again and type /ping @Ankit helloworld in any channel.

As we can see that message has been sent successfully. Let’s check our mobile device for the message.

Let’s check the logs on the Twilio Dashboard.

Conclusion

In this article, we learned how to send SMS using Twilio SDK, as well as how we can use it to ping users from Slack.

You could also use Laravel Queues to queue your messages and use the Slack Delayed Response feature to scale your application. To learn more about Laravel Queues, check out this article.

The complete code for the tutorial can be found on GitHub repo → send-sms-from-slack.

Feel free to comment and ask me anything. You can reach me at ankitjain28may77@gmail.com and follow me on Twitter @ankitjain28may. You can also read more of my articles on Medium @ankitjain28may.

Thanks for reading!





Comments are closed.