This tutorial demonstrates using Cloud Run functions to implement a Slack Slash Command that searches the Google Knowledge Graph API.
Visualizing the flow of data
The flow of data in the Slack Slash Command tutorial application involves several steps:
- The user executes the
/kg <search_query>
Slash Command in a Slack channel. - Slack sends the command payload to the function's trigger endpoint.
- The function sends a request with the user's search query to the Knowledge Graph API.
- The Knowledge Graph API responds with any matching results.
- The function formats the response into a Slack message.
- The function sends the message back to Slack.
- The user sees the formatted response in the Slack channel.
It may help to visualize the steps:
Creating the Knowledge Graph API key
In the Google Cloud console Credentials page, click the Create credentials button and select API key. Remember this key, as you will use it to access the Knowledge Graph API in the next section.
Preparing the function
Clone the sample app repository to your local machine:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Run functions sample code:
Node.js
cd nodejs-docs-samples/functions/slack/
Python
cd python-docs-samples/functions/slack/
Go
cd golang-samples/functions/slack/
Java
cd java-docs-samples/functions/slack/
Ruby
cd ruby-docs-samples/functions/slack/
Deploying the function
To deploy the function that is executed when you (or Slack) make an HTTP
POST request to the function's endpoint, run the following command in the
directory that contains the sample code (or the pom.xml
file for Java):
Replace YOUR_SLACK_SIGNING_SECRET
with the signing
secret provided by Slack in the Basic information page of your app
configuration, and YOUR_KG_API_KEY
with the Knowledge
Graph API Key you created previously.
Node.js
gcloud functions deploy kgSearch \ --runtime nodejs22 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy kg_search \ --runtime python312 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy KGSearch \ --runtime go121 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-slack-function \ --entry-point functions.SlackSlashCommand \ --runtime java17 \ --memory 512MB \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
Ruby
gcloud functions deploy kg_search --runtime ruby33 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
Configuring the application
After the function is deployed, you need to create a Slack Slash Command that sends the query to your function every time the command is triggered:
Create a Slack App to host your Slack Slash Command. Associate it with a Slack team where you have permissions to install integrations.
Go to Slash commands and click the Create new command button.
Enter
/kg
as the name of the command.Enter the URL for the command:
Node.js
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/kgSearch
Python
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/kg_search
Go
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/KGSearch
Java
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/java-slack-function
Ruby
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/kg_search
where
YOUR_REGION
is the region where your function is deployed andYOUR_PROJECT_ID
is your Cloud project ID.Both values are visible in your terminal when your function finishes deploying.
Click Save.
Go to Basic Information.
Click Install your app to your workspace and follow the instructions on screen to enable the application for your workspace.
Your Slack Slash Command should come online shortly.
Understanding the code
Importing dependencies
The application must import several dependencies in order to communicate with Google Cloud Platform services:
Node.js
Python
Go
Java
Ruby
Receiving the webhook
The following function is executed when you (or Slack) make an HTTP POST request to the function's endpoint:
Node.js
Python
Go
Java
Ruby
The following function authenticates the incoming request by verifying the
X-Slack-Signature
header provided by Slack:
Node.js
Python
Go
Java
Ruby
Querying the Knowledge Graph API
The following function sends a request with the user's search query to the Knowledge Graph API:
Node.js
Python
Go
Java
Ruby
Formatting the Slack message
Finally, the following function formats the Knowledge Graph result into a richly formatted Slack message that will be displayed to the user:
Node.js
Python
Go
Java
Ruby
Slack API timeouts
The Slack API expects your function to respond within 3 seconds of receiving a webhook request.
The commands in this tutorial typically take less than 3 seconds to respond. For
longer-running commands, we recommend configuring a function to push requests
(including their response_url
)
to a Pub/Sub topic that acts as a task queue.
Then, you can create a second function triggered by Pub/Sub
that processes those tasks and sends results back to Slack's response_url
.
Using the Slash command
Type the command into your Slack channel:
/kg giraffe
Watch the logs to be sure the executions have completed:
gcloud functions logs read --limit 100