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

Fix Map Plugin #118

Merged
merged 6 commits into from
Dec 25, 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
15 changes: 11 additions & 4 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ lint:
- [email protected]
- [email protected]
- [email protected]
- [email protected].334
- [email protected].344
- git-diff-check
- [email protected]
- [email protected]
- [email protected].1
- [email protected].2
- [email protected]
- [email protected].3
- trufflehog@3.86.1
- [email protected].4
- trufflehog@3.88.0
- [email protected]
# Remove map_plugin.py after staticmaps is updated and we're able to work on it again
# For more info: https://github.com/geoffwhittington/meshtastic-matrix-relay/issues/117
ignore:
- linters: [ALL]
paths:
- sample_config.yaml
- plugins/map_plugin.py
actions:
disabled:
- trunk-announce
Expand Down
14 changes: 11 additions & 3 deletions matrix_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

matrix_client = None


def bot_command(command, event):
"""
Checks if the given command is directed at the bot,
Expand All @@ -62,11 +63,17 @@ def bot_command(command, event):
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):
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))
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
Expand All @@ -75,6 +82,7 @@ def bot_command(command, event):
# # 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():
"""
Establish a connection to the Matrix homeserver.
Expand Down
1 change: 1 addition & 0 deletions plugin_loader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# trunk-ignore-all(bandit)
import hashlib
import importlib.util
import os
Expand Down
38 changes: 28 additions & 10 deletions plugins/map_plugin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import staticmaps
import s2sphere
import io
import math
import random
import io
import re
from PIL import Image

import PIL.ImageDraw
import s2sphere
import staticmaps
from nio import AsyncClient, UploadResponse
from PIL import Image

from plugins.base_plugin import BasePlugin


def textsize(self: PIL.ImageDraw.ImageDraw, *args, **kwargs):
x, y, w, h = self.textbbox((0, 0), *args, **kwargs)
return w, h


# Monkeypatch fix for https://github.com/flopp/py-staticmaps/issues/39
PIL.ImageDraw.ImageDraw.textsize = textsize


class TextLabel(staticmaps.Object):
def __init__(self, latlng: s2sphere.LatLng, text: str, fontSize: int = 12) -> None:
staticmaps.Object.__init__(self)
Expand All @@ -35,7 +47,9 @@ def render_pillow(self, renderer: staticmaps.PillowRenderer) -> None:
x, y = renderer.transformer().ll2pixel(self.latlng())
x = x + renderer.offset_x()

tw, th = renderer.draw().textsize(self._text)
# Updated to use textbbox instead of textsize
bbox = renderer.draw().textbbox((0, 0), self._text)
tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
w = max(self._arrow, tw + 2 * self._margin)
h = th + 2 * self._margin

Expand Down Expand Up @@ -202,10 +216,14 @@ async def upload_image(client: AsyncClient, image: Image.Image) -> UploadRespons
async def send_room_image(
client: AsyncClient, room_id: str, upload_response: UploadResponse
):
response = await client.room_send(
await client.room_send(
room_id=room_id,
message_type="m.room.message",
content={"msgtype": "m.image", "url": upload_response.content_uri, "body": ""},
content={
"msgtype": "m.image",
"url": upload_response.content_uri,
"body": "image.png",
},
)


Expand All @@ -220,7 +238,7 @@ class Plugin(BasePlugin):
@property
def description(self):
return (
f"Map of mesh radio nodes. Supports `zoom` and `size` options to customize"
"Map of mesh radio nodes. Supports `zoom` and `size` options to customize"
)

async def handle_meshtastic_message(
Expand All @@ -235,7 +253,7 @@ def get_mesh_commands(self):
return []

async def handle_room_message(self, room, event, full_message):
# Pass the whole event to matches() for compatibility w/ updated base_plugin.py
# Pass the whole event to matches() for compatibility w/ updated base_plugin.py
if not self.matches(event):
return False

Expand Down Expand Up @@ -275,7 +293,7 @@ async def handle_room_message(self, room, event, full_message):
image_size = (1000, 1000)

locations = []
for node, info in meshtastic_client.nodes.items():
for _node, info in meshtastic_client.nodes.items():
if "position" in info and "latitude" in info["position"]:
locations.append(
{
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
meshtastic
Pillow==11
py-staticmaps==0.4.0
git+https://github.com/flopp/py-staticmaps.git@e0266dc40163e87ce42a0ea5d8836a9a4bd92208
matrix-nio==0.25.2
matplotlib==3.9.0
requests==2.32.3
markdown==3.4.3
haversine==2.8.0
schedule==1.2.0
schedule==1.2.0