diff options
author | Mole Shang <[email protected]> | 2023-01-25 12:49:45 +0800 |
---|---|---|
committer | Mole Shang <[email protected]> | 2023-01-25 12:49:45 +0800 |
commit | 71624ff95ee090a19056ee40ec37d4535d92d51d (patch) | |
tree | cfcdb91347596d778375ad626f729f732db6cd9e | |
parent | d741c552a7dbe18515955bfc2c173367a03fdbc7 (diff) | |
download | telegram-mail-bot-71624ff95ee090a19056ee40ec37d4535d92d51d.tar.gz telegram-mail-bot-71624ff95ee090a19056ee40ec37d4535d92d51d.tar.bz2 telegram-mail-bot-71624ff95ee090a19056ee40ec37d4535d92d51d.zip |
refactor: migrate to python-telegram-bot-20 apis...
... and generate requirements.txt for deps.
-rw-r--r-- | Dockerfile | 5 | ||||
-rw-r--r-- | bot.py | 60 | ||||
-rw-r--r-- | requirements.txt | 3 |
3 files changed, 34 insertions, 34 deletions
@@ -1,11 +1,12 @@ FROM python:3.9 -RUN pip install --no-cache-dir pytz python-telegram-bot==13.1 pyzmail36 imapclient +WORKDIR /opt/workdir/telegram-mail-bot COPY utils /opt/workdir/telegram-mail-bot/utils COPY bot.py /opt/workdir/telegram-mail-bot/ +COPY requirements.txt /opt/workdir/telegram-mail-bot/ -WORKDIR /opt/workdir/telegram-mail-bot +RUN pip install --no-cache-dir -r requirements.txt ENV OWNER_CHAT_ID= ENV TELEGRAM_TOKEN= @@ -1,9 +1,9 @@ import logging import os import sys -from telegram import ParseMode, Update -from telegram.constants import MAX_MESSAGE_LENGTH -from telegram.ext import Updater, CommandHandler, CallbackContext +from telegram import Update, Bot +from telegram.constants import MessageLimit, ParseMode +from telegram.ext import ApplicationBuilder, CommandHandler, CallbackContext from utils.client import EmailClient @@ -25,11 +25,11 @@ def is_owner(update: Update) -> bool: def handle_large_text(text): while text: - if len(text) < MAX_MESSAGE_LENGTH: + if len(text) < MessageLimit.MAX_TEXT_LENGTH: yield text text = None else: - out = text[:MAX_MESSAGE_LENGTH] + out = text[: MessageLimit.MAX_TEXT_LENGTH] yield out text = text.lstrip(out) @@ -39,15 +39,15 @@ def error(update: Update, context: CallbackContext) -> None: logger.warning('Update "%s" caused error "%s"', update, context.error) -def start_callback(update: Update, context: CallbackContext) -> None: +async 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) + await update.message.reply_text(msg) -def _help(update: Update, context: CallbackContext) -> None: +async def _help(update: Update, context: CallbackContext) -> None: if not is_owner(update): return """Send a message when the command /help is issued.""" @@ -60,31 +60,31 @@ def _help(update: Update, context: CallbackContext) -> None: # "/setting [email protected] password\n" \ # "/inbox\n" \ # "/get mail_index" - context.bot.send_message( + await context.bot.send_message( update.message.chat_id, # parse_mode=ParseMode.MARKDOWN, text=help_str, ) -def setting_email(update: Update, context: CallbackContext) -> None: +async def setting_email(update: Update, context: CallbackContext) -> None: if not is_owner(update): return global email_addr, email_passwd, inbox_num email_addr = context.args[0] email_passwd = context.args[1] logger.info("received setting_email command.") - update.message.reply_text("Configure email success!") + await update.message.reply_text("Configure email success!") with EmailClient(email_addr, email_passwd) as client: inbox_num = client.get_mails_count() context.job_queue.run_repeating( - periodic_task, interval=60, context=update.message.chat_id + periodic_task, interval=60, chat_id=update.message.chat_id ) # chat_data['job'] = job logger.info("periodic task scheduled.") -def periodic_task(context: CallbackContext) -> None: +async def periodic_task(context: CallbackContext) -> None: global inbox_num logger.info("entering periodic task.") with EmailClient(email_addr, email_passwd) as client: @@ -94,11 +94,11 @@ def periodic_task(context: CallbackContext) -> None: mail = client.get_mail_by_index(i + 1) content = mail.__repr__() for text in handle_large_text(content): - context.bot.send_message(context.job.context, text=text) + await context.bot.send_message(context.job.context, text=text) inbox_num = new_inbox_num -def inbox(update: Update, context: CallbackContext) -> None: +async def inbox(update: Update, context: CallbackContext) -> None: if not is_owner(update): return logger.info("received inbox command.") @@ -111,17 +111,17 @@ def inbox(update: Update, context: CallbackContext) -> None: " time you checked." % (new_num, new_num - inbox_num) ) inbox_num = new_num - context.bot.send_message( + await context.bot.send_message( update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text=reply_text ) -def get_email(update: Update, context: CallbackContext) -> None: +async def get_email(update: Update, context: CallbackContext) -> None: if not is_owner(update): return index = context.args[0] if not index: - context.bot.send_message( + await context.bot.send_message( update.message.chat_id, text="$index should be a positive number!" ) logger.info("received get command.") @@ -129,38 +129,34 @@ def get_email(update: Update, context: CallbackContext) -> None: mail = client.get_mail_by_index(index) content = mail.__repr__() for text in handle_large_text(content): - context.bot.send_message(update.message.chat_id, text=text) + await 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, use_context=True) + bot = Bot(token=bot_token) print(bot_token) - # Get the dispatcher to register handlers - dp = updater.dispatcher + application = ApplicationBuilder().bot(bot).build() # simple start function - dp.add_handler(CommandHandler("start", start_callback)) + application.add_handler(CommandHandler("start", start_callback)) - dp.add_handler(CommandHandler("help", _help)) + application.add_handler(CommandHandler("help", _help)) # # Add command handler to set email address and account. - dp.add_handler(CommandHandler("setting", setting_email)) + application.add_handler(CommandHandler("setting", setting_email)) - dp.add_handler(CommandHandler("inbox", inbox)) + application.add_handler(CommandHandler("inbox", inbox)) - dp.add_handler(CommandHandler("get", get_email)) + application.add_handler(CommandHandler("get", get_email)) - dp.add_error_handler(error) - - # Start the Bot - updater.start_polling() + application.add_error_handler(error) # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. - updater.idle() + application.run_polling() if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..08243c6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +IMAPClient==2.3.1 +python_telegram_bot==20.0 +pyzmail36==1.0.5 |