Heavy improvements. Version 1.2

This commit is contained in:
Ben 2025-06-30 09:38:59 -07:00
parent 24f6f25506
commit 2193a9ce62
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
5 changed files with 67 additions and 39 deletions

View file

@ -2,8 +2,11 @@
# DO NOT CHANGE THIS UNLESS YOU KNOW WHAT YOU ARE DOING # DO NOT CHANGE THIS UNLESS YOU KNOW WHAT YOU ARE DOING
command_prefix: report command_prefix: report
# Which regions the bot should pay attention to # Which regions the bot should pay attention to.
# Additionally, optionally whitelist specific accounts for each region # Pass through empty list to allow all senders for a region.
# Add a list of MatrixIDs to whitelist senders for a region.
# Use __global__ to refer to all regions.
# Code looks to see if a sender is allowed for either a specific region or __global__.
allowed_regions: allowed_regions:
foo: foo:
- "@reports:fiftyfiftyonerarizona.org" - "@reports:fiftyfiftyonerarizona.org"
@ -14,6 +17,9 @@ allowed_regions:
aaa: aaa:
# Bot-specific configurations per region # Bot-specific configurations per region
# Code will assume no authentication if either username or password are missing.
# Use __global__ to specify an endpoint for all regions.
# Code will push to both __global__ and region-specific endpoint.
region_configs: region_configs:
arizona: arizona:
# Where and how the plugin will send the data to # Where and how the plugin will send the data to
@ -21,10 +27,11 @@ region_configs:
server_topic: "changeme" server_topic: "changeme"
# If the plugin should use a username and password to send notifications # If the plugin should use a username and password to send notifications
server_use_authentication: true username: "no"
server_username: "no" password: "way"
server_password: "way"
# If this bot should send a reaction to the message # If this bot should send a reaction to the message
# if the notification was successful # Will send 👍 on successful POST to NTFY, and 👎 on failiure.
# Failiure to post to either the region specific, or __global__ endpoint is
# considered complete failiure.
send_reaction: true send_reaction: true

9
compile.bash Normal file
View file

@ -0,0 +1,9 @@
#!/bin/bash
# Get filename
id=$(cat maubot.yaml | grep "id:" | tr ' ' '\n' | tail -n1)
version=$(cat maubot.yaml | grep "version:" | tr ' ' '\n' | tail -n1)
filename="$id-$version.mbp"
# Compress to zip
zip $filename *

View file

@ -30,39 +30,44 @@ class ConsumerNTFY(Plugin):
return self.config["command_prefix"] return self.config["command_prefix"]
# Checks if a sender if allowed to send for a particular region # Checks if a sender if allowed to send for a particular region
def validateSender(self, region: str, sender: str): def validate_sender(self, region: str, sender: str):
# Check that config value exists # Mautrix isn't documented, like at all, so I'm just gonna catch the
if not self.config["allowed_regions"]: return False # error because IDK how to see if a map is inside a map.
# Check that region is allowed try: allowed_list = self.config["allowed_regions"][region]
if not self.config["allowed_regions"][region]: return False except: return False
# All senders allowed for this region
if len(self.config["allowed_regions"][region]) == 0: return True
# Check that sender is allowed for region
if not sender in self.config["allowed_regions"][region]: return False
return True
# Does the necesary config checks for the given event if len(allowed_list) == 0: return True
# Returns list of regions to process (strings) if sender in allowed_list: return True
# Currently just the specified region and "__global__"
def validateReport(self, evt: MessageEvent, message: str): # Sender not allowed in region config
return False
# Does the necessary config checks for the given event
# Returns list of recursive configs to process
def validate_report(self, evt: MessageEvent, message: str):
# Split command (minus !command_name) into tokens # Split command (minus !command_name) into tokens
tokens = message.split() tokens = message.split()
region = tokens[0].lower() region = tokens[0].lower()
# Each command must have a state/territory designation and a message # Each command must have a state/territory designation and a message
if len(tokens) < 2: return None if len(tokens) < 2: return []
# And we must have self.config["region_configs"]
try: trashvariable = self.config["allowed_regions"][region]
except: return []
self.log.debug(region) configs = [] # To be returned
# This is a list of regions to process for this specific message allowed_globally = self.validate_sender("__global__", evt.sender)
# This is only used to consider __global__ allowed_region = self.validate_sender(region, evt.sender)
regions_to_process = []
if (self.validateSender("__global__", evt.sender)):
regions_to_process.append("__global__")
if (self.validateSender(region, evt.sender)):
regions_to_process.append(region)
return regions_to_process # If user is allowed globally and/or for a region,
# the plugin should process both the region and __global__ configs
if allowed_globally or allowed_region:
for i in ["__global__", region]:
try: configs.append(self.config["region_configs"][i])
except: trashvariable = None
return configs
# What gets called when !command_name message is sent # What gets called when !command_name message is sent
@command.new(name=get_command_name, help="Report Something") @command.new(name=get_command_name, help="Report Something")
@ -73,12 +78,11 @@ class ConsumerNTFY(Plugin):
# Iterate through each endpoint that the message should be pushed to # Iterate through each endpoint that the message should be pushed to
# (if any) # (if any)
for region in self.validateReport(evt, message): for region_config in self.validate_report(evt, message):
# Detect no regions in reaction # Detect no regions in reaction
if ntfy_posts_passed is None: ntfy_posts_passed = True if ntfy_posts_passed is None: ntfy_posts_passed = True
# Grab region-specific conrfig self.log.debug(region_config)
region_config = self.config["region_configs"][region]
# Create notification text # Create notification text
split_message = message.split() split_message = message.split()
@ -88,12 +92,12 @@ class ConsumerNTFY(Plugin):
url = region_config["server_url"] + "/" + region_config["server_topic"] url = region_config["server_url"] + "/" + region_config["server_topic"]
# Consider authentication # Consider authentication
authentication = None auth = None
if region_config["server_use_authentication"]: if "username" in region_config and "password" in region_config:
authentication = aiohttp.BasicAuth(region_config["server_username"], region_config["server_password"]) auth = aiohttp.BasicAuth(region_config["username"], region_config["password"])
# Send notification # Send notification
async with self.http.post(url, data=text, auth=authentication) as response: async with self.http.post(url, data=text, auth=auth) as response:
if not response.status == 200: if not response.status == 200:
ntfy_posts_passed = False ntfy_posts_passed = False

View file

@ -1,6 +1,6 @@
maubot: 0.1.0 maubot: 0.1.0
id: org.fiftyfiftyonearizona.reports.consumerntfy id: org.fiftyfiftyonearizona.reports.consumerntfy
version: 1.1.0 version: 1.2.0
license: MIT license: MIT
modules: modules:
- consumerntfy - consumerntfy

View file

@ -2,4 +2,12 @@
Please see https://git.fiftyfiftyonearizona.org/webmaster/matrix-report-documentation Please see https://git.fiftyfiftyonearizona.org/webmaster/matrix-report-documentation
This plugin supports whitelisting state/territory designators, allowed senders, reaction receipt, and authenticated NTFY posting. This plugin supports the following:
* whitelisting regions, or allowing all regions.
* allowed senders per region, or all regions.
* reaction receipt upon successful POST to NTFY.
* authenticated NTFY POSTing.
* Different NTFY endpoint, topic, and authentication per region.
Please see [base-config.yaml](base-config.yaml) for explanation of behavior.