Skip to content

Commit

Permalink
Increased file size limits for custom telegram-bot-api server (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
thankjura authored Dec 14, 2024
1 parent b410655 commit b5e43c8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions bot/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def __init__(self, config: configparser.ConfigParser):
self.ssl_verify: bool = self._get_boolean("ssl_verify", default=True)
self.port: int = self._get_int("port", default=80)
self.api_url: str = self._get_str("api_url", default="https://api.telegram.org/bot")
self.max_upload_file_size: int = 50 if self.api_url == "https://api.telegram.org/bot" else 2000
self.socks_proxy: str = self._get_str("socks_proxy", default="")
self.light_device_name: str = self._get_str("light_device", default="")
self.poweroff_device_name: str = self._get_str("power_device", default="")
Expand Down
5 changes: 3 additions & 2 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ async def get_video(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
loop_loc = asyncio.get_running_loop()
(video_bio, thumb_bio, width, height) = await loop_loc.run_in_executor(executors_pool, cameraWrap.take_video)
await info_reply.edit_text(text="Uploading video")
if video_bio.getbuffer().nbytes > 52428800:
await info_reply.edit_text(text="Telegram has a 50mb restriction...")
max_upload_file_size: int = configWrap.bot_config.max_upload_file_size
if video_bio.getbuffer().nbytes > max_upload_file_size * 1024 * 1024:
await info_reply.edit_text(text=f"Telegram has a {max_upload_file_size}mb restriction...")
else:
await update.effective_message.reply_video(
video=video_bio,
Expand Down
9 changes: 5 additions & 4 deletions bot/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
self._interval: int = config.notifications.interval
self._notify_groups: List[int] = config.notifications.notify_groups
self._group_only: bool = config.notifications.group_only
self._max_upload_file_size: int = config.bot_config.max_upload_file_size

self._progress_update_message = config.telegram_ui.progress_update_message
self._silent_progress: bool = config.telegram_ui.silent_progress
Expand Down Expand Up @@ -554,8 +555,8 @@ async def _send_video(self, paths: List[str], message: str) -> None:
with open(path_obj, "rb") as fh:
bio.write(fh.read())
bio.seek(0)
if bio.getbuffer().nbytes > 52428800:
await self._bot.send_message(self._chat_id, text=f"Telegram bots have a 50mb filesize restriction, video couldn't be uploaded: `{path}`")
if bio.getbuffer().nbytes > self._max_upload_file_size * 1024 * 1024:
await self._bot.send_message(self._chat_id, text=f"Telegram bots have a {self._max_upload_file_size}mb filesize restriction, video couldn't be uploaded: `{path}`")
else:
if not photos_list:
photos_list.append(InputMediaVideo(bio, filename=bio.name, caption=message))
Expand Down Expand Up @@ -599,8 +600,8 @@ async def _send_document(self, paths: List[str], message: str) -> None:
with open(path_obj, "rb") as fh:
bio.write(fh.read())
bio.seek(0)
if bio.getbuffer().nbytes > 52428800:
await self._bot.send_message(self._chat_id, text=f"Telegram bots have a 50mb filesize restriction, document couldn't be uploaded: `{path}`")
if bio.getbuffer().nbytes > self._max_upload_file_size * 1024 * 1024:
await self._bot.send_message(self._chat_id, text=f"Telegram bots have a {self._max_upload_file_size}mb filesize restriction, document couldn't be uploaded: `{path}`")
else:
if not photos_list:
photos_list.append(InputMediaDocument(bio, filename=bio.name, caption=message))
Expand Down
7 changes: 4 additions & 3 deletions bot/timelapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(
self._limit_fps: bool = config.timelapse.limit_fps
self._min_lapse_duration: int = config.timelapse.min_lapse_duration
self._max_lapse_duration: int = config.timelapse.max_lapse_duration
self._max_upload_file_size: int = config.bot_config.max_upload_file_size
self._last_frame_duration: int = config.timelapse.last_frame_duration

self._after_lapse_gcode: str = config.timelapse.after_lapse_gcode
Expand Down Expand Up @@ -251,8 +252,8 @@ async def upload_timelapse(self, lapse_filename: str, info_mess, gcode_name_out:
if self._send_finished_lapse:
await info_mess.edit_text(text="Uploading time-lapse")

if len(video_bytes) > 52428800:
await info_mess.edit_text(text=f"Telegram bots have a 50mb filesize restriction, please retrieve the timelapse from the configured folder\n{video_path}")
if len(video_bytes) > self._max_upload_file_size * 1024 * 1024:
await info_mess.edit_text(text=f"Telegram bots have a {self._max_upload_file_size}mb filesize restriction, please retrieve the timelapse from the configured folder\n{video_path}")
else:
lapse_caption = f"time-lapse of {gcode_name}"
if self._camera.lapse_missed_frames > 0:
Expand All @@ -264,7 +265,7 @@ async def upload_timelapse(self, lapse_filename: str, info_mess, gcode_name_out:
width=width,
height=height,
caption=lapse_caption,
write_timeout=120,
write_timeout=600,
disable_notification=self._silent_progress,
)
try:
Expand Down

0 comments on commit b5e43c8

Please sign in to comment.