This tutorial demonstrates using Cloud Run functions to implement a Slack Slash Command that searches the Google Knowledge Graph API.
Objectives
- Create a Slash Command in Slack.
- Write and deploy an HTTP Cloud Run function.
- Search the Google Knowledge Graph API using the Slash Command.
Costs
In this document, you use the following billable components of Google Cloud:
- Cloud Run functions
- Cloud Build
- Artifact Registry
- Cloud Logging
For details, see Cloud Run functions pricing.
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, and Logging APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, and Logging APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Prepare your development environment.
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
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 Cloud Run function's trigger endpoint.
- The Cloud Run 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 Cloud Run function formats the response into a Slack message.
- The Cloud Run function sends the message back to Slack.
- The user sees the formatted response in the Slack channel.
It may help to visualize the steps:
Getting credentials
To deploy your function, you need a Google Cloud console-supplied API key and a Slack signing secret.
Getting the Knowledge Graph API key
On the Google Cloud console Credentials page,
click the Create credentials button and select API key. Remember this
key, as you will include it as part of your deploy
command. This key is what
enables your function to access the Knowledge Graph API.
Getting the Slack signing secret
You also need the Slack signing secret to deploy your function. To get the Slack signing secret, create the Slack App that will host your Slack Slash Command. This app needs to be associated with a Slack team where you have permissions to install integrations.
Go to the Slack Your Apps page and click Create New App.
Choose From Scratch.
Supply a name for your app and select a Slack workspace where you have permissions to install integrations.
Click Create App.
The app is created, and the display changes to the Basic Information page.
On the Basic Information page, copy your Slack signing secret and save it.
Save your changes.
Next you need to get the source code and deploy your function. After you deploy your function, you will configure your Slack app to integrate it with the deployed function, as described in Configuring the application.
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.
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-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.
PHP
git clone https://github.com/GoogleCloudPlatform/php-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/functionsv2/slack/
Java
cd java-docs-samples/functions/slack/
C#
cd dotnet-docs-samples/functions/slack/SlackKnowledgeGraphSearch/
Ruby
cd ruby-docs-samples/functions/slack/
PHP
cd php-docs-samples/functions/slack_slash_command/
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 nodejs-slack-function \ --gen2 \ --runtime=nodejs22 \ --region=REGION
\ --source=. \ --entry-point=kgSearch \
--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 python-slack-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=kg_search \
--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 go-slack-function \ --gen2 \ --runtime=go122 \ --region=REGION
\ --source=. \ --entry-point=KGSearch \
--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 \ --gen2 \ --runtime=java21 \ --region=REGION
\ --source=. \ --entry-point=functions.SlackSlashCommand \ --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.
C#
gcloud functions deploy csharp-slack-function \ --gen2 \ --runtime=dotnet8 \ --region=REGION
\ --source=. \ --entry-point=SlackKnowledgeGraphSearch.Function \
--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 .NET version to run
your function.
Ruby
gcloud functions deploy ruby-slack-function \ --gen2 \ --runtime=ruby33 \ --region=REGION
\ --source=. \ --entry-point=kg_search \
--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.
PHP
gcloud functions deploy php-slack-function \ --gen2 \ --runtime=php83 \ --region=REGION
\ --source=. \ --entry-point=receiveRequest \
--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 PHP 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:
Select the app, go to Slash commands, and click the Create New Command button.
Enter
/kg
as the name of the command.In the Request URL field, enter the URL of your function.
You can either copy the URL from the
deploy
command output, or go to the Cloud Run functions Overview page in Google Cloud console, click the function to open its Function Details page, and copy the URL from there.Enter a short description and click Save.
Go to Basic Information.
Click Install to 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
C#
Ruby
PHP
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
C#
Ruby
PHP
The following function authenticates the incoming request by verifying the
X-Slack-Signature
header provided by Slack:
Node.js
Python
Go
Java
C#
Ruby
PHP
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
C#
Ruby
PHP
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
C#
Ruby
PHP
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
field)
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
Once the function deployment has completed, type this command into your Slack channel:
/kg giraffe
You should see the Knowledge Graph entry for "giraffe."
Check the logs to see the output from the function execution:
gcloud functions logs read --limit 100
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Deleting the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Deleting the function
To delete the Cloud Run function you deployed in this tutorial, run the following command:
Node.js
gcloud functions delete nodejs-slack-function --gen2 --region REGION
Python
gcloud functions delete python-slack-function --gen2 --region REGION
Go
gcloud functions delete go-slack-function --gen2 --region REGION
Java
gcloud functions delete java-slack-function --gen2 --region REGION
C#
gcloud functions delete csharp-slack-function --gen2 --region REGION
Ruby
gcloud functions delete ruby-slack-function --gen2 --region REGION
PHP
gcloud functions delete php-slack-function --gen2 --region REGION
You can also delete Cloud Run functions from the Google Cloud console.