mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-07-06 09:52:06 +02:00
Merge pull request #274 from fred777/fred777-add_bot_globals
Introduce _bot_globals for persistent data storage between bot executions
This commit is contained in:
@@ -39,6 +39,9 @@ BOT_MESSAGE_SPACING = 2.0
|
|||||||
_bot_send_lock = asyncio.Lock()
|
_bot_send_lock = asyncio.Lock()
|
||||||
_last_bot_send_time: float = 0.0
|
_last_bot_send_time: float = 0.0
|
||||||
|
|
||||||
|
# global container for persistent data storage between bot executions, will be added to execution namespace
|
||||||
|
_bot_globals: dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class BotCallPlan:
|
class BotCallPlan:
|
||||||
@@ -194,6 +197,7 @@ def execute_bot_code(
|
|||||||
# Build execution namespace with allowed imports
|
# Build execution namespace with allowed imports
|
||||||
namespace: dict[str, Any] = {
|
namespace: dict[str, Any] = {
|
||||||
"__builtins__": __builtins__,
|
"__builtins__": __builtins__,
|
||||||
|
"_bot_globals": _bot_globals,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -103,6 +103,15 @@ const DEFAULT_BOT_CODE = `def bot(**kwargs) -> str | list[str] | None:
|
|||||||
# Don't reply to our own outgoing messages
|
# Don't reply to our own outgoing messages
|
||||||
if is_outgoing:
|
if is_outgoing:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# If you want to make use of persistant data between calls to this function,
|
||||||
|
# you can put that data into the global _bot_globals dictionary, e.g.:
|
||||||
|
#
|
||||||
|
# bot_globals = globals()["_bot_globals"]
|
||||||
|
# if not "known_sender_names" in bot_globals:
|
||||||
|
# bot_globals["known_sender_names"] = set()
|
||||||
|
#
|
||||||
|
# bot_globals["known_sender_names"].add(sender_name)
|
||||||
|
|
||||||
# Example: Only respond in #bot channel to "!pling" command
|
# Example: Only respond in #bot channel to "!pling" command
|
||||||
if channel_name == "#bot" and "!pling" in message_text.lower():
|
if channel_name == "#bot" and "!pling" in message_text.lower():
|
||||||
|
|||||||
Reference in New Issue
Block a user