Upgrade to version 1.2
This commit is contained in:
parent
2b6dafbd37
commit
574eab2b06
5 changed files with 94 additions and 50 deletions
|
@ -2,24 +2,37 @@
|
||||||
# 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:
|
||||||
__global__:
|
|
||||||
- "@reports:fiftyfiftyonearizona.org"
|
|
||||||
foo:
|
foo:
|
||||||
- "@reports:fiftyfiftyonearizona.org"
|
- "@reports:fiftyfiftyonerarizona.org"
|
||||||
bar:
|
bar:
|
||||||
- "@reports:example.org"
|
- "@reports:example.org"
|
||||||
- "@reports:example.com"
|
- "@reports:example.com"
|
||||||
buzz:
|
buzz:
|
||||||
aaa:
|
aaa:
|
||||||
|
|
||||||
# Bot-specific configurations
|
# Bot-specific configurations per region
|
||||||
api_url: "https://example.org/wp-json/wp/v2/yourposttype"
|
# Use __global__ to specify an endpoint for all regions.
|
||||||
api_user: "youruser"
|
# Code will push to both __global__ and region-specific endpoint.
|
||||||
api_token: "your token"
|
region_configs:
|
||||||
|
__global__:
|
||||||
|
# Bot-specific configurations
|
||||||
|
api_url: "https://example.org/wp-json/wp/v2/yourposttype"
|
||||||
|
api_user: "youruser"
|
||||||
|
api_token: "your token"
|
||||||
|
arizona:
|
||||||
|
# Bot-specific configurations
|
||||||
|
api_url: "https://example.org/wp-json/wp/v2/yourposttype"
|
||||||
|
api_user: "youruser"
|
||||||
|
api_token: "your token"
|
||||||
|
|
||||||
# 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 creation of post, 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
9
compile.bash
Normal 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 *
|
|
@ -12,9 +12,7 @@ class Config(BaseProxyConfig):
|
||||||
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
||||||
helper.copy("command_prefix")
|
helper.copy("command_prefix")
|
||||||
helper.copy("allowed_regions")
|
helper.copy("allowed_regions")
|
||||||
helper.copy("api_url")
|
helper.copy("region_configs")
|
||||||
helper.copy("api_user")
|
|
||||||
helper.copy("api_token")
|
|
||||||
helper.copy("send_reaction")
|
helper.copy("send_reaction")
|
||||||
|
|
||||||
class ConsumerWordpress(Plugin):
|
class ConsumerWordpress(Plugin):
|
||||||
|
@ -30,59 +28,76 @@ class ConsumerWordpress(Plugin):
|
||||||
# Get !command_name setting from config to register it
|
# Get !command_name setting from config to register it
|
||||||
def get_command_name(self) -> str:
|
def get_command_name(self) -> str:
|
||||||
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 region in self.config["allowed_regions"]: return False
|
except: return False
|
||||||
# All senders allowed for this region
|
|
||||||
if len(self.config["allowed_regions"][region]) == 0: return True
|
if allowed_list is None: return True # Empty list
|
||||||
# Check that sender is allowed for region
|
if sender in allowed_list: return True
|
||||||
if not sender in self.config["allowed_regions"][region]: return False
|
|
||||||
return True
|
# Sender not allowed in region config
|
||||||
|
return False
|
||||||
|
|
||||||
# Does the necesary config checks for the given event
|
# Does the necessary config checks for the given event
|
||||||
# Returns list of regions to process (strings)
|
# Returns list of recursive configs to process
|
||||||
# Currently just the specified region and "__global__"
|
def validate_report(self, evt: MessageEvent, message: str):
|
||||||
def validateReport(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["region_configs"]
|
||||||
|
except: return []
|
||||||
|
|
||||||
if (self.validateSender("__global__", evt.sender)) or (self.validateSender(region, evt.sender)):
|
configs = [] # To be returned
|
||||||
return [region]
|
|
||||||
|
allowed_globally = self.validate_sender("__global__", evt.sender)
|
||||||
|
allowed_region = self.validate_sender(region, evt.sender)
|
||||||
|
|
||||||
|
# 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")
|
||||||
@command.argument("message", pass_raw=True)
|
@command.argument("message", pass_raw=True)
|
||||||
async def report(self, evt: MessageEvent, message: str) -> None:
|
async def report(self, evt: MessageEvent, message: str) -> None:
|
||||||
# If all have passed
|
# If all have passed
|
||||||
ntfy_posts_passed = None
|
wp_posts_passed = None
|
||||||
|
|
||||||
# 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)
|
||||||
region = self.validateReport(evt, message)
|
for region_config in self.validate_report(evt, message):
|
||||||
if region is None: return
|
# Detect no regions in reaction
|
||||||
|
if wp_posts_passed is None: wp_posts_passed = True
|
||||||
# Create post with only a title
|
|
||||||
split_message = message.split()
|
# Create post with only a title
|
||||||
title = "[" + split_message[0] + "] " + ' '.join(message.split()[1:])
|
split_message = message.split()
|
||||||
body = {"title": title, "status": "publish"}
|
title = "[" + split_message[0] + "] " + ' '.join(message.split()[1:])
|
||||||
|
body = {"title": title, "status": "publish"}
|
||||||
auth = aiohttp.BasicAuth(self.config["api_user"], self.config["api_token"])
|
|
||||||
|
auth = aiohttp.BasicAuth(region_config["api_user"], region_config["api_token"])
|
||||||
|
|
||||||
|
async with self.http.post(region_config["api_url"], json=body, auth=auth) as response:
|
||||||
|
if response.status < 200 or response.status > 299:
|
||||||
|
wp_posts_passed = False
|
||||||
|
|
||||||
# Send notification
|
# Send notification
|
||||||
async with self.http.post(self.config["api_url"], json=body, auth=auth) as response:
|
if self.config["send_reaction"]:
|
||||||
if self.config["send_reaction"]:
|
if wp_posts_passed is True:
|
||||||
if response.status == 201:
|
await evt.react("👍")
|
||||||
await evt.react("👍")
|
elif wp_posts_passed is False:
|
||||||
else :
|
await evt.react("👎")
|
||||||
await evt.react("👎")
|
|
||||||
self.log.debug(response.text)
|
|
||||||
|
|
||||||
# That's all, folks
|
# That's all, folks
|
|
@ -1,6 +1,6 @@
|
||||||
maubot: 0.1.0
|
maubot: 0.1.0
|
||||||
id: org.fiftyfiftyonearizona.reports.consumerwordpress
|
id: org.fiftyfiftyonearizona.reports.consumerwordpress
|
||||||
version: 1.1.0
|
version: 1.2.0
|
||||||
license: MIT
|
license: MIT
|
||||||
modules:
|
modules:
|
||||||
- consumerwordpress
|
- consumerwordpress
|
||||||
|
|
|
@ -2,4 +2,11 @@
|
||||||
|
|
||||||
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 creation of posts.
|
||||||
|
* Different users and WPJson endpoints per region.
|
||||||
|
|
||||||
|
Please see [base-config.yaml](base-config.yaml) for explanation of behavior.
|
Loading…
Add table
Add a link
Reference in a new issue