diff --git a/bot/klippy.py b/bot/klippy.py index 50693aa..05d0bc6 100644 --- a/bot/klippy.py +++ b/bot/klippy.py @@ -603,20 +603,24 @@ def stop_all(self) -> None: self._reset_file_info() async def get_versions_info(self, bot_only: bool = False) -> str: - response = await self.make_request("GET", "/machine/update/status?refresh=false") - if not response.is_success: - return "" - version_info = orjson.loads(response.text)["result"]["version_info"] version_message = "" - for comp, inf in version_info.items(): - if comp == "system": - continue - if bot_only and comp != "moonraker-telegram-bot": - continue - if "full_version_string" in inf: - version_message += f"{comp}: {inf['full_version_string']}\n" - else: - version_message += f"{comp}: {inf['version']}\n" + try: + response = await self.make_request("GET", "/machine/update/status?refresh=false") + if not response.is_success: + return "" + version_info = orjson.loads(response.text)["result"]["version_info"] + + for comp, inf in version_info.items(): + if comp == "system": + continue + if bot_only and comp != "moonraker-telegram-bot": + continue + if "full_version_string" in inf: + version_message += f"{comp}: {inf['full_version_string']}\n" + else: + version_message += f"{comp}: {inf['version']}\n" + except Exception as e: + logger.error(e) if version_message: version_message += "\n" return version_message diff --git a/bot/notifications.py b/bot/notifications.py index 9a029d1..05214cd 100644 --- a/bot/notifications.py +++ b/bot/notifications.py @@ -174,61 +174,70 @@ async def _send_message(self, message: str, silent: bool, group_only: bool = Fal continue self._groups_status_mesages[group] = sent_message - async def _notify(self, message: str, silent: bool, group_only: bool = False, manual: bool = False) -> None: - if not self._cam_wrap.enabled: - await self._send_message(message, silent, manual) - else: - loop = asyncio.get_running_loop() - with await loop.run_in_executor(self._executors_pool, self._cam_wrap.take_photo) as photo: - if not group_only: - await self._bot.send_chat_action(chat_id=self._chat_id, action=ChatAction.UPLOAD_PHOTO) - if self._status_message and not manual: - if self._bzz_mess_id != 0: - try: - await self._bot.delete_message(self._chat_id, self._bzz_mess_id) - except BadRequest as badreq: - logger.warning("Failed deleting bzz message \n%s", badreq) - self._bzz_mess_id = 0 - - # Fixme: check if media in message! - await self._status_message.edit_media(media=InputMediaPhoto(photo)) - await self._status_message.edit_caption(caption=message, parse_mode=ParseMode.MARKDOWN_V2) - - if self._progress_update_message: - mes = await self._bot.send_message(self._chat_id, text="Status has been updated\nThis message will be deleted", disable_notification=silent) - self._bzz_mess_id = mes.message_id + async def _send_photo(self, group_only, manual, message, silent): + loop = asyncio.get_running_loop() + with await loop.run_in_executor(self._executors_pool, self._cam_wrap.take_photo) as photo: + if not group_only: + await self._bot.send_chat_action(chat_id=self._chat_id, action=ChatAction.UPLOAD_PHOTO) + if self._status_message and not manual: + if self._bzz_mess_id != 0: + try: + await self._bot.delete_message(self._chat_id, self._bzz_mess_id) + except BadRequest as badreq: + logger.warning("Failed deleting bzz message \n%s", badreq) + self._bzz_mess_id = 0 + + # Fixme: check if media in message! + await self._status_message.edit_media(media=InputMediaPhoto(photo)) + await self._status_message.edit_caption(caption=message, parse_mode=ParseMode.MARKDOWN_V2) - else: - sent_message = await self._bot.send_photo( - self._chat_id, - photo=photo, - caption=message, - parse_mode=ParseMode.MARKDOWN_V2, - disable_notification=silent, - ) - if not self._status_message and not manual: - self._status_message = sent_message - - for group in self._notify_groups: - photo.seek(0) - await self._bot.send_chat_action(chat_id=group, action=ChatAction.UPLOAD_PHOTO) - if group in self._groups_status_mesages and not manual: - mess = self._groups_status_mesages[group] - await mess.edit_media(media=InputMediaPhoto(photo)) - await mess.edit_caption(caption=message, parse_mode=ParseMode.MARKDOWN_V2) - else: - sent_message = await self._bot.send_photo( - group, - photo=photo, - caption=message, - parse_mode=ParseMode.MARKDOWN_V2, - disable_notification=silent, - ) - if group in self._groups_status_mesages or manual: - continue - self._groups_status_mesages[group] = sent_message - - photo.close() + if self._progress_update_message: + mes = await self._bot.send_message(self._chat_id, text="Status has been updated\nThis message will be deleted", disable_notification=silent) + self._bzz_mess_id = mes.message_id + + else: + sent_message = await self._bot.send_photo( + self._chat_id, + photo=photo, + caption=message, + parse_mode=ParseMode.MARKDOWN_V2, + disable_notification=silent, + ) + if not self._status_message and not manual: + self._status_message = sent_message + + for group in self._notify_groups: + photo.seek(0) + await self._bot.send_chat_action(chat_id=group, action=ChatAction.UPLOAD_PHOTO) + if group in self._groups_status_mesages and not manual: + mess = self._groups_status_mesages[group] + await mess.edit_media(media=InputMediaPhoto(photo)) + await mess.edit_caption(caption=message, parse_mode=ParseMode.MARKDOWN_V2) + else: + sent_message = await self._bot.send_photo( + group, + photo=photo, + caption=message, + parse_mode=ParseMode.MARKDOWN_V2, + disable_notification=silent, + ) + if group in self._groups_status_mesages or manual: + continue + self._groups_status_mesages[group] = sent_message + + photo.close() + + async def _notify(self, message: str, silent: bool, group_only: bool = False, manual: bool = False, finish: bool = False) -> None: + try: + if not self._cam_wrap.enabled: + await self._send_message(message, silent, manual) + else: + await self._send_photo(group_only, manual, message, silent) + except Exception as ex: + logger.error(ex) + finally: + if finish: + await self.reset_notifications() # manual notification methods def send_error(self, message: str, logs_upload: bool = False) -> None: @@ -319,7 +328,7 @@ async def reset_notifications(self) -> None: finally: self._bzz_mess_id = 0 - def _schedule_notification(self, message: str = "", schedule: bool = False) -> None: # pylint: disable=W0613 + def _schedule_notification(self, message: str = "", schedule: bool = False, finish: bool = False) -> None: # pylint: disable=W0613 mess = escape_markdown(self._klippy.get_print_stats(message), version=2) if self._last_m117_status and "m117_status" in self._message_parts: mess += f"{escape_markdown(self._last_m117_status, version=2)}\n" @@ -330,11 +339,7 @@ def _schedule_notification(self, message: str = "", schedule: bool = False) -> N self._sched.add_job( self._notify, - kwargs={ - "message": mess, - "silent": self._silent_progress, - "group_only": self._group_only, - }, + kwargs={"message": mess, "silent": self._silent_progress, "group_only": self._group_only, "finish": finish}, misfire_grace_time=None, coalesce=False, max_instances=6, @@ -453,8 +458,7 @@ def send_print_start_info(self) -> None: # Todo: reset something? or check if reseted by setting new filename? async def _send_print_finish(self) -> None: - self._schedule_notification(message="Finished printing") - await self.reset_notifications() + self._schedule_notification(message="Finished printing", finish=True) def send_print_finish(self) -> None: if self._enabled: diff --git a/bot/websocket_helper.py b/bot/websocket_helper.py index 5fb8064..0eaa488 100644 --- a/bot/websocket_helper.py +++ b/bot/websocket_helper.py @@ -282,8 +282,8 @@ async def parse_print_stats(self, message_params): self._notifier.remove_notifier_timer() # Fixme: check manual mode self._timelapse.is_running = False - if not self._timelapse.manual_mode: - self._timelapse.send_timelapse() + # if not self._timelapse.manual_mode: + # self._timelapse.send_timelapse() self._notifier.send_printer_status_notification(f"Printer state change: {print_stats_loc['state']} \n") elif state == "cancelled": self._klippy.paused = False diff --git a/scripts/requirements.txt b/scripts/requirements.txt index 662a2a4..571ef99 100644 --- a/scripts/requirements.txt +++ b/scripts/requirements.txt @@ -9,4 +9,4 @@ psutil==6.1.0 python-telegram-bot[socks,http2,rate-limiter,callback-data,job-queue]==21.7 tzlocal==2.1 uvloop==0.20.0 ; platform_system != "Windows" -websockets==14.0 +websockets==14.1