I am trying to build a Google Chat App that calls a Cloud Run function as the logic. I am assuming the GCP interfaces must have recently changed/updated because a lot of the guides and troubleshooting do not match the UI elements I am seeing in GCP.
I am getting the below two errors in my Log Explorer when I try to send a simple message to the Chat Bot.
Failed to parse JSON as RenderActions, DataActions or Card.
Failed to parse JSON as RenderActions. Failed with error: Cannot find field: cardsV2 in message google.apps.card.v1.RenderActions
Failed to parse JSON as DataActions. Failed with error: Cannot find field: cardsV2 in message google.apps.card.v1.DataActions
Failed to parse JSON as Card. Failed with error: Cannot find field: cardsV2 in message google.apps.card.v1.Card
and
Can't post a reply. The Chat app didn't respond or its response was invalid. If your Chat app is configured as an add-on, see "Build Google Chat interfaces" (https://developers.google.com/workspace/add-ons/chat/build) in the Google Workspace add-ons documentation. Otherwise, see "Receive and respond to Google Chat events" (https://developers.google.com/chat/api/guides/message-formats) in the Chat API documentation.
Below is my Cloud Function (Node.js).
/**
* Google Chat Bot Echo function.
*
* This function is triggered by an HTTP request from Google Chat.
* It processes the event payload and responds with a structured card message.
*
* @param {object} req The HTTP request object.
* @param {object} res The HTTP response object.
*/
exports.chatBot = (req, res) => {
// Check if the request method is POST. Google Chat sends POST requests.
if (req.method !== 'POST') {
return res.status(403).send('Forbidden');
}
const event = req.body;
console.log('Received event:', JSON.stringify(event, null, 2));
let responseMessage;
// Handle different event types from Google Chat.
switch (event.type) {
case 'ADDED_TO_SPACE':
// This event is triggered when the bot is added to a space or a DM.
const spaceType = event.space.type;
if (spaceType === 'DM') {
responseMessage = `Hi ${event.user.displayName}! Thanks for starting a chat. I'll echo back anything you say.`;
} else {
responseMessage = `Thanks for adding me to ${event.space.displayName}! I'm ready to echo messages.`;
}
break;
case 'MESSAGE':
// This event is triggered when a user sends a message to the bot.
// event.message.argumentText contains the text sent after the bot's @-mention.
const userMessage = (event.message.argumentText || '').trim();
if (userMessage) {
responseMessage = `You said: "${userMessage}"`;
} else {
responseMessage = 'Hello! Please mention me and type a message.';
}
break;
case 'REMOVED_FROM_SPACE':
// This event is triggered when the bot is removed. No response is needed.
console.log(`Bot removed from ${event.space.displayName}.`);
return res.status(200).send();
default:
// For any other event type, send a default response.
responseMessage = 'I received an event I don't know how to handle.';
break;
}
// For a Google Workspace Add-on responding to a MESSAGE event,
// the response must be a `renderActions` object.
const reply = {
"renderActions": {
"action": {
"navigations": [
{
"pushCard": {
"header": {
"title": "Echo Bot"
},
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": responseMessage
}
}
]
}
]
}
}
]
}
}
};
// Send the JSON response back to Google Chat.
res.status(200).json(reply);
};
I am fairly new to GCP Google Chat API and Cloud Run Functions, so my apologies if this question seems a bit obvious. But I have been stuck for a while and cannot find a resolution anywhere. Any help would be much appreciate!