Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot command work #115

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions matrix_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,34 @@

matrix_client = None

def bot_command(command, event):
"""
Checks if the given command is directed at the bot,
accounting for variations in different Matrix clients.
"""
full_message = event.body.strip()
content = event.source.get("content", {})
formatted_body = content.get("formatted_body", "")

# Remove HTML tags and extract the text content
text_content = re.sub(r"<[^>]+>", "", formatted_body).strip()

# Check if the message starts with bot_user_id or bot_user_name
if full_message.startswith(bot_user_id) or text_content.startswith(bot_user_id):
# Construct a regex pattern to match variations of bot mention and command
pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$"
return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content))
elif full_message.startswith(bot_user_name) or text_content.startswith(bot_user_name):
# Construct a regex pattern to match variations of bot mention and command
pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$"
return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content))
else:
return False
# # Construct a regex pattern to match variations of bot mention and command
# pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$"

def bot_command(command, payload):
# Checks if the given command is directed at the bot
return f"{bot_user_name}: !{command}" in payload

# # Check if the message matches the pattern
# return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content))

async def connect_matrix():
"""
Expand Down Expand Up @@ -470,7 +493,7 @@ async def on_room_message(
is_command = False
for plugin in plugins:
for command in plugin.get_matrix_commands():
if bot_command(command, text):
if bot_command(command, event):
is_command = True
break
if is_command:
Expand Down
7 changes: 3 additions & 4 deletions plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,11 @@ def get_node_data(self, meshtastic_id):
def get_data(self):
return get_plugin_data(self.plugin_name)

def matches(self, payload):
def matches(self, event):
from matrix_utils import bot_command

if isinstance(payload, str):
return bot_command(self.plugin_name, payload)
return False
# Pass the entire event to bot_command
return bot_command(self.plugin_name, event)

@abstractmethod
async def handle_meshtastic_message(
Expand Down
3 changes: 2 additions & 1 deletion plugins/drop_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ async def handle_meshtastic_message(
return True

async def handle_room_message(self, room, event, full_message):
if self.matches(full_message):
# Pass the event to matches() instead of full_message
if self.matches(event):
return True
3 changes: 1 addition & 2 deletions plugins/health_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ async def handle_meshtastic_message(

async def handle_room_message(self, room, event, full_message):

full_message = full_message.strip()
if not self.matches(full_message):
if not self.matches(event):
return False

await self.send_matrix_message(
Expand Down
4 changes: 2 additions & 2 deletions plugins/help_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def get_mesh_commands(self):
return []

async def handle_room_message(self, room, event, full_message):
full_message = full_message.strip()
if not self.matches(full_message):
# Pass the event to matches()
if not self.matches(event):
return False

command = None
Expand Down
4 changes: 2 additions & 2 deletions plugins/map_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ def get_mesh_commands(self):
return []

async def handle_room_message(self, room, event, full_message):
full_message = full_message.strip()
if not self.matches(full_message):
# Pass the event to matches()
if not self.matches(event):
return False

from matrix_utils import connect_matrix
Expand Down
17 changes: 10 additions & 7 deletions plugins/mesh_relay_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,19 @@ async def handle_meshtastic_message(

return False

def matches(self, payload):
if isinstance(payload, str):
match = re.match(r"^Processed (.+) radio packet$", payload)
return match
def matches(self, event):
# Check for the presence of necessary keys in the event
content = event.source.get("content", {})
body = content.get("body", "")

if isinstance(body, str):
match = re.match(r"^Processed (.+) radio packet$", body)
return bool(match)
return False

async def handle_room_message(self, room, event, full_message):
full_message = full_message.strip()

if not self.matches(full_message):
# Use the event for matching instead of full_message
if not self.matches(event):
return False

channel = None
Expand Down
5 changes: 2 additions & 3 deletions plugins/nodes_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ async def handle_meshtastic_message(
return False

async def handle_room_message(self, room, event, full_message):

full_message = full_message.strip()
if not self.matches(full_message):
# Pass the event to matches()
if not self.matches(event):
return False

await self.send_matrix_message(
Expand Down
4 changes: 2 additions & 2 deletions plugins/ping_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def get_mesh_commands(self):
return [self.plugin_name]

async def handle_room_message(self, room, event, full_message):
full_message = full_message.strip()
if not self.matches(full_message):
# Pass the event to matches()
if not self.matches(event):
return False

await self.send_matrix_message(room.room_id, "pong!")
Expand Down
14 changes: 7 additions & 7 deletions plugins/telemetry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ def get_matrix_commands(self):
def get_mesh_commands(self):
return []

def matches(self, payload):
def matches(self, event):
from matrix_utils import bot_command

if isinstance(payload, str):
for option in ["batteryLevel", "voltage", "airUtilTx"]:
if bot_command(option, payload):
return True
# Use bot_command() to check if any of the commands match
for command in self.get_matrix_commands():
if bot_command(command, event):
return True
return False

async def handle_room_message(self, room, event, full_message):
full_message = full_message.strip()
if not self.matches(full_message):
# Pass the event to matches()
if not self.matches(event):
return False

match = re.search(
Expand Down