Last active
January 13, 2023 10:46
-
-
Save ivanfgm/dce0495f0ee00b064616958e450d05bd to your computer and use it in GitHub Desktop.
Small script to send received emails to a telegram bot, easy piece thanks to: node-telegram-bot-api and mail-listener2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeBot = require('node-telegram-bot-api'); | |
const mailListener = require("mail-listener2"); | |
const config = { | |
// First get your telegram bot | |
telegramToken: 'Obtain your token talking to BotFather on telegram', | |
// Send a message to the bot and paste here your chat id (the response) | |
// TODO: login to avoid this step and allow multiple users | |
chatID = 'Chat ID', | |
// Gmail or Gsuite settings | |
email: "gmail or gsuite email here", | |
email_pass: "email password here" | |
}; | |
const bot = new makeBot(config.telegramToken, { polling: true }); | |
bot.on('message', msg => bot.sendMessage(chatId, msg.chat.id)); | |
var mailInbox = new mailListener({ | |
username: config.email, | |
password: config.email_pass, | |
host: "imap.gmail.com", | |
port: 993, | |
tls: true, | |
markSeen: true, | |
fetchUnreadOnStart: true, | |
}); | |
mailInbox.start(); // start listening | |
mailInbox.on("server:connected", function () { | |
console.log("imapConnected"); | |
}); | |
mailInbox.on("server:disconnected", function () { | |
console.log("imapDisconnected"); | |
throw new Error('Restart app due to IMAP disconnection'); | |
}); | |
mailInbox.on("error", function (err){ | |
console.log(err); | |
}); | |
mailInbox.on("mail", function (mail, seqno, attributes){ | |
bot.sendMessage(config.chatID, ` | |
<b>${mail.subject}</b> | |
${mail.text} | |
`, { | |
parse_mode: 'HTML' | |
}); | |
}); |
Does this still work? Providing a password from your gmail account is the only way for this work properly? Can you have like a specific app password for this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependencies install:
yarn init && yarn add node-telegram-bot-api mail-listener2