Skip to content

Commit 122b4ef

Browse files
Prepare 1.10.0 release
2 parents e4ec6fc + 00a44dd commit 122b4ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+7415
-4991
lines changed

octoprint_telegram/__init__.py

Lines changed: 676 additions & 502 deletions
Large diffs are not rendered by default.

octoprint_telegram/commands/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from abc import ABC, abstractmethod
2+
from typing import TYPE_CHECKING
3+
4+
if TYPE_CHECKING:
5+
from .. import TelegramPlugin
6+
7+
8+
class CommandContext:
9+
def __init__(
10+
self, cmd: str, chat_id: str, from_id: str, parameter: str = "", msg_id_to_update: str = "", user: str = ""
11+
):
12+
self.cmd = cmd
13+
self.chat_id = chat_id
14+
self.from_id = from_id
15+
self.parameter = parameter
16+
self.msg_id_to_update = msg_id_to_update
17+
self.user = user
18+
19+
20+
class BaseCommand(ABC):
21+
def __init__(self, main: "TelegramPlugin"):
22+
self.main = main
23+
self._logger = main._logger.getChild("Commands")
24+
25+
def __call__(self, cmd, chat_id, from_id, parameter="", msg_id_to_update="", user=""):
26+
context = CommandContext(cmd, chat_id, from_id, parameter, msg_id_to_update, user)
27+
return self.execute(context)
28+
29+
@abstractmethod
30+
def execute(self, context: CommandContext):
31+
pass
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from ..emoji import Emoji
2+
from .base import BaseCommand, CommandContext
3+
4+
render_emojis = Emoji.render_emojis
5+
6+
7+
class CmdAbort(BaseCommand):
8+
def execute(self, context: CommandContext):
9+
if context.parameter == "stop":
10+
self.main._printer.cancel_print(user=context.user)
11+
12+
msg = render_emojis("{emo:check} Aborting the print.")
13+
14+
self.main.send_msg(
15+
msg,
16+
chatID=context.chat_id,
17+
msg_id=context.msg_id_to_update,
18+
)
19+
else:
20+
if self.main._printer.is_printing() or self.main._printer.is_pausing() or self.main._printer.is_paused():
21+
msg = render_emojis("{emo:question} Really abort the currently running print?")
22+
23+
command_buttons = [
24+
[
25+
[
26+
render_emojis("{emo:check} Stop print"),
27+
f"{context.cmd}_stop",
28+
],
29+
[render_emojis("{emo:cancel} Close"), "close"],
30+
]
31+
]
32+
33+
self.main.send_msg(
34+
msg,
35+
responses=command_buttons,
36+
chatID=context.chat_id,
37+
msg_id=context.msg_id_to_update,
38+
)
39+
else:
40+
msg = render_emojis("{emo:warning} Currently I'm not printing, so there is nothing to stop.")
41+
42+
self.main.send_msg(
43+
msg,
44+
chatID=context.chat_id,
45+
msg_id=context.msg_id_to_update,
46+
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import html
2+
3+
from ..emoji import Emoji
4+
from .base import BaseCommand, CommandContext
5+
6+
render_emojis = Emoji.render_emojis
7+
8+
9+
class CmdCancelObject(BaseCommand):
10+
def execute(self, context: CommandContext):
11+
cancelobject_id = "cancelobject"
12+
13+
if not self.main._plugin_manager.get_plugin(cancelobject_id, True):
14+
msg = render_emojis(
15+
f"{{emo:attention}} Please install <a href='https://plugins.octoprint.org/plugins/{cancelobject_id}/'>Cancelobject</a> plugin."
16+
)
17+
self.main.send_msg(
18+
msg,
19+
chatID=context.chat_id,
20+
markup="HTML",
21+
msg_id=context.msg_id_to_update,
22+
)
23+
return
24+
25+
if context.parameter:
26+
params = context.parameter.split("_")
27+
28+
id = params[0]
29+
self.main.send_octoprint_simpleapi_command(cancelobject_id, "cancel", dict(cancelled=id))
30+
31+
msg = render_emojis("{emo:check} Command sent!")
32+
command_buttons = [[[render_emojis("{emo:back} Back"), context.cmd]]]
33+
34+
self.main.send_msg(
35+
msg,
36+
chatID=context.chat_id,
37+
markup="HTML",
38+
responses=command_buttons,
39+
msg_id=context.msg_id_to_update,
40+
)
41+
else:
42+
objlist = self.main.send_octoprint_simpleapi_command(cancelobject_id, "objlist").json().get("list", [])
43+
if objlist:
44+
msg = render_emojis("{emo:question} Which object do you want to cancel?")
45+
46+
cancelled_objects = [obj["object"] for obj in objlist if obj.get("cancelled", False)]
47+
if cancelled_objects:
48+
msg += "\n\nObjects already cancelled:\n"
49+
msg += "\n".join(f"- <code>{html.escape(object_name)}</code>" for object_name in cancelled_objects)
50+
51+
command_buttons = [
52+
[[obj["object"], f"{context.cmd}_{obj['id']}"]]
53+
for obj in objlist
54+
if not obj.get("cancelled", False)
55+
]
56+
command_buttons.append([[render_emojis("{emo:cancel} Close"), "close"]])
57+
58+
self.main.send_msg(
59+
msg,
60+
chatID=context.chat_id,
61+
markup="HTML",
62+
responses=command_buttons,
63+
msg_id=context.msg_id_to_update,
64+
)
65+
else:
66+
msg = render_emojis(
67+
"{emo:attention} No objects found. Please make sure you've selected for printing the gcode."
68+
)
69+
self.main.send_msg(
70+
msg,
71+
chatID=context.chat_id,
72+
markup="HTML",
73+
msg_id=context.msg_id_to_update,
74+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from .base import BaseCommand, CommandContext
2+
3+
4+
class CmdClose(BaseCommand):
5+
def execute(self, context: CommandContext):
6+
# According to https://core.telegram.org/bots/api#deletemessage:
7+
# - A message can only be deleted if it was sent less than 48 hours ago.
8+
# The try-except block handles this condition.
9+
try:
10+
if context.msg_id_to_update:
11+
self.main.telegram_utils.send_telegram_request(
12+
f"{self.main.bot_url}/deleteMessage",
13+
"post",
14+
data=dict(chat_id=context.chat_id, message_id=context.msg_id_to_update),
15+
)
16+
except Exception:
17+
pass

0 commit comments

Comments
 (0)