如何通过GmailApp.sendEmail在Telegram机器人中发送图片与音频?
在Telegram机器人中通过GmailApp.sendEmail发送图片与音频
首先,先看你现有的代码片段:
function doPost(e){ var data = JSON.parse(e.postData.contents); //Part 1 if (data.message.text){ var text = data.message.text; var id = data.message.chat.id; var name = data.message.chat.first_name ; var answer = "Hi " + name + ", thank you for your comment "; var answer2 = "Hi " + name + ", thank you for your comment " + "\n -----------\n " + text; sendText(id, answer); GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Message send to Babak", answer2); //Part 2 } else if (data.message.... }
要实现图片和音频的邮件发送,得从识别Telegram的媒体消息、获取文件、再添加到邮件附件这几步入手,具体实现如下:
1. 先搞懂Telegram的媒体消息结构
Telegram通过webhook推送的消息里:
- 图片消息存在
data.message.photo数组中(数组元素是不同分辨率的图片,一般取最后一个即最高清版本) - 音频消息存在
data.message.audio对象中
我们需要先判断这些字段是否存在,再做后续处理。
2. 获取媒体文件的下载链接
要拿到用户发送的图片/音频,得调用Telegram Bot API的getFile接口,传入文件的file_id,拿到file_path后拼接成完整的下载URL:https://api.telegram.org/bot<你的BotToken>/file/<file_path>
3. 下载文件并作为邮件附件发送
用UrlFetchApp.fetch下载文件,转换成Blob对象,然后在GmailApp.sendEmail的配置参数里添加attachments选项即可。
修改后的完整代码示例
// 替换成你自己的Telegram Bot Token(从@BotFather获取) const BOT_TOKEN = "你的BotToken"; function doPost(e){ var data = JSON.parse(e.postData.contents); const chatId = data.message.chat.id; const userName = data.message.chat.first_name; const emailRecipient = Session.getEffectiveUser().getEmail(); let emailSubject = "新消息来自Telegram用户 " + userName; let emailBody = `用户 ${userName} 发送了媒体消息\n`; // 保留原有文本消息处理逻辑 if (data.message.text){ const text = data.message.text; const answer = `Hi ${userName}, thank you for your comment `; const emailBodyText = `Hi ${userName}, thank you for your comment \n -----------\n ${text}`; sendText(chatId, answer); GmailApp.sendEmail(emailRecipient, `文本消息来自 ${userName}`, emailBodyText); } // 处理图片消息 else if (data.message.photo){ // 取最高分辨率的图片 const photo = data.message.photo[data.message.photo.length - 1]; const fileId = photo.file_id; const fileInfo = getTelegramFileInfo(fileId); if (fileInfo && fileInfo.file_path){ const fileUrl = `https://api.telegram.org/bot${BOT_TOKEN}/file/${fileInfo.file_path}`; // 下载图片并转为Blob,设置文件名 const imageBlob = UrlFetchApp.fetch(fileUrl).getBlob().setName("telegram_image.jpg"); // 回复用户 sendText(chatId, `Hi ${userName}, 收到你的图片啦!`); // 发送带图片附件的邮件 GmailApp.sendEmail( emailRecipient, emailSubject, emailBody, {attachments: [imageBlob]} ); } } // 处理音频消息 else if (data.message.audio){ const audio = data.message.audio; const fileId = audio.file_id; const fileInfo = getTelegramFileInfo(fileId); if (fileInfo && fileInfo.file_path){ const fileUrl = `https://api.telegram.org/bot${BOT_TOKEN}/file/${fileInfo.file_path}`; // 下载音频并转为Blob,保留原文件名(如果有) const audioBlob = UrlFetchApp.fetch(fileUrl).getBlob().setName(audio.file_name || "telegram_audio.mp3"); // 回复用户 sendText(chatId, `Hi ${userName}, 收到你的音频啦!`); // 发送带音频附件的邮件 GmailApp.sendEmail( emailRecipient, emailSubject, emailBody, {attachments: [audioBlob]} ); } } } // 封装获取Telegram文件信息的工具函数 function getTelegramFileInfo(fileId){ try { const response = UrlFetchApp.fetch(`https://api.telegram.org/bot${BOT_TOKEN}/getFile?file_id=${fileId}`); return JSON.parse(response.getContentText()); } catch (error) { console.log("获取文件信息失败:" + error.toString()); return null; } } // 原有的发送文本消息函数(假设你已实现) function sendText(id, text){ UrlFetchApp.fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${id}&text=${encodeURIComponent(text)}`); }
关键细节说明
BOT_TOKEN:一定要替换成你自己的机器人Token,否则无法调用Telegram API- Blob处理:下载文件后转为Blob对象,通过
setName设置文件名,确保邮件附件显示正确的名称和格式 - 邮件配置:
GmailApp.sendEmail的第四个参数是可选配置对象,attachments字段接受Blob数组,支持同时添加多个附件 - 错误处理:
getTelegramFileInfo函数里加了try-catch,避免因API调用失败导致整个机器人崩溃
需要注意的是,Telegram Bot API对文件大小有一定限制,超过限制的文件可能无法获取下载链接,你可以根据需求添加对应的大小判断逻辑。
内容的提问来源于stack exchange,提问作者babak
相关产品推荐
相关产品推荐

