Sending a “please wait” message in google hangouts chat

半世苍凉 提交于 2021-02-11 18:20:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!