Where do I even begin... (Google, why must you hurt me this way?)
Background Info
I have created a new chatbot using Google Apps Script, which receives messages from users in Google Chat and responds synchronously with a single message (each message can only have one response from the chatbot).
Now I need a way to send asynchronous messages so that the bot can send messages on its own, or send multiple separate responses at a time.
The problem
The Google Chat REST API has a method to create a message asynchronously, but this method (spaces.messages.create
) does not work! There are no working examples of this method from 2020.
Here is Google's example code for creating a message using the REST API.
The problem is that in their example, the SCOPE
is set to a URL that no longer exits:
var SCOPE = 'https://www.googleapis.com/auth/chat.bot';
If you navigate to that URL, you will see this 404 error:
Not Found
Error 404
Furthermore, if you check the list of available OAuth2 scopes, you will notice that there are no scopes related to Hangouts or Chat, and there is no mention of the chat.bot
the scope which was used in the example code.
What have I tried?
I have read through every question on StackOverflow that is related to this Chat API, plus every tutorial for the REST API.
chat.bot
scope no longer exists:
chat.bot
scope:
chat
scope (i.e. googleapis.com/auth/chat
), but that scope does not exist either:
In conclusion
How to send messages from Google Apps Script to Google Chat using the Google Chat REST API?
It seems that Google's documentation is outdated, and none of the examples for this API work as of August 2020. They are either unaware that their REST API does not work, or they deprecated the REST API without telling anyone.
I can confirm that the chat.bot
scope does indeed exist. To set up a chat bot with the REST API, you must use a service account.
As per the documentation you linked on Developing bots with Apps Script, for sending async messages on trigger:
...the only way to achieve this currently is via the external HTTP API (see documentation). This requires the use of a Cloud service account (see documentation) via the OAuth2 for Apps Script library.
This means, that you must first set up a service account in the GCP console so that the chat.bot
scope can be used for these messages. The whole process can be quite arduous for the unintitiated, so I will provide the steps from start to finish here.
Creating a Service Account:
Select a project
at the top of the page and click NEW PROJECT
.
Project name
, the other fields should be filled out for you automatically.CREATE
- a new pop-up will appear in the top-right of the screen confirming that a new project is being created. Once loaded, you can click VIEW
.☰
icon in the top-left, and follow the APIs & Services > Credentials
menu item.+ CREATE CREDENTIALS > Service Account
.
CREATE
, followed by CONTINUE
, and finally DONE
.Your service account has now been created.
Creating Service Account Credentials:
These will be needed for the code provided in the example from the Developing bots with Apps Script page.
Service Accounts
section, click you newly-created service account. This will be called [email protected]
ADD KEY > Create new key
JSON
selected, and press CREATE
.
Enabling the Hangouts Chat API:
☰ > APIs & Services
, and select Library
.Hangouts Chat API
and click the only result.ENABLE
. This will enable the API for your project.Note: Do not close this tab yet! We will still need to use the GCP console later.
Setting up the Apps Script Project:
private_key
value (the one that starts with -----BEGIN PRIVATE KEY-----
and paste it into value of SERVICE_ACCOUNT_PRIVATE_KEY
in the Apps Script project.client_email
value from the credentials file, and paste it into the SERVICE_ACCOUNT_EMAIL
in the Apps Script project.In order to use the Google Apps Script OAuth2 library as in the example, you will need to add the library to the project using the library's script ID.
Resources > Libraries...
menu item, and copy paste the OAuth2 script ID into the Add a library
box
1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
Save
.Next, you will need to link the Apps Script project to the GCP project you created earlier.
☰ > IAM & Admin > Settings
menu item.Project number
defined on this page.Resources > Cloud Platform project...
menu item, and paste the Project number into the Enter Project Number here
dialog.Set Project
.Setting up the Project Manifest:
In order to use a chat bot in Apps Script, you must include the chat
key in the project's manifest.
View > Show manifest file
. "chat": {
"addToSpaceFallbackMessage": "Thank you for adding me!"
}
Your full manifest file will now look something like this:
{
"timeZone": "Europe/Paris",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Drive",
"serviceId": "drive",
"version": "v2"
}],
"libraries": [{
"userSymbol": "OAuth2",
"libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF",
"version": "38"
}]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"chat": {
"addToSpaceFallbackMessage": "Thank you for adding me!"
}
}
You're nearly done! Now, you will need to deply the bot from manifest, and set up the configuration in GCP and set up the trigger which will make the actual call.
Deploying the bot:
Publish > Deploy from manifest...
and hit Create
in the newly opened dialog.
Save
.Get ID
next to the deployment you just created, and copy the Deployment ID
.Setting up GCP configuration:
☰ > APIs & Services > Dashboard
.Hangouts Chat API
.Configuration
.Bot name
, Avatar URL
, and Description
. Set up the functionality settings so that it works in rooms.Connection Settings
, select Apps Script project
, and paste in your deployment ID from the previous section.Save
.The Elusive Trigger:
The only thing you now need to do is set up your trigger. This is done like a normal Apps Script trigger - from the Edit > Current project's triggers
menu item in Apps Script. To complete the example, click the + Add Trigger button in the bottom right and set up the trigger settings as follows:
Choose which function to run: onTrigger
Choose which deployment should run: Head
Select event source: Time-driven
Select type of time based trigger: Minutes timer
Select minute interval: Every minute
And press save.
And you're done! This created bot will now post to all rooms that it is in the current time, every minute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With