aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2022-11-24 16:29:14 -0800
committerclarkzjw <[email protected]>2022-11-24 16:29:14 -0800
commit3b869974d0513d6d0a00e2bc8dd5d13d872e05ad (patch)
tree724da43df10ef4e6661513e02dbf3f4627f24055
parent727240c777b212e6191ff5acbfd4b994d37ca2b2 (diff)
downloadSquare-3b869974d0513d6d0a00e2bc8dd5d13d872e05ad.tar.gz
bot: add telegram bot to produce square image
-rw-r--r--.gitignore1
-rw-r--r--bot.py84
-rw-r--r--requirements.txt17
3 files changed, 102 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index be2fb47..40992df 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
1__pycache__/
1.idea/ 2.idea/
2*.png 3*.png
3*.jpg 4*.jpg
diff --git a/bot.py b/bot.py
new file mode 100644
index 0000000..2d48783
--- /dev/null
+++ b/bot.py
@@ -0,0 +1,84 @@
1import logging
2import os
3
4from telegram import __version__ as TG_VER
5
6try:
7 from telegram import __version_info__
8except ImportError:
9 __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
10
11if __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
19from telegram import ForceReply, Update, File
20from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
21from telegram.constants import ParseMode
22from PIL import Image, ImageFilter
23from square import square_size_padding, drop_shadow
24
25# Enable logging
26
27logging.basicConfig(
28 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
29)
30
31logger = logging.getLogger(__name__)
32
33
34# Define a few command handlers. These usually take the two arguments update and context.
35
36async 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
44async 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
67def 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
83if __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 @@
1anyio==3.6.2
2APScheduler==3.6.3
3cachetools==4.2.2
4certifi==2022.9.24
5h11==0.14.0
6httpcore==0.16.1
7httpx==0.23.1
8idna==3.4
1Pillow==9.2.0 9Pillow==9.2.0
10python-telegram-bot==20.0a6
11pytz==2022.6
12pytz-deprecation-shim==0.1.0.post0
13rfc3986==1.5.0
14six==1.16.0
15sniffio==1.3.0
16tornado==6.1
17tzdata==2022.6
18tzlocal==4.2
Powered by cgit v1.2.3 (git 2.41.0)