Google-chat-notification

This project provide tools that helps to send message to Google Chat from any pub/sub message. The tools support templating messages base on Handlebars

Create a Google chat webhook

To create a webhook, register it in the Google Chat space you want to receive messages, then write a script that sends messages.

Step 1: Register the incoming webhook

  1. Open Google Chat in a web browser.
  2. Go to the space to which you want to add a webhook.
  3. At the top, next to space title, click arrow_drop_down Down Arrow > The icon for manage webhooks Manage webhooks.
  4. If this space already has other webhooks, click Add another. Otherwise, skip this step.
  5. For Name, enter "Quickstart Webhook".
  6. For Avatar URL, enter https://developers.google.com/chat/images/chat-product-icon.png.
  7. Click SAVE.
  8. Click content_copy Copy to copy the full webhook URL.
  9. Click outside the box to close the Incoming webhooks dialog.

For more details, please refer to this documentation

Deploy a handlebar (HBS) template and upload to a GCS bucket

This function use handlebar templating. Thanks to this you can create powerfull message template depends on the pub/sub message recieved. Google chat support 2 type of message :

You can find 2 sample of Handlebars templating for message here with corresponding event and for cards here with this event.

To render the .hbs template, the library use JsonPath. You receive a JSON message from pubsub, then the function populate the template with the JSON value refered to the JsonPath syntax provided in your hbs file ( with the {{ <"jsonptath query">}} syntax).

Let say your hbs file is "mytemplate.hbs" and the bucket "mybucket-hbs" has been created. Execute :

# if my mybucket-hbs does not exist
# gsutil mb gs://mybucket-hbs/

# Copy template to the target bucket
gsutil cp mytemplate.hbs gs://mybucket-hbs/

Deploy the Cloud Function

For more details about how to deploy a Cloud Function, please refer to this documentation

Clone the github repo

  1. First of all clone the github repo
git clone https://github.com/jbleroy1/google-chat-notifier.git
  1. Create pub/sub topic
gcloud pubsub topics create chat-notification
  1. Deploy Cloud Function
gcloud functions deploy my-pubsub-function \
 --gen2 \
 --region=europe-west2 \
 --runtime=java17 \
 --source=./google-chat-notifier \
 --entry-point=com.google.cloud.PubSubListener \
 --trigger-topic=chat-notification \
 --set-env-vars CHAT_URL="< chat web hook >",TEMPLATE_FILE_BUCKET=< bucket name >,TEMPLATE_FILE_OBJECT=< HBS object name >

Service account

This documentation use the default service account provided by Cloud Function. You can specify your own SA. Be sure this SA have :

  • Read access to the HBS GCS bucket (access to the template)
  • Pub/Sub can send notification to your Cloud Function endpoint

Disclaimer

This is not an officially supported Google product. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.