mirror of
https://github.com/agessaman/meshcore-bot.git
synced 2026-10-22 22:11:15 +00:00
feat: Enhance keyword response handling with escape sequence support
- Updated `config.ini.example` and `README.md` to document the use of escape sequences for newlines and other formatting in keyword responses. - Implemented `_decode_escape_sequences` method in `CommandManager` to process escape sequences like `\\n`, `\\t`, and `\\r`, allowing users to format responses more flexibly. - Modified `load_keywords` and `load_syntax_patterns` methods to decode escape sequences in keyword responses and formats, improving user experience.
This commit is contained in:
1 parent
7984fa3af6
commit
ce561c18db
3 files changed
+52
No files matched your search
@@ -205,6 +205,24 @@ Keyword responses support these template variables:
|
||||
- `{timestamp}` - Message timestamp
|
||||
- `{path}` - Message routing path
|
||||
|
||||
### Adding Newlines
|
||||
|
||||
To add newlines in keyword responses, use `\\n` (double backslash + n):
|
||||
|
||||
```ini
|
||||
[Keywords]
|
||||
test = "Line 1\\nLine 2\\nLine 3"
|
||||
```
|
||||
|
||||
This will output:
|
||||
```
|
||||
Line 1
|
||||
Line 2
|
||||
Line 3
|
||||
```
|
||||
|
||||
Other escape sequences: `\\t` (tab), `\\r` (carriage return), `\\\\` (literal backslash)
|
||||
|
||||
Example:
|
||||
```ini
|
||||
[Keywords]
|
||||
|
||||
@@ -229,6 +229,15 @@ long_jokes = false
|
||||
# {path_distance} - Total distance between all hops in path with locations (e.g., "123.4km (3 segs, 1 no-loc)")
|
||||
# {firstlast_distance} - Distance between first and last repeater in path (e.g., "45.6km" or empty if locations missing)
|
||||
# {elapsed} - Message elapsed time
|
||||
#
|
||||
# To add newlines in responses, use \\n (double backslash + n):
|
||||
# Example: test = "Line 1\\nLine 2\\nLine 3"
|
||||
# This will output:
|
||||
# Line 1
|
||||
# Line 2
|
||||
# Line 3
|
||||
#
|
||||
# Other escape sequences: \\t (tab), \\r (carriage return), \\\\ (literal backslash)
|
||||
#
|
||||
# Available placeholders (mesh network info - same as Scheduled_Messages):
|
||||
# Total counts (ever heard):
|
||||
|
||||
@@ -177,6 +177,27 @@ class CommandManager:
|
||||
self.bot.bot_tx_rate_limiter.record_tx()
|
||||
return True
|
||||
|
||||
def _decode_escape_sequences(self, text: str) -> str:
|
||||
"""Decode escape sequences in config strings.
|
||||
|
||||
Processes common escape sequences like \\n (newline), \\t (tab), \\\\ (backslash).
|
||||
This allows users to add newlines in keyword responses using \\n.
|
||||
|
||||
Args:
|
||||
text: The text string to process.
|
||||
|
||||
Returns:
|
||||
str: The text with escape sequences decoded.
|
||||
"""
|
||||
# Replace escape sequences
|
||||
# Order matters: \\ must be processed first to avoid double-processing
|
||||
text = text.replace('\\\\', '\x00') # Temporary placeholder for backslash
|
||||
text = text.replace('\\n', '\n') # Newline
|
||||
text = text.replace('\\t', '\t') # Tab
|
||||
text = text.replace('\\r', '\r') # Carriage return
|
||||
text = text.replace('\x00', '\\') # Restore backslash
|
||||
return text
|
||||
|
||||
def load_keywords(self) -> Dict[str, str]:
|
||||
"""Load keywords from config.
|
||||
|
||||
@@ -189,6 +210,8 @@ class CommandManager:
|
||||
# Strip quotes from the response if present
|
||||
if response.startswith('"') and response.endswith('"'):
|
||||
response = response[1:-1]
|
||||
# Decode escape sequences (e.g., \\n for newlines)
|
||||
response = self._decode_escape_sequences(response)
|
||||
keywords[keyword.lower()] = response
|
||||
return keywords
|
||||
|
||||
@@ -200,6 +223,8 @@ class CommandManager:
|
||||
# Strip quotes from the response format if present
|
||||
if response_format.startswith('"') and response_format.endswith('"'):
|
||||
response_format = response_format[1:-1]
|
||||
# Decode escape sequences (e.g., \\n for newlines)
|
||||
response_format = self._decode_escape_sequences(response_format)
|
||||
syntax_patterns[pattern] = response_format
|
||||
return syntax_patterns
|
||||
|
||||
|
||||
Reference in new issue
Block a user