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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. 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. 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.
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
- PHP development environment with Laravel
- Composer globally installed
- Slack Account
- 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.
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.
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
Laravel Slack Notifications
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.
Laravel Slack Bot
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:
- User mobile number.
- Twilio Account for sending SMS
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.
Laravel Slack Logging
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
Laravel Slack Notification
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.
Laravel Slack Notification Channel
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.
Laravel Slack Webhook
Thanks for reading!