简体   繁体   English

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

[英]How to download files from telegram to Gdrive using Google App Script by bot API

Noob question, I am trying to download files from Telegram servers to Google Drive using Bot Api and Google App Script.菜鸟问题,我正在尝试使用 Bot Api 和 Google App Script 将文件从 Telegram 服务器下载到 Google Drive。 As far as I know there is a bot called @getpubliclink in Telegram, which downloads the files to their servers and again re-upload them to Google Drive and it creates bandwidth problem and not a server-less one.据我所知,Telegram 中有一个名为 @getpubliclink 的机器人,它将文件下载到他们的服务器,然后再次将它们重新上传到 Google Drive,这会造成带宽问题,而不是无服务器问题。

So if this concepts works, then those issues will be solved.因此,如果这个概念有效,那么这些问题将得到解决。

This is my code in Google App Script:这是我在 Google App Script 中的代码:

 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(); } } }

So far I got the file as text in response and I am using fileURL for Google Drive but it needs the url, if a better solution is available.到目前为止,我将文件作为文本作为响应,并且我正在为 Google Drive 使用fileURL ,但如果有更好的解决方案可用,它需要 url。 How can I download these files into a Google Drive?如何将这些文件下载到 Google Drive?

I used this script in my project to move the file in Telegram to Google Drive.我在我的项目中使用这个脚本将 Telegram 中的文件移动到 Google Drive。 And it worked!它奏效了!

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM