You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google ChatBot中使用resourceName处理上传附件并迁移至Drive?

解决Google ChatBot上传附件到指定Drive文件夹的问题

我用Google Apps Script开发Google ChatBot,需求是获取用户上传的附件(如PDF)并移动到指定Drive文件夹。目前能处理DRIVE_FILE类型的附件,但对于用户直接上传的UPLOADED_CONTENT类型,虽然能拿到resourceName,却不知道如何利用它完成文件迁移。当前代码如下:

function onMessage(event) {
  var destination = DriveApp.getFolderById("XXX")
  var name = (event.message.hasOwnProperty("attachment") ? name = event.message?.attachment[0]?.name?.trim() : "");
  var fileId = "";
  if (name != "") {
    console.log("attachment");
    if (event.message.attachment[0]?.source == "DRIVE_FILE") {
      console.log("Google ID " + event.message.attachment[0]?.driveDataRef?.driveFileId)
      fileId = event.message.attachment[0]?.driveDataRef?.driveFileId

    } else if (event.message.attachment[0]?.source == "UPLOADED_CONTENT") {
      console.log("Uploaded Content " + event.message.attachment[0]?.attachmentDataRef?.resourceName)
      var resourceName = event.message.attachment[0]?.attachmentDataRef?.resourceName
    }
    DriveApp.getFileById(fileId).moveTo(destination)
  }
  else {
    console.log("No attachment")
  }
}

解决方案

对于UPLOADED_CONTENT类型的附件,resourceName是Google Chat临时存储文件的资源标识,需要通过Google Chat API的媒体下载接口获取文件内容,再保存到Drive。具体步骤如下:

  1. 启用Google Chat API:在脚本编辑器中,点击「资源」>「高级Google服务」,找到「Google Chat API」并开启。

  2. 修改代码,添加上传文件处理逻辑:

    • 添加下载临时文件并保存到Drive的函数
    • 在原代码中针对UPLOADED_CONTENT类型调用该函数

完整示例代码:

function onMessage(event) {
  const destinationFolderId = "XXX"; // 替换为你的目标文件夹ID
  const destination = DriveApp.getFolderById(destinationFolderId);
  const attachment = event.message?.attachment?.[0];
  
  if (!attachment) {
    console.log("No attachment");
    return;
  }

  const attachmentName = attachment.name?.trim() || "Untitled File";
  
  if (attachment.source === "DRIVE_FILE") {
    const fileId = attachment.driveDataRef?.driveFileId;
    if (fileId) {
      console.log("Moving Drive file: " + fileId);
      DriveApp.getFileById(fileId).moveTo(destination);
    }
  } else if (attachment.source === "UPLOADED_CONTENT") {
    const resourceName = attachment.attachmentDataRef?.resourceName;
    if (resourceName) {
      console.log("Processing uploaded content: " + resourceName);
      const uploadedFile = downloadUploadedAttachment(resourceName, attachmentName);
      if (uploadedFile) {
        uploadedFile.moveTo(destination);
        console.log("Uploaded file saved to Drive");
      }
    }
  }
}

/**
 * 通过resourceName下载Chat中的上传文件并保存到Drive根目录,再返回文件对象
 * @param {string} resourceName - 附件的resourceName
 * @param {string} fileName - 文件名称
 * @returns {GoogleAppsScript.Drive.File} 保存后的Drive文件对象
 */
function downloadUploadedAttachment(resourceName, fileName) {
  try {
    // 调用Chat API下载媒体文件
    const mediaResponse = Chat.Media.download({
      resourceName: resourceName
    });
    
    // 将媒体内容转为Blob并保存到Drive
    const blob = mediaResponse.getBlob().setName(fileName);
    return DriveApp.createFile(blob);
  } catch (error) {
    console.error("Failed to download uploaded attachment: " + error.message);
    return null;
  }
}

关键说明

  • Chat.Media.download会获取临时文件的二进制内容,转为Blob后用DriveApp.createFile保存到Drive根目录,再移动到目标文件夹
  • 确保脚本有足够的权限:需要Drive的编辑权限和Chat API的访问权限
  • 处理异常情况,避免因API调用失败导致脚本报错

内容的提问来源于stack exchange,提问作者Tom91

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 13:22:09