Skip to content

Commit 3091fc3

Browse files
committed
fix some logic around status_inline_update keyboard
1 parent 24135e5 commit 3091fc3

File tree

5 files changed

+59
-70
lines changed

5 files changed

+59
-70
lines changed

bot/klippy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ async def check_connection(self) -> str:
392392
except Exception as ex:
393393
logger.error(ex, exc_info=True)
394394
retries += 1
395-
time.sleep(1)
395+
await asyncio.sleep(1)
396396
return f"Connection failed. {last_reason}"
397397

398398
def update_sensor(self, name: str, value) -> None:

bot/main.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import subprocess
1818
import sys
1919
import tarfile
20-
import time
2120
from typing import Any, Dict, List, Optional, Union
2221
from zipfile import ZipFile
2322

@@ -151,24 +150,9 @@ async def status_no_confirm(effective_message: Message) -> None:
151150
is_inline_button_press = effective_message.from_user is not None and effective_message.from_user.id == effective_message.get_bot().id
152151
if klippy.printing and not configWrap.notifications.group_only:
153152
notifier.update_status()
154-
time.sleep(configWrap.camera.light_timeout + 1.5)
155-
if not is_inline_button_press:
156-
await effective_message.delete()
157153
else:
158154
text = await klippy.get_status()
159-
inline_keyboard = None
160-
if configWrap.telegram_ui.status_update_button:
161-
inline_keyboard = InlineKeyboardMarkup(
162-
[
163-
[
164-
InlineKeyboardButton(
165-
text="Update",
166-
callback_data="updstatus",
167-
)
168-
]
169-
]
170-
)
171-
message = TelegramMessageRepr(text, parse_mode=ParseMode.HTML, silent=notifier.silent_commands, reply_markup=inline_keyboard)
155+
message = TelegramMessageRepr(text, parse_mode=ParseMode.HTML, silent=notifier.silent_commands, reply_markup=notifier.get_status_keyboard())
172156
if cameraWrap.enabled:
173157
loop_loc = asyncio.get_running_loop()
174158
with await loop_loc.run_in_executor(executors_pool, cameraWrap.take_photo) as bio:

bot/notifications.py

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,38 @@ def interval(self, new_value: int) -> None:
129129
self._interval = new_value
130130
self._reschedule_notifier_timer()
131131

132+
def get_status_keyboard(self, finish: bool = False) -> Optional[InlineKeyboardMarkup]:
133+
inline_keyboard = None
134+
if self._use_status_update_button and not finish:
135+
inline_keyboard = InlineKeyboardMarkup(
136+
[
137+
[
138+
InlineKeyboardButton(
139+
text="Update",
140+
callback_data="updstatus",
141+
)
142+
]
143+
]
144+
)
145+
return inline_keyboard
146+
147+
async def _send_bzz_message(self, message: TelegramMessageRepr) -> None:
148+
if self._progress_update_message:
149+
if self._bzz_mess_id != 0:
150+
try:
151+
await self._bot.delete_message(self._chat_id, self._bzz_mess_id)
152+
except BadRequest as badreq:
153+
logger.warning("Failed deleting bzz message \n%s", badreq)
154+
self._bzz_mess_id = 0
155+
mes = await self._bot.send_message(self._chat_id, text="Status has been updated\nThis message will be deleted", disable_notification=message.is_silent())
156+
self._bzz_mess_id = mes.message_id
157+
132158
async def _send_message(self, message: TelegramMessageRepr, group_only: bool = False, manual: bool = False) -> None:
133159
if not group_only:
134160
if self._status_message and not manual:
135-
if self._bzz_mess_id != 0:
136-
try:
137-
await self._bot.delete_message(self._chat_id, self._bzz_mess_id)
138-
except BadRequest as badreq:
139-
logger.warning("Failed deleting bzz message \n%s", badreq)
140-
self._bzz_mess_id = 0
141161
await message.update_existing(self._status_message)
142-
143-
if self._progress_update_message:
144-
mes = await self._bot.send_message(self._chat_id, text="Status has been updated\nThis message will be deleted", disable_notification=message.is_silent())
145-
self._bzz_mess_id = mes.message_id
162+
await self._send_bzz_message(message)
146163
else:
147-
148164
sent_message = await message.send(self._bot, self._chat_id)
149165
if not self._status_message and not manual:
150166
self._status_message = sent_message
@@ -165,18 +181,8 @@ async def _send_photo(self, message: TelegramMessageRepr, group_only: bool = Fal
165181
with await loop.run_in_executor(self._executors_pool, self._cam_wrap.take_photo) as photo:
166182
if not group_only:
167183
if self._status_message and not manual:
168-
if self._bzz_mess_id != 0:
169-
try:
170-
await self._bot.delete_message(self._chat_id, self._bzz_mess_id)
171-
except BadRequest as badreq:
172-
logger.warning("Failed deleting bzz message \n%s", badreq)
173-
self._bzz_mess_id = 0
174184
await message.update_existing(self._status_message, photo=photo)
175-
176-
if self._progress_update_message:
177-
mes = await self._bot.send_message(self._chat_id, text="Status has been updated\nThis message will be deleted", disable_notification=message.is_silent())
178-
self._bzz_mess_id = mes.message_id
179-
185+
await self._send_bzz_message(message)
180186
else:
181187
sent_message = await message.send(self._bot, self._chat_id, photo=photo)
182188
if not self._status_message and not manual:
@@ -198,12 +204,12 @@ async def _send_photo(self, message: TelegramMessageRepr, group_only: bool = Fal
198204

199205
async def _notify(self, message: TelegramMessageRepr, group_only: bool = False, manual: bool = False, finish: bool = False) -> None:
200206
try:
201-
if not self._cam_wrap.enabled:
202-
await self._send_message(message, manual)
207+
if self._cam_wrap.enabled:
208+
await self._send_photo(message, group_only=group_only, manual=manual)
203209
else:
204-
await self._send_photo(message, group_only, manual)
210+
await self._send_message(message, group_only=group_only, manual=manual)
205211
except Exception as ex:
206-
logger.error(ex)
212+
logger.error(ex, exc_info=True, stack_info=True)
207213
finally:
208214
if finish:
209215
await self.reset_notifications()
@@ -326,23 +332,11 @@ def _schedule_notification(self, message: str = "", schedule: bool = False, fini
326332
if "last_update_time" in self._message_parts:
327333
mess += f"_Last update at {datetime.now():%H:%M:%S}_"
328334

329-
inline_keyboard = None
330-
if self._use_status_update_button and not finish:
331-
inline_keyboard = InlineKeyboardMarkup(
332-
[
333-
[
334-
InlineKeyboardButton(
335-
text="Update",
336-
callback_data="updstatus",
337-
)
338-
]
339-
]
340-
)
341335
tg_message = TelegramMessageRepr(
342336
text=mess,
343337
silent=self._silent_progress,
344338
suppress_escaping=True,
345-
reply_markup=inline_keyboard,
339+
reply_markup=self.get_status_keyboard(finish=finish),
346340
)
347341

348342
self._sched.add_job(
@@ -425,34 +419,45 @@ async def stop_all(self) -> None:
425419
await self.reset_notifications()
426420
self.remove_notifier_timer()
427421

422+
# Todo: refactor with TelegramMessageRepr class
428423
async def _send_print_start_info(self) -> None:
429424
message, bio = await self._klippy.get_file_info("Printer started printing")
425+
430426
if bio is not None:
431-
status_message = await self._bot.send_photo(
432-
self._chat_id,
433-
photo=bio,
434-
caption=message,
435-
disable_notification=self.silent_status,
436-
)
427+
if not self._group_only:
428+
status_message = await self._bot.send_photo(
429+
self._chat_id,
430+
photo=bio,
431+
caption=message,
432+
reply_markup=self.get_status_keyboard(),
433+
disable_notification=self.silent_status,
434+
)
435+
self._status_message = status_message
436+
437437
for group_, message_thread_id in self._notify_groups:
438438
bio.seek(0)
439439
self._groups_status_messages[group_] = await self._bot.send_photo(
440440
chat_id=group_,
441441
message_thread_id=message_thread_id,
442442
photo=bio,
443443
caption=message,
444+
reply_markup=self.get_status_keyboard(),
444445
disable_notification=self.silent_status,
445446
)
446447
bio.close()
447448
else:
448-
status_message = await self._bot.send_message(chat_id=self._chat_id, text=message, disable_notification=self.silent_status)
449+
if not self._group_only:
450+
status_message = await self._bot.send_message(chat_id=self._chat_id, text=message, reply_markup=self.get_status_keyboard(), disable_notification=self.silent_status)
451+
self._status_message = status_message
452+
449453
for group_, message_thread_id in self._notify_groups:
450-
self._groups_status_messages[group_] = await self._bot.send_message(chat_id=group_, message_thread_id=message_thread_id, text=message, disable_notification=self.silent_status)
451-
self._status_message = status_message
454+
self._groups_status_messages[group_] = await self._bot.send_message(
455+
chat_id=group_, message_thread_id=message_thread_id, text=message, reply_markup=self.get_status_keyboard(), disable_notification=self.silent_status
456+
)
452457

453-
if self._pin_status_single_message:
458+
if self._pin_status_single_message and self._status_message is not None:
454459
await self._bot.unpin_all_chat_messages(self._chat_id)
455-
await self._bot.pin_chat_message(self._chat_id, status_message.message_id, disable_notification=self.silent_status)
460+
await self._bot.pin_chat_message(self._chat_id, self._status_message.message_id, disable_notification=self.silent_status)
456461

457462
def send_print_start_info(self) -> None:
458463
if self._enabled:

bot/websocket_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def power_device_state(self, device):
310310
self._klippy.light_device.device_state = device_state
311311

312312
async def websocket_to_message(self, ws_message):
313-
logger.debug(ws_message)
313+
# logger.debug(ws_message)
314314
json_message = orjson.loads(ws_message)
315315

316316
if "error" in json_message:

scripts/requirements.dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ black==25.1.0
33
identify==2.6.6
44
isort==6.0.0
55
memory_profiler==0.61.0
6-
mypy==1.14.1
6+
mypy==1.15.0
77
numpy~=2.2.2 ; python_version>='3.10'
88
numpy~=2.0.2 ; python_version=='3.9'
99
opencv-python~=4.11.0.86

0 commit comments

Comments
 (0)