Home > Software engineering >  Send Image From Google Sheets (Google Disk) to Telegram bot
Send Image From Google Sheets (Google Disk) to Telegram bot

Time:12-06

I'm trying sent images chart from Google Sheets to Telegram. I'm saving image chart to Google Disk and then send it to Telegram chat (helping this my telegram bot).

If I send Google Drive link to telegram in that way: first try:

file_id_0 = "https://drive.google.com/file/d/1kvitP05ofdyT4YtHgNBdjP-sxIFQlpo7/view?usp=drivesdk";

This is first try result: enter image description here

On this site I found resolve this problem (sorry I'm loss the link): use Google Docs with file_id from Google Drive. File ID on link up between "/d/file_id/view". To confirm this decision, I took the id of some other file on Google Drive and concatenate it:

file_id_2 = "1bSSzt5S9SgafeAK7D6dfRiFGECxhSuXo";
sendImage(chatId, "https://docs.google.com/uc?id="   file_id_2);

Second try result: enter image description here

Now I'm trying to extract file_id from Google Drive file file.getId() and concatenate this Id to google docs link.

Third try result:enter image description here If I grab the link in debug programm and open this at browser - this opened correctly (as in second try). In my opinion link on second and third link without differences. At least they open correctly in the browser.

How should I insert this link to my function so that there are no problems?

P.S: I'm so sorry for my English. I am ready to give any explanations and I hope that the pictures helped understanding my problem.

Full Code:

function downloadChart2() {
  let chatId = "-xxx";
  var sheet = SpreadsheetApp.getActiveSheet();

  // Get chart and save it into your Drive
  var chart = sheet.getCharts()[0];
  var file = DriveApp.createFile(chart.getBlob());

  // Set url in one cell and resize the column 
  sheet.getRange(23, 1).setValue(file.getUrl())
  sheet.autoResizeColumn(1);
  file.setName("1234");

  // first try - successful, but bad view on Telegram
  var file_id_0 = file.getUrl();
  sendText(chatId, file_id_0);

  // second try - successful, but i grab this id on adress panel in my brouser
  var file_id_1 = "1cAdAMWFdzZFRgXiM9kbxkzrwVAGpoOIy";
  sendImage(chatId, "https://docs.google.com/uc?id="   file_id_1);

  // third try - unsuccessful
  var file_id_2 = file.getId();
  var file_id_3 = "https://docs.google.com/uc?id="   file_id_2; // this link correctly
  sendImage(chatId, file_id_3); // but this don't work
  }
  }

function sendImage(chatId, text, keyBoard) {

  let data = {
    method: 'post',
    payload: {
      method: 'sendMessage',
      chat_id: String(chatId),
      text: text,
      parse_mode: 'HTML',
      reply_markup: JSON.stringify(keyBoard)
    }
  }
  var caption = "Second";
   UrlFetchApp.fetch("https://api.telegram.org/bot"   token   "/sendPhoto?caption="   encodeURIComponent(caption)   "&photo="   encodeURIComponent(text)   "&chat_id=" 
     chatId   "&parse_mode=HTML");
}

CodePudding user response:

When I saw your script, I thought that the created image file might not be publicly shared. I thought that this might be the reason for your issue. So how about the following modification?

From:

var file = DriveApp.createFile(chart.getBlob());

To:

var file = DriveApp.createFile(chart.getBlob());
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

Reference:

  • Related