Slack App with Laravel

John Kazakopoulos
Monospace Pub
Published in
3 min readJul 10, 2017

--

You always wanted to know how to create a Slack app?
Well, who doesn’t want to?

There is a lot of programming languages out there that you can create the app, but I think Laravel is an easy way to make it work like a charm.

Ok so let’s cut to the chase.

First of all this (Slack API) is your buddy.
So stay close to it. (You will need it)

We need to create an account and a team to Slack. (it’s obvious.)

Then you need to go to api.slack.com/app .

Here you create your first app.
So give an app name and select the team that you created before.

You have three options when you go to your app.

  1. Add features and functionality
  2. Install your app to your team
  3. Manage Distribution

We will need only the first two.

So you go to “Install your app to your team.”

You install (easy) the app, and you go to “Add features and functionality.”
Click on “
Slash Commands” and then “Create New Command.”

This command ex. /order will be needed so when you use it to your Slack it will do an API call to your server.

You will need to fill these:

Command: Here you fill the name of the command.

Request URL: The URL to your server that slack is going to hit (remember it’s going to hit a post method)
we will create the URL later so now you can write it as example.com/get-order

Short Description: it’s up to you. (write whatever you want)

Usage Hint: it’s up to you again.

Ok now let’s do some coding.

We need an already Laravel project installed on our server.
(I’m not going to bother you. it’s pretty easy)

So we create a route(URL) that has(use) post method.
This URL is the one that you wrote before to “Request URL” field.

When Slack hits your API call, it will send you some info about the user and the channel.

As long as we have created the url we need to go to our controller that our method is created and paste the code below.

Also, every command that you create has a token so you can be safe that no one else will hit your call.

Ok so basically we are going to get the user input and return it back to the user.

public function order(Request $request)
{
#we are not going to need all of this but a part of the info that slack sends.
$command = $request->input(‘command’);
$text = $request->input(‘text’);
$token = $request->input(‘token’);
$user = $request->input(‘user_name’);
$channel_id = $request->input(‘channel_id’);
$channel_name = $request->input(‘channel_name’);
# Check the token and make sure the request is from our team

if($token != ‘hP5ZtkrBZ0UnVrZN4uQgkJMr’){

# replace this with the token from your slash command
$msg = “The token for the slash command doesn’t match.”;die($msg);
echo $msg;
}

$respond[‘text’] = $text;
return $respond;

}

So this is it. You did it. Now when you hit your command with a text next to it, you will get the text back to Slack!

--

--