diff options
author | Mole Shang <[email protected]> | 2023-01-25 16:40:48 +0800 |
---|---|---|
committer | Mole Shang <[email protected]> | 2023-01-25 16:40:48 +0800 |
commit | 12b38b59e73f45c542d87bd2acf553f837e20179 (patch) | |
tree | 586632713db2e0b484b467add7f2ffce47d344c5 | |
parent | b6d4781af03497c19442cc2081790793bbe25e87 (diff) | |
download | telegram-mail-bot-12b38b59e73f45c542d87bd2acf553f837e20179.tar.gz telegram-mail-bot-12b38b59e73f45c542d87bd2acf553f837e20179.tar.bz2 telegram-mail-bot-12b38b59e73f45c542d87bd2acf553f837e20179.zip |
feat: support showing mails list
-rw-r--r-- | bot.py | 46 | ||||
-rw-r--r-- | utils/client.py | 10 |
2 files changed, 44 insertions, 12 deletions
@@ -51,18 +51,15 @@ async def _help(update: Update, context: CallbackContext) -> None: if not is_owner(update): return """Send a message when the command /help is issued.""" - help_str = """邮箱设置: -/setting [email protected] password -/inbox -/get mail_index -/help get help""" - # help_str = "*Mailbox Setting*:\n" \ - # "/setting [email protected] password\n" \ - # "/inbox\n" \ - # "/get mail_index" + help_str = """*Mailbox Settings*: +/setting [email protected] password Login (needed) +/inbox Get mails status in INBOX +/get $ndex Get the $index mail +/help get help +/list List all mails in INBOX""" await context.bot.send_message( update.message.chat_id, - # parse_mode=ParseMode.MARKDOWN, + parse_mode=ParseMode.MARKDOWN, text=help_str, ) @@ -113,7 +110,9 @@ async def inbox(update: Update, context: CallbackContext) -> None: ) inbox_num = new_num await context.bot.send_message( - update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text=reply_text + update.message.chat_id, + parse_mode=ParseMode.MARKDOWN, + text=reply_text, ) except NameError as e: await context.bot.send_message( @@ -148,6 +147,29 @@ async def get_email(update: Update, context: CallbackContext) -> None: logger.warning(e) +async def list_email(update: Update, context: CallbackContext) -> None: + if not is_owner(update): + return + try: + with EmailClient(email_addr, email_passwd) as client: + mails = client.get_listed_mails() + text = "" + for i in mails: + text += f"*{mails.index(i)}*: Subject: {i[0]};\nSender: {i[1]};\nDate: {i[2]}\n\n" + await context.bot.send_message( + update.message.chat_id, + parse_mode=ParseMode.MARKDOWN, + text=text, + ) + except NameError as e: + await context.bot.send_message( + update.message.chat_id, + parse_mode=ParseMode.MARKDOWN, + text="Email unset, please set valid email by the '/setting' command.", + ) + logger.warning(e) + + def main(): # Create the EventHandler and pass it your bot's token. bot = Bot(token=bot_token) @@ -167,6 +189,8 @@ def main(): application.add_handler(CommandHandler("get", get_email)) + application.add_handler(CommandHandler("list", list_email)) + application.add_error_handler(error) # Run the bot until you press Ctrl-C or the process receives SIGINT, diff --git a/utils/client.py b/utils/client.py index e0bd2f9..676a859 100644 --- a/utils/client.py +++ b/utils/client.py @@ -34,9 +34,17 @@ class EmailClient(object): def get_mail_by_index(self, index): list = self.get_mails_list() - msg_data = list[int(index)-1][1] + msg_data = list[int(index) - 1][1] return Email(msg_data[b"RFC822"]) + def get_listed_mails(self): + list = self.get_mails_list() + listed_mails = [] + for i in range(0, self.get_mails_count()): + email = Email(list[i][1][b"RFC822"]) + listed_mails.append([email.subject, email.sender, email.date]) + return listed_mails + def __enter__(self): return self |