aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py84
1 files changed, 84 insertions, 0 deletions
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()
Powered by cgit v1.2.3 (git 2.41.0)