This is not an officially supported Google product
This repo contains a full example of a Google Cloud Function which can server as a bot for Google Hangouts Chat written in NodeJS. The bot is able to authenticate users with their Google account by means of a OAuth 2.0 web flow and acquire permissions for scopes from the Google Cloud Platform (GCP). In this particular example, the bot asks to access Google BigQuery on behalf of the user.
The following sections describe how to deploy the bot on Google Cloud Functions and which prior steps are necessary to prepare your GCP project.
Following a list of steps which prepare your GCP project and configure the code example:
First, you need to enable the following APIs in your GCP project:
Then, following steps are required:
- Install Google Cloud SDK or Use Google Cloud Shell
- Create Google Cloud Storage bucket for the function code
- Create Google Cloud Datastore in you GCP project
- Grant Google Cloud Build permissions to administer Cloud Functions
- Grant Cloud Function service account access to Cloud Datastore
To enable Google Cloud Build to deploy to Cloud Functions, you need to grant developer permissions and enable the use of service accounts. Add the permissions Service Account > Use Service Account and Cloud Functions > Developer to the Cloud Build service account.
Finally, copy src/param.js.template
to src/params.js
.
In the new file, copy your OAuth client credentials and add scopes (optional).
The deployment of the code is a one line execution using the Google Cloud SDK. From the root folder, simply run the following:
gcloud builds submit --substitutions=_BUCKET=[code-bucket]
You can use the following command to trigger the authentication flow from the shell:
curl -vvv -X POST -H "Content-Type: application/json" -d '{"configCompleteRedirectUrl": "https://google.com"}' https://[region]-[gcp-project-id].cloudfunctions.net/[function-name]/bot
To follow the OAuth flow, pick the URL included in the response and open it in a browser (e.g. Chrome).
Read more about concepts of a Hangouts Chat bot.
The procedure to publish the chat bot is as described in the documentation. When entering the URL of your bot, use the following pattern to construct your bot URL:
https://[region]-[gcp-prject-id].cloudfunctions.net/[function-name]/bot
- Michael Menzel, [email protected]