diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | bot.py | 84 | ||||
-rw-r--r-- | requirements.txt | 17 |
3 files changed, 102 insertions, 0 deletions
@@ -1,3 +1,4 @@ | |||
1 | __pycache__/ | ||
1 | .idea/ | 2 | .idea/ |
2 | *.png | 3 | *.png |
3 | *.jpg | 4 | *.jpg |
@@ -0,0 +1,84 @@ | |||
1 | import logging | ||
2 | import os | ||
3 | |||
4 | from telegram import __version__ as TG_VER | ||
5 | |||
6 | try: | ||
7 | from telegram import __version_info__ | ||
8 | except ImportError: | ||
9 | __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | ||
10 | |||
11 | if __version_info__ < (20, 0, 0, "alpha", 1): | ||
12 | raise RuntimeError( | ||
13 | f"This example is not compatible with your current PTB version {TG_VER}. To view the " | ||
14 | f"{TG_VER} version of this example, " | ||
15 | f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" | ||
16 | |||
17 | ) | ||
18 | |||
19 | from telegram import ForceReply, Update, File | ||
20 | from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters | ||
21 | from telegram.constants import ParseMode | ||
22 | from PIL import Image, ImageFilter | ||
23 | from square import square_size_padding, drop_shadow | ||
24 | |||
25 | # Enable logging | ||
26 | |||
27 | logging.basicConfig( | ||
28 | format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO | ||
29 | ) | ||
30 | |||
31 | logger = logging.getLogger(__name__) | ||
32 | |||
33 | |||
34 | # Define a few command handlers. These usually take the two arguments update and context. | ||
35 | |||
36 | async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
37 | """Send a message when the command /start is issued.""" | ||
38 | user = update.effective_user | ||
39 | await update.message.reply_html( | ||
40 | rf"Hi {user.mention_html()}!", | ||
41 | ) | ||
42 | |||
43 | |||
44 | async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
45 | """Echo the user message.""" | ||
46 | filename = update.message.document.file_name | ||
47 | chat_id = update.message.chat_id | ||
48 | await context.bot.send_message(chat_id, "Processing `{}`".format(filename), | ||
49 | parse_mode=ParseMode.MARKDOWN_V2) | ||
50 | |||
51 | file = await update.message.effective_attachment.get_file() | ||
52 | img = await file.download_to_drive(filename) | ||
53 | |||
54 | try: | ||
55 | im = Image.open(img) | ||
56 | names = filename.split(".") | ||
57 | result = square_size_padding(im) | ||
58 | filename = "{}-result.{}".format(names[0], names[1]) | ||
59 | result.save(filename, quality=100) | ||
60 | |||
61 | await context.bot.send_document(chat_id=update.message.chat_id, document=filename) | ||
62 | |||
63 | except Exception as e: | ||
64 | await update.message.reply_markdown_v2(text="Error:\n```{}```".format(str(e))) | ||
65 | |||
66 | |||
67 | def main() -> None: | ||
68 | """Start the bot.""" | ||
69 | # Create the Application and pass it your bot's token. | ||
70 | tg_token = os.getenv("TG_TOKEN") | ||
71 | application = Application.builder().token(tg_token).build() | ||
72 | |||
73 | # on different commands - answer in Telegram | ||
74 | application.add_handler(CommandHandler("start", start)) | ||
75 | |||
76 | # on non command i.e message - echo the message on Telegram | ||
77 | application.add_handler(MessageHandler(filters.ATTACHMENT & ~filters.COMMAND, process)) | ||
78 | |||
79 | # Run the bot until the user presses Ctrl-C | ||
80 | application.run_polling() | ||
81 | |||
82 | |||
83 | if __name__ == "__main__": | ||
84 | main() | ||
diff --git a/requirements.txt b/requirements.txt index a6d4d60..ac7741f 100644 --- a/requirements.txt +++ b/requirements.txt | |||
@@ -1 +1,18 @@ | |||
1 | anyio==3.6.2 | ||
2 | APScheduler==3.6.3 | ||
3 | cachetools==4.2.2 | ||
4 | certifi==2022.9.24 | ||
5 | h11==0.14.0 | ||
6 | httpcore==0.16.1 | ||
7 | httpx==0.23.1 | ||
8 | idna==3.4 | ||
1 | Pillow==9.2.0 | 9 | Pillow==9.2.0 |
10 | python-telegram-bot==20.0a6 | ||
11 | pytz==2022.6 | ||
12 | pytz-deprecation-shim==0.1.0.post0 | ||
13 | rfc3986==1.5.0 | ||
14 | six==1.16.0 | ||
15 | sniffio==1.3.0 | ||
16 | tornado==6.1 | ||
17 | tzdata==2022.6 | ||
18 | tzlocal==4.2 | ||