diff options
author | hyfc <[email protected]> | 2021-01-25 10:49:16 +0800 |
---|---|---|
committer | GitHub <[email protected]> | 2021-01-25 10:49:16 +0800 |
commit | 578759eed8ecdde831f0982784eec4b500555f37 (patch) | |
tree | a6e4707dcb6dc1ebb658ca52a22741fdbddd8fe7 | |
parent | 2fd8c96edd91a37fc22def123a717b89adba0725 (diff) | |
parent | 477b4cdaf60bae65a809697465e89197e7c877ea (diff) | |
download | telegram-mail-bot-578759eed8ecdde831f0982784eec4b500555f37.tar.gz telegram-mail-bot-578759eed8ecdde831f0982784eec4b500555f37.tar.bz2 telegram-mail-bot-578759eed8ecdde831f0982784eec4b500555f37.zip |
Merge pull request #5 from bit-rich/master
New Telegram APIs and checking current owner.
-rw-r--r-- | Dockerfile | 12 | ||||
-rw-r--r-- | Pipfile | 2 | ||||
-rw-r--r-- | bot.py | 81 |
3 files changed, 65 insertions, 30 deletions
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2aca3f3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +from python:3.9 + +RUN pip install --no-cache-dir pytz python-telegram-bot pyzmail36 + +COPY utils /opt/workdir/telegram-mail-bot/utils +COPY bot.py /opt/workdir/telegram-mail-bot/ + +WORKDIR /opt/workdir/telegram-mail-bot + +ENV TELEGRAM_TOKEN= + +CMD ["/bin/sh", "-c", "/usr/local/bin/python bot.py" ]
\ No newline at end of file @@ -12,4 +12,4 @@ pyzmail36 = "*" pylint = "*" [requires] -python_version = "3.6" +python_version = "3.9" @@ -1,18 +1,24 @@ import logging import os -from telegram import ParseMode +import sys +from telegram import ParseMode, Update from telegram.constants import MAX_MESSAGE_LENGTH -from telegram.ext import (Updater, CommandHandler) +from telegram.ext import (Updater, CommandHandler, CallbackContext) from utils.client import EmailClient -logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s:%(lineno)d' - ' - %(message)s', filename='/var/log/mailbot.log', +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s:%(lineno)d - %(message)s', + stream=sys.stdout, level=logging.INFO) logger = logging.getLogger(__name__) bot_token = os.environ['TELEGRAM_TOKEN'] +owner_chat_id = int(os.environ['OWNER_CHAT_ID']) + +def is_owner(update: Update) -> bool: + return update.message.chat_id == owner_chat_id + def handle_large_text(text): while text: if len(text) < MAX_MESSAGE_LENGTH: @@ -23,37 +29,50 @@ def handle_large_text(text): yield out text = text.lstrip(out) -def error(bot, update, _error): +def error(update: Update, context: CallbackContext) -> None: """Log Errors caused by Updates.""" - logger.warning('Update "%s" caused error "%s"', update, _error) + logger.warning('Update "%s" caused error "%s"', update, context.error) -def start_callback(bot, update): +def start_callback(update: Update, context: CallbackContext) -> None: + if not is_owner(update): + return msg = "Use /help to get help" + # print(update) update.message.reply_text(msg) -def _help(bot, update): +def _help(update: Update, context: CallbackContext) -> None: + if not is_owner(update): + return """Send a message when the command /help is issued.""" - help_str = "*Mailbox Setting*: \n" \ - "/setting [email protected] yourpassword" - bot.send_message(update.message.chat_id, - parse_mode=ParseMode.MARKDOWN, + 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" + context.bot.send_message(update.message.chat_id, + # parse_mode=ParseMode.MARKDOWN, text=help_str) -def setting_email(bot, update, args, job_queue, chat_data): +def setting_email(update: Update, context: CallbackContext) -> None: + if not is_owner(update): + return global email_addr, email_passwd, inbox_num - chat_id = update.message.chat_id - email_addr = args[0] - email_passwd = args[1] + email_addr = context.args[0] + email_passwd = context.args[1] logger.info("received setting_email command.") update.message.reply_text("Configure email success!") with EmailClient(email_addr, email_passwd) as client: inbox_num = client.get_mails_count() - job = job_queue.run_repeating(periodic_task, 120, context=chat_id) - chat_data['job'] = job + context.job_queue.run_repeating(periodic_task, interval=60, context=update.message.chat_id) + # chat_data['job'] = job logger.info("periodic task scheduled.") -def periodic_task(bot, job): +def periodic_task(context: CallbackContext) -> None: global inbox_num logger.info("entering periodic task.") with EmailClient(email_addr, email_passwd) as client: @@ -62,11 +81,13 @@ def periodic_task(bot, job): mail = client.get_mail_by_index(new_inbox_num) content = mail.__repr__() for text in handle_large_text(content): - bot.send_message(job.context, + context.bot.send_message(context.job.context, text=text) inbox_num = new_inbox_num -def inbox(bot, update): +def inbox(update: Update, context: CallbackContext) -> None: + if not is_owner(update): + return logger.info("received inbox command.") with EmailClient(email_addr, email_passwd) as client: global inbox_num @@ -76,23 +97,26 @@ def inbox(bot, update): " time you checked." % \ (new_num, new_num - inbox_num) inbox_num = new_num - bot.send_message(update.message.chat_id, + context.bot.send_message(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text=reply_text) -def get_email(bot, update, args): - index = args[0] +def get_email(update: Update, context: CallbackContext) -> None: + if not is_owner(update): + return + index = context.args[0] logger.info("received get command.") with EmailClient(email_addr, email_passwd) as client: mail = client.get_mail_by_index(index) content = mail.__repr__() for text in handle_large_text(content): - bot.send_message(update.message.chat_id, + context.bot.send_message(update.message.chat_id, text=text) def main(): # Create the EventHandler and pass it your bot's token. - updater = Updater(token=bot_token) + updater = Updater(token=bot_token, use_context=True) + print(bot_token) # Get the dispatcher to register handlers dp = updater.dispatcher @@ -103,12 +127,11 @@ def main(): dp.add_handler(CommandHandler("help", _help)) # # Add command handler to set email address and account. - dp.add_handler(CommandHandler("setting", setting_email, pass_args=True, - pass_job_queue=True, pass_chat_data=True)) + dp.add_handler(CommandHandler("setting", setting_email)) dp.add_handler(CommandHandler("inbox", inbox)) - dp.add_handler(CommandHandler("get", get_email, pass_args=True)) + dp.add_handler(CommandHandler("get", get_email)) dp.add_error_handler(error) |