Before you begin

Note: If you don't plan to keep the
resources you create in this tutorial, create a new project instead of
selecting an existing project. After you finish, you can delete the
project, removing all resources associated with the project and
tutorial.

Python (Beta)

Replace YOUR_SLACK_TOKEN with the verification token provided by Slack in the Basic information page of your app configuration.

Replace YOUR_KG_API_KEY with the Knowledge Graph API Key you just created.

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 Cloud Functions sample code:

Node.js 6

gcloud functions deploy kgSearch --runtime nodejs6 --trigger-http

Node.js 8 (Beta)

gcloud functions deploy kgSearch --runtime nodejs8 --trigger-http

Python (Beta)

gcloud functions deploy kg_search --runtime python37 --trigger-http

Configuring the application

After the function is deployed, you need to create a Slack Slash Command
that sends the query to your Cloud 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 (Beta)

https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/kg_search

where YOUR_REGION is the region where your
Cloud Function is deployed and YOUR_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:

Slack API timeouts

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.

YOUR_SLACK_TOKEN is the token provided by Slack in your Slash Command
configuration.

Watch the logs to be sure the executions have completed:

gcloud functions logs read --limit 100

Type the command into your Slack channel:

/kg giraffe

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for
the resources used in this tutorial:

Deleting the project

The easiest way to eliminate billing is to delete the project you
created for the tutorial.

To delete the project:

Caution: Deleting a project has the following effects:

Everything in the project is deleted. If you used an existing project for
this tutorial, when you delete it, you also delete any other work you've done in the project.

Custom project IDs are lost.
When you created this project, you might have created a custom project ID that you want to use in
the future. To preserve the URLs that use the project ID, such as an appspot.com
URL, delete selected resources inside the project instead of deleting the whole project.

If you plan to explore multiple tutorials and quickstarts, reusing projects can help you avoid
exceeding project quota limits.