|
| 1 | +/* based on https://github.com/edevil/email_worker_parser/blob/0c3938d537ce134bc1a8a9f3b301badcb1c5ead0/src/index.js |
| 2 | +notification to Telegram by @dishapatel010 |
| 3 | +*/ |
| 4 | + |
| 5 | +const PostalMime = require('postal-mime'); |
| 6 | +async function streamToArrayBuffer(stream, streamSize) { |
| 7 | + let result = new Uint8Array(streamSize); |
| 8 | + let bytesRead = 0; |
| 9 | + const reader = stream.getReader(); |
| 10 | + while (true) { |
| 11 | + const { done, value } = await reader.read(); |
| 12 | + if (done) { |
| 13 | + break; |
| 14 | + } |
| 15 | + result.set(value, bytesRead); |
| 16 | + bytesRead += value.length; |
| 17 | + } |
| 18 | + return result; |
| 19 | +} |
| 20 | +export default { |
| 21 | + async email(message, env, ctx) { |
| 22 | + // Define the Telegram API URL and chat ID |
| 23 | + const tgToken = env.TOKEN //env VAR |
| 24 | + const chatId = env.CHATID //env VAR |
| 25 | + const fMailid = env.FMAILID //env VAR |
| 26 | + const telegramUrl = `https://api.telegram.org/bot${tgToken}/sendMessage`; |
| 27 | + try { |
| 28 | + // Extract the email data |
| 29 | + const from = message.headers.get("from"); |
| 30 | + const to = message.headers.get("to"); |
| 31 | + const subject = message.headers.get("subject") || "[No Subject]"; |
| 32 | + const date = message.headers.get("date"); |
| 33 | + // Parse the raw email message using PostalMime |
| 34 | + const rawEmail = await streamToArrayBuffer( |
| 35 | + message.raw, |
| 36 | + message.rawSize |
| 37 | + ); |
| 38 | + const parser = new PostalMime.default(); |
| 39 | + const parsedEmail = await parser.parse(rawEmail); |
| 40 | + // Construct the notification message |
| 41 | + const attachmentCount = parsedEmail.attachments.length; |
| 42 | + let notificationMessage = `📧 New email received\n\nFrom: ${from}\nTo: ${to}\nSubject: ${subject}\nDate: ${date}\n\n`; |
| 43 | + if (parsedEmail.text) { |
| 44 | + notificationMessage += `Text version: ${parsedEmail.text}\n`; |
| 45 | + } |
| 46 | + if (attachmentCount == 0) { |
| 47 | + notificationMessage += "📎 No attachments"; |
| 48 | + } else if (attachmentCount == 1) { |
| 49 | + notificationMessage += "📎 1 attachment"; |
| 50 | + } else { |
| 51 | + notificationMessage += `📎 ${attachmentCount} attachments`; |
| 52 | + } |
| 53 | + // Paste the raw message and html version into Spacebin |
| 54 | + const spacebinUrl = "https://spaceb.in/api/v1/documents/"; |
| 55 | + const spacebinData = { |
| 56 | + title: `Email - ${subject}` || "none", |
| 57 | + from: from, |
| 58 | + to: to, |
| 59 | + date: date, |
| 60 | + text: parsedEmail.text, |
| 61 | + html: parsedEmail.html, |
| 62 | + }; |
| 63 | + const mypaste = `${spacebinData.title}\nFrom: ${spacebinData.from}\nTo: ${spacebinData.to}\nDate: ${spacebinData.date}\nText Version:\n${spacebinData.text}\nHtml Version:\n${spacebinData.html}`; |
| 64 | + const spacebinResponse = await fetch(spacebinUrl, { |
| 65 | + method: "POST", |
| 66 | + headers: { "Content-Type": "application/json" }, |
| 67 | + body: JSON.stringify({ |
| 68 | + content: mypaste, |
| 69 | + extension: "txt", |
| 70 | + }), |
| 71 | + }); |
| 72 | + const spacebinJson = await spacebinResponse.json(); |
| 73 | + const sbinUrl = `https://spaceb.in/${spacebinJson.payload.id}`; |
| 74 | + // Add the Spacebin URL to the notification message as an inline button |
| 75 | + const buttonText = "View Spacebin"; |
| 76 | + const button = { text: buttonText, url: sbinUrl }; |
| 77 | + const replyMarkup = { inline_keyboard: [[button]] }; |
| 78 | + // Send the notification to Telegram with the inline button |
| 79 | + await fetch(telegramUrl, { |
| 80 | + method: "POST", |
| 81 | + headers: { "Content-Type": "application/json" }, |
| 82 | + body: JSON.stringify({ |
| 83 | + chat_id: chatId, |
| 84 | + text: notificationMessage, |
| 85 | + reply_markup: replyMarkup, |
| 86 | + }), |
| 87 | + }); |
| 88 | + // Forward the email to the specified inbox |
| 89 | + await message.forward(fMailid); |
| 90 | + } catch (error) { |
| 91 | + console.error(error); |
| 92 | + // Handle the error here |
| 93 | + } |
| 94 | + }, |
| 95 | +}; |
0 commit comments