Powered By GitBook
Run a "hello, world" Slack slash command on App Engine using Java.
Tim Swast | Developer Programs Engineer | Google
Contributed by Google employees.
This tutorial demonstrates how to build and deploy a slash command for Slack on Google Cloud.
Slash commands provide a way to call external web services from a Slack conversation. For example, the Giphy app can be run by /giphy in a conversation.


Before you begin

    Follow the Quickstart for Java in the App Engine flexible environment to set up your environment to deploy the sample applications App Engine. 1. Download and install the Cloud SDK. 1. Install and configure Apache Maven. 1. Create a new Google Cloud project, or use an existing one. 1. Enable billing for your project. 1. Initialize the Cloud SDK:
    gcloud init
    Create a new Slack team, or use a team where you have permissions to add custom integrations.


This tutorial uses billable components of Google Cloud, including App Engine flexible environment.
Use the Pricing Calculator to generate a cost estimate based on your projected usage.
Slack is free for up to 10 apps and integrations. Check the Slack pricing page for details.

Getting the sample code

Get the latest sample code from GitHub using Git or download the repository as a ZIP file. (Download)
git clone https://github.com/GoogleCloudPlatform/slack-samples.git
The java/command/1-start directory contains a simple application, which you will modify to support Slack slash commands.
cd java/command/1-start

Create a Slack app

To create a new Slack app, go to the app management page and click Create new app.
    Give the app a name, such as "Hello World".
    Choose the Slack team for development and where you will eventually install it.

Create a slash command

    On the left navigation panel, click Slash commands > Create new command.
    Enter a command name, such as /greet.
    Enter https://YOUR_PROJECT.appspot.com/hello as your request URL, replacing YOUR_PROJECT
    with your Google Cloud project ID.
    Enter a short description, such as "Sends a greeting."

Install the command to your workspace

    On the left navigation panel, click Basic information.
    Expand Install your app to your workspace, then click Install app to workspace.

Deploying your app

Your app requires some configuration. This sample uses the RuntimeConfig API to store configuration values, such as secret tokens.

Create a configuration

Using the command-line Cloud SDK, create a new runtime configuration:
gcloud beta runtime-config configs create slack-samples-java

Copy the verification token

To ensure that HTTP requests to your app originate from Slack, Slack provides a validation token. You check that the token field of an incoming request matches the expected value.
    Select your app on the app management page.
    Go to the Basic information page.
    Scroll to App credentials and copy the Verification token text.

Add verification token to the configuration

Create a variable called slack-token in the runtime configuration. Use the Cloud SDK from the command-line to add the variable:
gcloud beta runtime-config configs variables set \
slack-token "YOUR-TOKEN-VALUE" \
--is-text --config-name slack-samples-java
Replace YOUR-TOKEN-VALUE with the verification token value you copied from the Slack app management page.

[Optional] Running locally

To run the application locally, use the Maven Spring Boot plugin:
mvn clean spring-boot:run
View the app at http://localhost:8080.
Since Slack requires a public URL to send webhooks, you may wish to use a service like ngrok to test your Slack application locally.

Deploying to App Engine

To deploy the app to App Engine, run the following command:
mvn clean appengine:deploy
After the deploy finishes (can take up to 10 minutes), you can view your application at https://YOUR_PROJECT.appspot.com, where YOUR_PROJECT is your Google Cloud project ID. You can see the new version deployed on the App Engine section of the Cloud Console.
For a more detailed walkthrough, see the Quickstart for Java in the App Engine flexible environment.

Trying the slash command

When you run the slash command, Slack will send a request to your app and show the result.
    Type /greet in your Slack team.
You should see the text Hello, world. in response.

Cleaning up

To prevent unnecessary charges, clean up the resources created for this tutorial.
    Remove the app from your Slack team.

Next steps

Last modified 7mo ago