Template
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from base_plugin import BasePlugin, HookResult, HookStrategy
|
|
from android_utils import log
|
|
from ui.settings import Header, Input, Text
|
|
from greeting import render, TemplateError
|
|
|
|
DEFAULT_TEMPLATE = "Hello, {name}!"
|
|
|
|
|
|
class HelloPlugin(BasePlugin):
|
|
def on_plugin_load(self):
|
|
log("hello_bundle loaded")
|
|
self.add_on_send_message_hook()
|
|
|
|
def on_plugin_unload(self):
|
|
log("hello_bundle unloaded")
|
|
|
|
def create_settings(self):
|
|
return [
|
|
Header(text="Hello Bundle"),
|
|
Input(
|
|
key="template",
|
|
text="Greeting template",
|
|
default=DEFAULT_TEMPLATE,
|
|
subtext="Use {name} as placeholder.",
|
|
icon="msg_edit",
|
|
),
|
|
Text(
|
|
text="Example",
|
|
subtext=".hello alice smith -> Hello, Alice Smith!",
|
|
icon="msg_info",
|
|
),
|
|
]
|
|
|
|
def on_send_message_hook(self, account, params):
|
|
message = getattr(params, "message", None)
|
|
if not isinstance(message, str):
|
|
return HookResult()
|
|
stripped = message.strip()
|
|
if not stripped.startswith(".hello"):
|
|
return HookResult()
|
|
parts = stripped.split(" ", 1)
|
|
name = parts[1] if len(parts) > 1 else ""
|
|
if not name.strip():
|
|
params.message = "Usage: .hello <name>"
|
|
else:
|
|
try:
|
|
params.message = render(self.get_setting("template", DEFAULT_TEMPLATE), name)
|
|
except TemplateError as exc:
|
|
params.message = "Bad template: " + str(exc)
|
|
return HookResult(strategy=HookStrategy.MODIFY, params=params)
|