development a telegram bot who download Google drive url to file plz help me
My code it's not working
// Configuration
var apiToken = "********";
var appUrl = "***************";
var apiUrl = "https://api.telegram.org/bot"+apiToken;
var command = {
"/start": "Hello! I am your Google Drive downloader bot. Send me a Google Drive link to download the file. Join for Updates @anonymous_robos",
"/help": "Send me a Google Drive link and I will download the file and send it to you",
"what is your name?": "My name is Gdrive2File Bot"
}
// Function to set webhook
function setWebhook(){
var url = apiUrl + "/setwebhook?url="+appUrl;
var res = UrlFetchApp.fetch(url).getContentText();
Logger.log(res);
}
// Function to extract the file ID from a Google Drive link
function extractFileIdFromDriveLink(link) {
var fileIdMatch = link.match(/\/file\/d\/([^\/]+)\//);
return fileIdMatch ? fileIdMatch[1] : null;
}
// Function to download file from Google Drive
function downloadFileFromDrive(fileId) {
try {
var file = DriveApp.getFileById(fileId);
var content = file.getBlob().getBytes();
var contentType = file.getMimeType();
var fileName = file.getName();
var response = {
content: content,
contentType: contentType,
fileName: fileName
};
return response;
} catch (error) {
Logger.log("Error downloading file:", error);
return null;
}
}
// Handle webhook
function doPost(e){
try {
var webhookData = JSON.parse(e.postData.contents);
var from = webhookData.message.from.id;
var text = webhookData.message.text;
var chatId = webhookData.message.chat.id;
var messageId = webhookData.message.message_id;
if (typeof command[text] == 'undefined') {
var sendText = encodeURIComponent("command not found");
} else {
var sendText = encodeURIComponent(command[text]);
}
// Check if the message contains a Google Drive link
var driveFileId = extractFileIdFromDriveLink(text);
if (driveFileId) {
// Send processing message
var processingMsg = "Processing... Please wait.";
var processingUrl = apiUrl + "/sendMessage?chat_id=" + chatId + "&text=" + encodeURIComponent(processingMsg);
UrlFetchApp.fetch(processingUrl, { muteHttpExceptions: true });
var driveResponse = downloadFileFromDrive(driveFileId);
if (driveResponse) {
// Send the file to the user
var url = apiUrl + "/sendDocument?chat_id=" + chatId;
var formData = {
document: {
fileName: driveResponse.fileName,
mimeType: driveResponse.contentType,
content: driveResponse.content,
}
};
var options = {
method: "post",
payload: formData,
muteHttpExceptions: true
};
UrlFetchApp.fetch(url, options);
// Edit the processing message with the file
var editUrl = apiUrl + "/editMessageText?chat_id=" + chatId + "&message_id=" + messageId + "&text=" + encodeURIComponent("File sent successfully!");
UrlFetchApp.fetch(editUrl, { muteHttpExceptions: true });
} else {
throw new Error("Error downloading file from Google Drive");
}
} else {
var url = apiUrl+"/sendmessage?parse_mode=HTML&chat_id="+from+"&text="+sendText;
var opts = {"muteHttpExceptions": true}
UrlFetchApp.fetch(url, opts).getContentText();
}
} catch (error) {
Logger.log("Error in doPost:", error);
var errorMsg = encodeURIComponent("An error occurred. Please try again later.");
var url = apiUrl+"/sendmessage?parse_mode=HTML&chat_id="+from+"&text="+errorMsg;
var opts = {"muteHttpExceptions": true}
UrlFetchApp.fetch(url, opts).getContentText();
}
}
// Handle GET request
function doGet(e){
return ContentService.createTextOutput("Method GET not allowed");
}
Hi! You have to create a separate Apps Script project for sharing the code for us
https://script.google.com/d/1pAyNlLKSTacnQPc6cLH4RvJwnu8n3u0VWwSenwqu8Z05Ss6NAoqtb_Tw/edit?usp=sharing
I can watch this later
Just see your wish sir I have just started learning coding and programming Please help this child a little 🥹
This code should not work. Because, in my opinion, it's done using GPT and you even didn't try to understand what's wrong. For start, I'd recommend you use console.log in different parts of the code, so you can understand where is the problem.
It's Google script I try like javascript console.log but it not show console.log message
Welcome aboard, sir
You can send screenshots and/or code as markdown text here
The Apps script doesn't exist.
Hmm I delete them
Can u try to start script and after send screenshot of result?
Hm, you needn't help?
Это doPost, не вводите в заблуждение, толку от запуска не будет.
Sir script live again https://script.google.com/d/10KzH-IHNkx5BkJUBEG339MuFaiPjH_WqcrdQJpcarpdN_t0JOHctUb27/edit?usp=sharing
You didn't answer - what does bot send you when you send link?
A file that contains a gdrive url Ex- when I send to bot a gdrive link(it's. A pdf) then bot send me that pdf file https://drive.google.com/file/d/1Z16edv7U8OKJyQH8ynvIdLxpEBbXUSoc/view?pli=1
You wrote that it doesn't work, now you write that it works. Bingo? It started working?
No code work but I not received file in bot some error in my code
Try to write function that send document without doPost, and without mutehttpExceptions:true. And run it to check errors. So you can get error from urlfetchapp. Then change code to make to work it.
Обсуждают сегодня