diff options
-rw-r--r-- | bot.py | 37 |
1 files changed, 23 insertions, 14 deletions
@@ -10,8 +10,6 @@ from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandl | |||
10 | 10 | ||
11 | from square import square_size_padding | 11 | from square import square_size_padding |
12 | 12 | ||
13 | # Enable logging | ||
14 | |||
15 | logging.basicConfig( | 13 | logging.basicConfig( |
16 | format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO | 14 | format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO |
17 | ) | 15 | ) |
@@ -25,23 +23,34 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |||
25 | 23 | ||
26 | async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 24 | async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
27 | chat_id = update.message.chat_id | 25 | chat_id = update.message.chat_id |
28 | 26 | save_format = "JPEG" | |
29 | filename = update.message.document.file_name | 27 | |
30 | names = filename.split(".") | 28 | if update.message.document is not None: |
31 | 29 | names = update.message.document.file_name.split(".") | |
32 | if str.upper(names[1]) in ("JPG", "JPEG"): | 30 | file_ext = names[1] |
33 | save_format = "JPEG" | 31 | filename = names[0] |
34 | elif str.upper(names[1]) in ("PNG",): | 32 | |
35 | save_format = str.upper(names[1]) | 33 | if str.upper(file_ext) in ("JPG", "JPEG"): |
34 | save_format = "JPEG" | ||
35 | elif str.upper(file_ext) in ("PNG",): | ||
36 | save_format = str.upper(file_ext) | ||
37 | else: | ||
38 | await context.bot.send_message(chat_id, "Image extension `{}` not supported".format(file_ext), | ||
39 | parse_mode=ParseMode.MARKDOWN_V2) | ||
40 | return | ||
41 | |||
42 | file = await update.message.effective_attachment.get_file() | ||
43 | |||
44 | elif update.message.photo is not None: | ||
45 | filename = update.message.photo[-1].file_unique_id | ||
46 | file_ext = "JPG" | ||
47 | file = await update.message.effective_attachment[-1].get_file() | ||
36 | else: | 48 | else: |
37 | await context.bot.send_message(chat_id, "Image extension `{}` not supported".format(names[1]), | ||
38 | parse_mode=ParseMode.MARKDOWN_V2) | ||
39 | return | 49 | return |
40 | 50 | ||
41 | await context.bot.send_message(chat_id, "Processing `{}`".format(filename), | 51 | await context.bot.send_message(chat_id, "Processing `{}`".format(filename), |
42 | parse_mode=ParseMode.MARKDOWN_V2) | 52 | parse_mode=ParseMode.MARKDOWN_V2) |
43 | 53 | ||
44 | file = await update.message.effective_attachment.get_file() | ||
45 | img = io.BytesIO() | 54 | img = io.BytesIO() |
46 | await file.download_to_memory(img) | 55 | await file.download_to_memory(img) |
47 | 56 | ||
@@ -55,7 +64,7 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |||
55 | await update.message.reply_markdown_v2(text="Sending processed result") | 64 | await update.message.reply_markdown_v2(text="Sending processed result") |
56 | 65 | ||
57 | await context.bot.send_document(chat_id=update.message.chat_id, | 66 | await context.bot.send_document(chat_id=update.message.chat_id, |
58 | filename="{}-result.{}".format(names[0], names[1]), | 67 | filename="{}-result.{}".format(filename, file_ext), |
59 | document=output.getvalue()) | 68 | document=output.getvalue()) |
60 | 69 | ||
61 | except Exception as e: | 70 | except Exception as e: |