Running a Slack Slash command on App Engine using Java
Contributed by Google employees.
- Deploy a Java application to the App Engine flexible environment.
- Create a slash command for Slack.
- Load tokens from the Google Cloud Runtime Config API
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.
- Download and install the Cloud SDK.
- Install and configure Apache Maven.
- Create a new Google Cloud project, or use an existing one.
- Enable billing for your project.
Initialize the Cloud SDK:
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
java/command/1-start directory contains a simple application, which you will
modify to support Slack slash commands.
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
https://YOUR_PROJECT.appspot.com/helloas your request URL, replacing
YOUR_PROJECTwith 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
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.
/greetin your Slack team.
You should see the text
Hello, world. in response.
To prevent unnecessary charges, clean up the resources created for this tutorial.
- Disable the App Engine application.
- Remove the app from your Slack team.