【问题标题】:where to store files for Telegram Bot API usage?在哪里存储 Telegram Bot API 使用的文件?
【发布时间】:2021-03-19 11:50:54
【问题描述】:

提前感谢您的帮助

如何使用 multipart/form-data 将本地照片文件传输到谷歌驱动器?

我已将本地文件复制到谷歌驱动器,获得谷歌驱动器文件链接,但以下代码行挂起并出现与 html 相关的错误???

var response = UrlFetchApp.fetch(vUrlTelegram + "/sendPhoto?chat_id=" + chat_id + "&photo=" + photoUrl );

注意: 已从 Internet 获取示例照片的 html 字符串,并且以下电报 bot google api javascript 代码有效

function sendPhoto( chat_id, photoUrl ) {
var response = UrlFetchApp.fetch(vUrlTelegram + "/sendPhoto?chat_id=" + chat_id + "&photo=" + photoUrl );
console.log(response.getContentText());  

}

电报文档

要发送的照片。 将 file_id 作为字符串传递以发送存在于 Telegram 服务器上的照片(推荐), 将 HTTP URL 作为字符串传递给 Telegram 以从 Internet 获取照片,或者 使用 multipart/form-data 上传新照片。有关发送文件的更多信息 »

【问题讨论】:

  • 您能描述一下您遇到的错误吗?

标签: javascript google-apps-script telegram-bot


【解决方案1】:
  • 从您的问题来看,当下面的脚本使用互联网图片的直接链接时,您可以确认该脚本有效。

      function sendPhoto( chat_id, photoUrl ) {
        var response = UrlFetchApp.fetch(vUrlTelegram + "/sendPhoto?chat_id=" + chat_id + "&photo=" + photoUrl );
        console.log(response.getContentText());
      }
    

从这种情况来看,当你想将Google Drive上的图片文件用于上述脚本时,下面的流程如何?

流程:

  1. Viewer of Anyone with the link 的身份在Google Drive 上公开共享图像文件。
  2. 使用图像文件的webContentLink作为photoUrl
    • 在这种情况下,当使用图像文件的文件ID时,webContentLink如下。

        https://drive.google.com/uc?export=download&id={fileId}
      

示例脚本:

例如,当镜像文件被公开共享并运行UrlFetchApp.fetch,共享镜像文件停止时,脚本如下。

var fileId = "###"; // Please set the file ID of the image file.
var file = DriveApp.getFileById(fileId)

file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);  // Image file is publicly shared.

var photoUrl = "https://drive.google.com/uc?export=download&id=" + fileId;
var response = UrlFetchApp.fetch(vUrlTelegram + "/sendPhoto?chat_id=" + chat_id + "&photo=" + photoUrl );
console.log(response.getContentText());

// Utilities.sleep(2000); // I'm not sure whether this is required for this situation.
file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);  // Sharing image file is stopped.

参考资料:

【讨论】:

  • a/ 函数 sendPhoto(chat_id, photoUrl) { var response = UrlFetchApp.fetch(vUrlTelegram + "/sendPhoto?chat_id=" + chat_id + "&photo=" + photoUrl ); console.log(response.getContentText()); } 有效,因为我已经测试了照片 html 链接 1.1/ 是的,此链接有效 thumbs.dreamstime.com/z/… 1.2/ 不,此链接无效
  • @Trajano Roberto 感谢您的回复。我不得不为我糟糕的英语水平道歉。很遗憾,我无法理解您的回复。可以问一下它的详细情况吗?我想确认一下。
  • 它现在可以工作了,在我将谷歌驱动器文件夹更改为:任何知道链接的人之后,查看器
  • 感谢您的回复。现在我可以理解了。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 2019-02-16
  • 2017-12-27
  • 2019-08-07
  • 2014-03-13
  • 1970-01-01
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多