Tutorial ini mendemonstrasikan penggunaan Cloud Run Functions untuk menerapkan Perintah Garis Miring Slack yang menelusuri Google Knowledge Graph API.
Tujuan
- Membuat Perintah Garis Miring di Slack.
- Menulis dan men-deploy fungsi Cloud Run HTTP.
- Menelusuri Google Knowledge Graph API menggunakan Perintah Garis Miring.
Biaya
Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:
- Cloud Run functions
Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda,
gunakan kalkulator harga.
Sebelum memulai
- 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, and Google Knowledge Graph Search APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, and Google Knowledge Graph Search APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init - Siapkan lingkungan pengembangan Anda.
- Pengguna menjalankan Perintah Garis Miring
/kg <search_query>di saluran Slack. - Slack mengirimkan payload perintah ke endpoint pemicu fungsi.
- Fungsi berikut mengirim permintaan dengan kueri penelusuran pengguna ke Knowledge Graph API.
- Knowledge Graph API merespons dengan hasil yang cocok.
- Fungsi memformat respons menjadi pesan Slack.
- Fungsi mengirim pesan kembali ke Slack.
- Pengguna akan melihat respons terformat di saluran Slack.
Buat clone repositori aplikasi contoh ke komputer lokal Anda:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Ubah ke direktori yang memuat kode contoh 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/
Ruby
cd ruby-docs-samples/functions/slack/
Buat Aplikasi Slack untuk menghosting Perintah Garis Miring di Slack. Kaitkan dengan tim Slack tempat Anda memiliki izin untuk menginstal integrasi.
Buka Slash commands, lalu klik tombol Create new command.
Masukkan
/kgsebagai nama perintah.Masukkan URL untuk perintah:
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
dengan
YOUR_REGIONsebagai region tempat fungsi di-deploy danYOUR_PROJECT_IDadalah project ID Cloud Anda.Kedua nilai akan terlihat di terminal Anda setelah fungsi selesai di-deploy.
Klik Save.
Buka Basic Information.
Klik Install your app to your workspace dan ikuti petunjuk di layar guna mengaktifkan aplikasi untuk ruang kerja Anda.
Perintah Garis Miring di Slack Anda akan segera online.
Ketik perintah ke saluran Slack Anda:
/kg giraffe
Perhatikan log untuk memastikan eksekusi telah selesai:
gcloud functions logs read --limit 100
- 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.
Jika Anda sudah menginstal gcloud CLI, update dengan menjalankan perintah berikut:
gcloud components update
Memvisualisasikan aliran data
Alur data di aplikasi tutorial Perintah Garis Miring di Slack melibatkan beberapa langkah:
Alur ini dapat memvisualisasikan langkah-langkahnya:
Membuat kunci Knowledge Graph API
Di halaman Credentials konsolGoogle Cloud , klik tombol Create credentials, lalu pilih API key. Ingat kunci ini, karena Anda akan menggunakannya untuk mengakses Knowledge Graph API di bagian berikutnya.
Menyiapkan fungsi
Men-deploy fungsi
Untuk men-deploy fungsi yang dijalankan saat Anda (atau Slack) membuat permintaan POST HTTP
ke endpoint fungsi, jalankan perintah berikut
di direktori yang berisi kode contoh (atau file pom.xml untuk Java):
Ganti YOUR_SLACK_SIGNING_SECRET dengan secret penandatanganan
yang disediakan oleh Slack di halaman Basic information pada konfigurasi
aplikasi Anda, dan YOUR_KG_API_KEY dengan kunci Knowledge Graph API yang Anda buat sebelumnya.
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
Gunakan flag --runtime
untuk menentukan ID runtime
versi Node.js yang didukung untuk menjalankan
fungsi Anda.
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
Gunakan flag --runtime
untuk menentukan ID runtime
versi Python yang didukung untuk menjalankan
fungsi Anda.
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
Gunakan flag --runtime
untuk menentukan ID runtime
versi Go yang didukung untuk menjalankan
fungsi Anda.
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
Gunakan flag --runtime
untuk menentukan ID runtime
versi Java yang didukung untuk menjalankan
fungsi Anda.
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
Gunakan flag --runtime
untuk menentukan ID runtime
versi Ruby yang didukung untuk menjalankan
fungsi Anda.
Mengonfigurasi aplikasi
Setelah fungsi di-deploy, Anda perlu membuat Perintah Garis Miring di Slack yang mengirimkan kueri ke fungsi setiap kali perintah dipicu:
Memahami kode
Mengimpor dependensi
Aplikasi harus mengimpor beberapa dependensi untuk berkomunikasi dengan layanan Google Cloud Platform:
Node.js
Python
Go
Java
Ruby
Menerima webhook
Fungsi berikut dijalankan saat Anda (atau Slack) membuat permintaan POST HTTP ke endpoint fungsi:
Node.js
Python
Go
Java
Ruby
Fungsi berikut mengautentikasi permintaan masuk dengan memverifikasi
header X-Slack-Signature yang disediakan oleh Slack:
Node.js
Python
Go
Java
Ruby
Membuat Kueri Knowledge Graph API
Fungsi berikut mengirim permintaan dengan kueri penelusuran pengguna ke Knowledge Graph API:
Node.js
Python
Go
Java
Ruby
Memformat pesan Slack
Terakhir, fungsi berikut memformat hasil Knowledge Graph menjadi pesan Slack berformat lengkap yang akan ditampilkan kepada pengguna:
Node.js
Python
Go
Java
Ruby
Waktu tunggu Slack API
Slack API mengharapkan fungsi Anda merespons dalam waktu 3 detik setelah menerima permintaan webhook.
Perintah dalam tutorial ini biasanya memerlukan waktu kurang dari 3 detik untuk merespons. Untuk perintah yang berjalan lebih lama,
sebaiknya konfigurasikan fungsi untuk permintaan push (termasuk response_url) ke Pub/Sub topik
yang bertindak sebagai task queue.
Kemudian, Anda dapat membuat fungsi kedua yang dipicu oleh Pub/Sub
yang memproses tugas tersebut dan mengirimkan hasilnya kembali ke response_url Slack.
Menggunakan perintah Garis miring
Pembersihan
Agar tidak menimbulkan biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus masing-masing resource.
Menghapus project
Cara termudah untuk menghilangkan penagihan adalah dengan menghapus project yang Anda buat untuk tutorial.
Untuk menghapus project:
Menghapus fungsi
Untuk menghapus fungsi yang Anda deploy dalam tutorial ini, jalankan perintah berikut:
Node.js
gcloud functions delete kgSearch
Python
gcloud functions delete kg_search
Go
gcloud functions delete KGSearch
Java
gcloud functions delete java-slack-function
Ruby
gcloud functions delete kg_search
Anda juga dapat menghapus fungsi Cloud Run dari konsolGoogle Cloud .