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
To create a webhook, register it in the Google Chat space you want to receive messages, then write a script that sends messages.
- Open Google Chat in a web browser.
- Go to the space to which you want to add a webhook.
- At the top, next to space title, click arrow_drop_down Down Arrow > Manage webhooks.
- If this space already has other webhooks, click Add another. Otherwise, skip this step.
- For Name, enter "Quickstart Webhook".
- For Avatar URL, enter
https://developers.google.com/chat/images/chat-product-icon.png
. - Click SAVE.
- Click content_copy Copy to copy the full webhook URL.
- Click outside the box to close the Incoming webhooks dialog.
For more details, please refer to this documentation
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/
For more details about how to deploy a Cloud Function, please refer to this documentation
- First of all clone the github repo
git clone https://github.com/jbleroy1/google-chat-notifier.git
- Create pub/sub topic
gcloud pubsub topics create chat-notification
- 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 >
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
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.