问题
I'm creating a google hangouts chatbot. I'm building the bot in Google Apps Script. The bot accesses some web apis that usually take a few seconds, but the wait can be disconcerting to the user who doesn't realize the system is not just ignoring them. I want to display a message like "please wait" before the results come in. But the event is triggered by onMessage and text is displayed as part of the return statement. Is there a way to show an immediate message and then the rest of the message when the api responds?
onMessage(event){
text="Please wait, processing...";
#send text to screen
response=UrlFetch(url, params);
return {"text":response['text']}
}
回答1:
The GAS package doesn't support inserting a message. It turns out you have to use the Chat REST API for that. Thus you have set up permissions (I used a service account and cgoa package at http://ramblings.mcpher.com/Home/excelquirks/goa)
Then I created a function that called the API and put the call in the onMessage event at the top. Here's the function:
function sendWait(event){
var packageName='Google_service_account'
var goa = cGoa.GoaApp.createGoa(packageName, PropertiesService.getScriptProperties()).execute();
if (!goa.hasToken()) {
throw 'no token retrieved';
} else {console.info('token retrieved')};
var endpoint='https://chat.googleapis.com/v1/'+event.space.name+"/messages"
console.info('endpoint=%s',endpoint);
var threadId=event.message.thread
var response={'text':'Processing request...'}
response.thread=threadId
console.info('response=%s',response);
var options = {
method: "post",
contentType : "application/json" ,
muteHttpExceptions : true,
payload: JSON.stringify(response),
headers: {
"Authorization": "Bearer " + goa.getToken(),
}
};
var aresponse=UrlFetchApp.fetch(endpoint, options)
来源:https://stackoverflow.com/questions/55788704/sending-a-please-wait-message-in-google-hangouts-chat