在Google Apps脚本聊天机器人中尝试捕获

时间:2018-12-18 15:07:15

标签: google-apps-script hangouts-chat

我一直未尝试在Google Apps脚本环聊聊天机器人中使用try-catch语法。我正在尝试打开一个要写入的Google文档。如果无法打开它,则返回错误消息以聊天。否则,写入文档并返回成功消息。问题在于,打开或写入文档时发生错误似乎永远不会进入代码的catch部分。请注意,我已经尝试了从返回“错误”一词到编写控制台日志消息的所有方法,但是每次发生错误(应调用catch部分)时,机器人都会返回一个没有响应的错误,并且程序中没有任何内容。日志。

function onMessage(event) {
  // Format the message to be appended to the document
  var email = Session.getActiveUser().getEmail();
  var formattedDate = Utilities.formatDate(new Date(), "UTC", "yyyy-MM-dd' 'HH:mm:ss'Z'");
  var message = formattedDate + " - " + email + " - " + event.message.argumentText.trim();

  //Get the document ID from the script properties
  var scriptProperties = PropertiesService.getScriptProperties();
  var docId = scriptProperties.getProperty(event.space.name);

  // Open the Google document
  try {
    var text = DocumentApp.openById(docId).getBody().editAsText();
  } catch(e) {
    return {'text':  'error'};
  }

  // Write the message into the document 
  text.appendText('\n' + message);
  return {'text':  'Done - Wrote text to document.'};
}

0 个答案:

没有答案