Reputation: 11
I am trying to create a survey bot based on this article (https://www.cloudskillsboost.google/focuses/21000?parent=catalog) without using node.js and cloud functions and only using Google Apps Script. I think it's possible to preserve the survey results as the state parameter and retrieve it and update it each time the Vote button is clicked. The intent is to use the event object when the Vote button is clicked (and the OnCardClicked trigger runs) to retrieve the survey results (via the state parameter), update the state object with the option clicked by the user, recreate the message card with the updated state object and then update the existing message in the chat space. But, when my recordVote function recreates the message card with the updated survey results and attempts to update the existing message, no updates to the message show.
I am using the Google Chat API to post the survey to the chat space using the sendPushMessage function below. The recordVote function below is run by the onCardClick function after one of the Vote buttons is clicked in the original survey message. I tested the card format I'm trying to return with the recordVote function by passing it directly into the sendPushMessage function and it worked to post the updated survey (with the updated results) but as a new message instead of updating the original.
Do I need to create a thread_key with my original message and pass that in with the update message?
function sendPushMessage(message,spaceName) {
var service = getService();
if (service.hasAccess()) {
var url = 'https://chat.googleapis.com/v1/'+spaceName+'/messages';
var options = {
method : 'POST',
contentType: 'application/json',
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
payload : JSON.stringify(message)
}
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
} else {
Logger.log(service.getLastError());
}
}
function recordVote(event) {
const parameters = event.common?.parameters;
const choice = parseInt(parameters['index']);
const userId = event.user.name;
const state = JSON.parse(parameters['state']);
// Add or update the user's selected option
state.votes[userId] = choice;
const card = buildVoteCard(state);
return {
thread: event.message.thread.name,
actionResponse: {
type: 'UPDATE_MESSAGE',
},
cards: [card],
}
}
Upvotes: 0
Views: 696
Reputation: 8118
Change your thread: event.message.thread.name,
to thread: event.message.thread,
thread_key
is not a must, but you should send thread
as object.
reference: https://developers.google.com/chat/api/reference/rest/v1/spaces.messages#thread
I tested that on my app, and it's works
Upvotes: 0