【发布时间】:2022-01-10 14:36:37
【问题描述】:
我正在尝试将图片图表从 Google 表格发送到 Telegram。 我将图像图表保存到 Google 磁盘,然后将其发送到 Telegram 聊天(帮助我的电报机器人)。
如果我以这种方式将 Google Drive 链接发送到电报: 第一次尝试:
file_id_0 = "https://drive.google.com/file/d/1kvitP05ofdyT4YtHgNBdjP-sxIFQlpo7/view?usp=drivesdk";
这是第一次尝试结果:First Try - 很糟糕:我只想在电报聊天中看到图像 - 没有链接。
在此站点上,我找到了解决此问题的方法(抱歉,我丢失了链接):将 Google Docs 与 Google Drive 中的 file_id 一起使用。 “/d/file_id/view”之间链接上的文件 ID。 为了确认这个决定,我获取了 Google Drive 上其他文件的 id 并将其连接起来:
file_id_2 = "1bSSzt5S9SgafeAK7D6dfRiFGECxhSuXo";
sendImage(chatId, "https://docs.google.com/uc?id=" + file_id_2);
第二次尝试结果:Second Try - 非常完美 - 我需要!
现在我正在尝试从 Google Drive 文件中提取 file_id
file.getId()
并将此 ID 连接到 google 文档链接。
第三次尝试结果:third try - 不成功。 如果我在调试程序中获取链接并在浏览器中打开它 - 它正确打开(如第二次尝试)。在我看来,第二个和第三个链接的链接没有区别。至少它们在浏览器中正确打开。
我应该如何将此链接插入到我的函数中,以免出现问题?
P.S:我很抱歉我的英语。我准备好给出任何解释,我希望这些图片有助于理解我的问题。
完整代码:
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");
}
【问题讨论】:
标签: google-apps-script google-sheets telegram google-docs google-docs-api