From 531ea77d52596a24b5a97889e1110e41f2b225a3 Mon Sep 17 00:00:00 2001 From: Alexandr Karpov Date: Thu, 16 Mar 2023 13:46:37 +0300 Subject: [PATCH] added photo_handler --- bots/README.md | 2 ++ bots/photo_handler.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 bots/README.md create mode 100644 bots/photo_handler.py diff --git a/bots/README.md b/bots/README.md new file mode 100644 index 0000000..45f2105 --- /dev/null +++ b/bots/README.md @@ -0,0 +1,2 @@ +# Telegram bot scrips +my small collection of bots(mainly as a mvp) diff --git a/bots/photo_handler.py b/bots/photo_handler.py new file mode 100644 index 0000000..b13b1ff --- /dev/null +++ b/bots/photo_handler.py @@ -0,0 +1,40 @@ +import time + +import telebot + +bot = telebot.TeleBot("132:TOKEN") + + +@bot.message_handler(content_types=["photo"]) +def send_text(message): + try: + file_name = message.document.file_name + file_id = message.document.file_name + file_id_info = bot.get_file(message.document.file_id) + downloaded_file = bot.download_file(file_id_info.file_path) + src = file_name + print(src) + with open(src, "wb") as new_file: + new_file.write(downloaded_file) + bot.send_message( + message.chat.id, + "[*] File added:\nFile name - {}".format( + str(file_name), + ), + ) + except Exception as ex: + bot.send_message(message.chat.id, "[!] error - {}".format(str(ex))) + + +while True: + try: + print("[*] bot starting..") + print("[*] bot started!") + bot.polling(none_stop=True, interval=2) + break + except Exception as ex: + print("[*] error - {}".format(str(ex))) + print("[*] rebooting..") + bot.stop_polling() + time.sleep(15) + print("[*] restarted!")