How to deploy packages using Slack

Black and White PDQ logo
PDQ|July 6, 2017

For those of you who don’t know, Slack is a team communication application and platform.

deploy applications using slack

Conversations are organized into channels where team members can chat, call, and share files. But one of the coolest features of Slack is their support for third party integrations, which is exactly what we’re going to use today to build a custom slash command to deploy packages directly from Slack!

Disclaimer: This project is not production ready, it’s just for funsies.

Creating a Slack app

1. Let’s get the Slack app created. You’ll need a Slack team, which you can sign up for here. Even if you have an existing team, you’ll probably want a new one for development.

2. After you’re logged in to Slack, head over to this link. Click the Create New App button to display the following screen:

Create an App in Slack

3. Give the app a name and assign it to your Slack team, then click Create App.

4. Next, it’s time to add features to the app. Since we’re doing a slash command, select Slash Commands.

Basic Information for Building Apps in Slack

5. Click Create New Command.

using Slash Commands in Slack

6. That will bring us to the command configuration display. Fill in the following fields and click Save.

create new command

Note: Keep this window open for a few more steps. The URL is intentionally fake. We’ll be back later to update it with a real one.

What these configuration details mean:

  • Command: this is the text that will be displayed when a user types “/” in the chat input field on Slack.

  • Request URL: when a user executes the deploy command, this is the URL Slack will notify with an HTTP Post request so that we can process it.

  • Short Description: tells the user what the command does.

  • Usage Hint: hints at the accepted parameters associated with the command. In our case, the name of the package and the name of the target computer.

Setting up the HTTP listener

Now that our Slack app has been created, we’re ready to start coding. The first thing we’ll do here is get a bare bones PowerShell script setup and configure it with ngrok.

In a directory of your choice, create a new PowerShell script file named “slack-deploy.ps1”. In it, add the following code:

= Net.HttpListener .Prefixes.Add() .Start() (.IsListening) { = .GetContext() = .Request = .Response = .StatusCode = 200 = = ::UTF8.GetBytes() .ContentType = .ContentLength64 = .Length = .OutputStream .Write(, 0, .Length) .Close() } .Stop()

Open a PowerShell terminal (make sure to run as administrator), change to your working directory and run your script: ./slack-deploy.ps1

If all goes well, you should see “listening on port: 8080…”. When you do, open a web browser and navigate to http://localhost:8080. You should see “It works!” displayed.

Configuring Ngrok

Ngrok is a service that allows you to create a secure tunnel between our local machine and the public URL they provide.

1. You’ll need to download ngrok from here

2. Unzip the executable to a directory of your choice (I put mine in C:\util\ngrok).

3. Navigate to Advanced System Settings (Win+Advanced system settings or Control Panel > System and Security > System > Advanced system settings) and click Environment Variables. We’re going to add the ngrok directory to our Path variable so we can run it just by typing “ngrok” in a terminal.

4. In Environment Variables, double-click Path and the ‘Edit environment variable’ dialog will display.

user variables

5. Click New and add the directory where you unzipped ngrok.

6. Open up another terminal and run ngrok http 8080. It should start displaying Session Status, Web Interface, and the public URL that it’s forwarding to your local machine.

7. When the Session Status switches to online, open up the URL it lists. Confirm that you get the same, “It works!” as you did locally.

8. Now that we have a public URL, we can go back to our Slack command configuration and update its URL field. Under Slash Commands, click Edit next to your custom command, and update your command’s Request URL with your new ngrok URL:

edit command

Note: anytime you restart the ngrok process, your unique URL will change requiring you to update this setting.

9. Click Save and after the dialog closes, click Install App in left navigation menu under Settings.

settings - install app

10. Click Install App again and then select Authorize to authorize the application.

11. After that’s complete, open up Slack and confirm it’s successfully installed. In the chat input, when you type a forward slash ‘/’, you should see ‘Deploy’ show up as one of your options.

Woot! Almost there. Now we just need to finish up our PowerShell script, then we’ll be deploying packages Slack-style!

Processing the Deploy Command

Here’s the updated PowerShell script in full:

= Net.HttpListener .Prefixes.Add() .Start() (.IsListening) { = .GetContext() = .Request = .Response = = 500 = (.HttpMethod ) { = 404 = } { = .ContentLength64 + 1 = byte[] = { = .InputStream.Read(, 0, ) += .ContentEncoding.GetString(, 0, ) } ( ) .InputStream.Close() = ::match(, , ) (.Success ) { = 400 = } { , = .Groups[0].Value.Split() = { = () = 200 } { = = 400 } { = } } } .StatusCode = = ::UTF8.GetBytes() .ContentType = .ContentLength64 = .Length = .OutputStream .Write(, 0, .Length) .Close() } .Stop()

What’s being done here:

  • We’ve replaced the test variables with initial values.

  • In the first “if” statement, any requests that are not “Post” methods, we reject. This is the only method our script is concerned about.

  • We grab the content from the Post body and use a regular expression to parse out the package and target values.

  • If the package and target were successfully parsed, we run the PDQ Deploy command. If that succeeds, we set the response content to the console’s output.

  • Finally, we set the response status code and content and return it.

We’re done! Now let’s try it out. Restart the PowerShell script ./slack-deploy.ps1 and open up Slack.For my test, I have a package named ‘TestPackage’ that I’ll run against my local machine.

In Slack:

deploy test package

After hitting enter, you should see something similar to:

Deployment started

To confirm in PDQ Deploy, click on All Deployments in the tree:

1 Deployment

Now, cut the slack and get deploying!

Black and White PDQ logo

PDQ is the best way to have healthy, up-to-date machines automatically. Streamline your patch management and software deployment processes — whether you manage 15 machines or 15,000.

Related articles