如何通过Bot API使用Google App Script将文件从电报下载到Gdrive

时间:2018-10-18 03:21:29

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

新手问题,我正在尝试使用Bot Api和Google App Script将文件从电报服务器下载到Google云端硬盘。据我所知,Telegram中有一个名为@getpubliclink的机器人,该机器人将文件下载到它们的服务器,然后再次将它们重新上传到Google云端硬盘,这会造成带宽问题,而不是没有服务器的问题。

因此,如果这个概念可行,那么这些问题将得到解决。

这是我在Google App脚本中的代码:

var token = "token"; // FILL IN YOUR OWN TOKEN
var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = "https://script.google.com/macros/"; // FILL IN YOUR GOOGLE WEB APP ADDRESS
//var folder = "1-n9vtrED_oUOKrTnHlEeZR0B2uK8it9O"; // FILL IN THE ID OF YOUR SPREADSHEET
var downloadUrl = "https://api.telegram.org/file/bot" + token;



function getMe() {
  var url = telegramUrl + "/getMe";
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function getUpdates(){
  var url = telegramUrl + "/getUpdates";
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
  
}

function setWebhook() {
  var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function getFile(file_id) {
  var url = telegramUrl + "/getFile?file_id=A...EC";
  var response = UrlFetchApp.fetch(url);
   Logger.log(response.getContentText());
}

function File(file_id,file_path) {
  var FileURL = downloadUrl + "/photos/f.jpg";
 // var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}







function downloadFile(fileURL,folder) {
  
  var fileName = "";
  var fileSize = "file_size";
  
  var response = UrlFetchApp.fetch(fileURL, {muteHttpExceptions: true});
  var rc = response.getResponseCode();
  
  if (rc == 200) {
    var fileBlob = response.getBlob()
    var folder = DocsList.getFolder(folder);
    if (folder != null) {
      var file = folder.createFile(fileBlob);
      fileName = file.getName();
      fileSize = file.getSize();
    }
  }

}

到目前为止,我已经将文件作为文本得到响应,并且我正在将fileURL用于Google云端硬盘,但如果有更好的解决方案,它需要使用该网址。如何将这些文件下载到Google云端硬盘?

1 个答案:

答案 0 :(得分:0)

我在我的项目中使用此脚本将 Telegram 中的文件移动到 Google Drive。 它奏效了!

var fileid = contents.message.photo[contents.message.photo.length - 1]["file_id"];

var response = UrlFetchApp.fetch(url + '/getFile?file_id=' + fileid);

var urlphoto = 'https://api.telegram.org/file/bot' + token + '/' + JSON.parse(response.getContentText()).result["file_path"];

var resa = UrlFetchApp.fetch(urlphoto);
var blob = resa.getBlob();

var jum = DriveApp.createFile(blob).setName(new Date().toLocaleString());