Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Running a Slack Slash Command on App Engine Using Java description: Run a "hello world" Slack slash command on App Engine using Java. author: tswast tags: App Engine, Slack date_published: 2017-05-26


This tutorial demonstrates how to build and deploy a slash command for Slack on Google Cloud Platform.

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.

Objectives

Before you begin

  1. 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 Google Cloud SDK.
    2. Install and configure Apache Maven.
    3. Create a new Google Cloud Platform project, or use an existing one.
    4. Enable billing for your project.
    5. Initialize the Cloud SDK:

      gcloud init

  2. Create a new Slack team, or use a team where you have permissions to add custom integrations.

Costs

This tutorial uses billable components of Cloud Platform, including:

  • Google 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 Hello World 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.

  1. Give the app a name, such as "Hello World".
  2. Choose the Slack team for development and where you will eventually install it.

Create a slash command

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

Install the command to your team

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

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 Google 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.

  1. Select your app on the app management page.
  2. Go to the Basic information page.
  3. 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 Google 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.

  1. Disable the App Engine application.
  2. Remove the app from your Slack team.

Next steps

See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.