このチュートリアルでは Cloud Run functions を使用して、Google Knowledge Graph API を検索する Slack スラッシュ コマンドの実装方法について説明します。
目標
- Slack でスラッシュ コマンドを作成する。
- Cloud Run HTTP 関数を作成してデプロイする。
- Slash コマンドを使って Google Knowledge Graph API を検索する。
費用
このドキュメントでは、Google Cloud の次の課金対象のコンポーネントを使用します。
- Cloud Run functions
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
始める前に
- 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, and Google Knowledge Graph Search 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, and Google Knowledge Graph Search APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
開発環境を準備します。
Node.js
Python
Go
Java
C#
Ruby
PHP
gcloud CLI がすでにインストールされている場合は、次のコマンドを実行して更新します。
gcloud components update
データの流れ
Slack のスラッシュ コマンドのチュートリアル アプリケーションでのデータの流れは次のとおりです。
- ユーザーが Slack チャネルで
/kg <search_query>
スラッシュ コマンドを実行します。 - Slack がコマンドのペイロードを関数のトリガー エンドポイントに送信します。
- 関数がユーザーの検索クエリとともにリクエストを Knowledge Graph API に送信します。
- Knowledge Graph API は、レスポンスで一致した結果を返します。
- 関数はフォーマット化したレスポンス内容を Slack メッセージに保存します。
- 関数はメッセージを Slack に送信します。
- ユーザーは Slack チャンネルでフォーマット化されたレスポンスを確認します。
次はこの手順を可視化した図です。
Knowledge Graph API Key の作成
Google Cloud Console の [認証情報] ページで、[認証情報を作成] ボタンをクリックして [API キー] を選択します。このキーは、次のセクションの Knowledge Graph API にアクセスするときに使用します。
関数の準備
ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
PHP
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Cloud Run functions のサンプルコードが含まれているディレクトリに移動します。
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/
C#
cd dotnet-docs-samples/functions/slack/SlackKnowledgeGraphSearch/
Ruby
cd ruby-docs-samples/functions/slack/
PHP
cd php-docs-samples/functions/slack_slash_command/
関数のデプロイ
ユーザー(または Slack)が関数のエンドポイントに HTTP POST リクエストを送信したときに実行される関数をデプロイするには、サンプルコード(または Java の pom.xml
ファイル)を含むディレクトリで次のコマンドを実行します。
YOUR_SLACK_SIGNING_SECRET
は、アプリの構成の [Basic information] ページで、Slack から提供された署名シークレットに置き換えます。YOUR_KG_API_KEY
は、前に作成した Knowledge Graph API Key で置き換えます。
Node.js
gcloud functions deploy kgSearch \ --runtime nodejs20 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
サポートされている Node.js バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
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
サポートされている Python バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
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
サポートされている Go バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
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
サポートされている Java バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
C#
gcloud functions deploy csharp-slack-function \ --entry-point SlackKnowledgeGraphSearch.Function \ --runtime dotnet6 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
サポートされている .NET バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Ruby
gcloud functions deploy kg_search --runtime ruby32 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
サポートされている Ruby バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
PHP
gcloud functions deploy searchKnowledgeGraph --runtime php82 \
--trigger-http \
--set-env-vars "SLACK_SECRET=YOUR_SLACK_SIGNING_SECRET
,KG_API_KEY=YOUR_KG_API_KEY
" \
--allow-unauthenticated
サポートされている PHP バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
アプリケーションの構成
関数をデプロイした後、Slack スラッシュ コマンドを作成する必要があります。このコマンドがトリガーされるたびに関数にクエリが送信されます。
Slack のスラッシュ コマンドをホストする Slack アプリを作成します。統合のインストール権限が与えられている Slack チームにそれを関連付けます。
[Slash commands] に移動し、[Create new command] ボタンをクリックします。
コマンドの名前として「
/kg
」を入力します。コマンドの URL を入力します。
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
C#
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/csharp-slack-function
Ruby
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/kg_search
PHP
https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/searchKnowledgeGraph
YOUR_REGION
は、関数がデプロイされているリージョンであり、YOUR_PROJECT_ID
は Cloud プロジェクト ID です。関数のデプロイが完了すると、両方の値がターミナルに表示されます。
[保存] をクリックします。
[Basic Information] に移動します。
[Install your app to your workspace] をクリックし、画面の指示に従って、ワークスペース用にアプリケーションを有効にします。
少し待つと、Slack Slash コマンドがオンラインになります。
コードについて
依存関係をインポートする
アプリケーションが Google Cloud Platform サービスとやり取りするには、いくつかの依存関係をインポートする必要があります。
Node.js
Python
Go
Java
C#
Ruby
PHP
Webhook の受け取り
ユーザー(または Slack)が関数のエンドポイントに HTTP POST リクエストを送信すると、次の関数が実行されます。
Node.js
Python
Go
Java
C#
Ruby
PHP
次の関数は、Slack から提供された X-Slack-Signature
ヘッダーを検証することで、受信リクエストを認証します。
Node.js
Python
Go
Java
C#
Ruby
PHP
Knowledge Graph API のクエリ作成
次の関数はユーザーの検索クエリとともにリクエストを Knowledge Graph API に送信します。
Node.js
Python
Go
Java
C#
Ruby
PHP
Slack メッセージのフォーマット化
最後に、次の関数は Knowledge Graph の結果をフォーマット化し、ユーザーに表示される適切な形式の Slack メッセージにします。
Node.js
Python
Go
Java
C#
Ruby
PHP
Slack API のタイムアウト
Slack API では、関数が Webhook リクエストを受け取ってから 3 秒以内に応答することが想定されています。
通常、このチュートリアルのコマンドは 3 秒以内に応答します。実行時間が長いコマンドの場合、タスクキューとして機能する Pub/Sub トピックに(リクエストの response_url
を含む)リクエストを push する関数を構成することをおすすめします。
この構成によって、2 番目の関数を作成して Pub/Sub によってトリガーすることで、それらのタスクを処理し、Slack の response_url
に結果を送信できます。
Slash コマンドの使用
Slack チャンネルにコマンドを入力します。
/kg giraffe
実行した内容が完了していることをログで確認します。
gcloud functions logs read --limit 100
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。
プロジェクトの削除
課金されないようにする最も簡単な方法は、チュートリアル用に作成したプロジェクトを削除することです。
プロジェクトを削除するには:
- 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.
関数の削除
このチュートリアルでデプロイした関数を削除するには、次のコマンドを実行します。
Node.js
gcloud functions delete kgSearch
Python
gcloud functions delete kg_search
Go
gcloud functions delete KGSearch
Java
gcloud functions delete java-slack-function
C#
gcloud functions delete csharp-slack-function
Ruby
gcloud functions delete kg_search
PHP
gcloud functions delete searchKnowledgeGraph
Google Cloud コンソールから Cloud Run functions を削除することもできます。