diff --git a/base-config.yaml b/base-config.yaml index 07fedc5..2068f68 100644 --- a/base-config.yaml +++ b/base-config.yaml @@ -5,25 +5,20 @@ command_prefix: report # Which regions the bot should pay attention to # Additionally, optionally whitelist specific accounts for each region allowed_regions: + __global__: + - "@reports:fiftyfiftyonearizona.org" foo: - - "@reports:fiftyfiftyonerarizona.org" + - "@reports:fiftyfiftyonearizona.org" bar: - "@reports:example.org" - "@reports:example.com" buzz: aaa: -# Bot-specific configurations per region -region_configs: - arizona: - # Where and how the plugin will send the data to - server_url: "https://ntfy.sh" - server_topic: "changeme" - - # If the plugin should use a username and password to send notifications - server_use_authentication: true - server_username: "no" - server_password: "way" +# 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 the notification was successful diff --git a/consumerntfy.py b/consumerwordpress.py similarity index 57% rename from consumerntfy.py rename to consumerwordpress.py index 822d18d..9e5a5de 100644 --- a/consumerntfy.py +++ b/consumerwordpress.py @@ -12,10 +12,12 @@ class Config(BaseProxyConfig): def do_update(self, helper: ConfigUpdateHelper) -> None: helper.copy("command_prefix") helper.copy("allowed_regions") - helper.copy("region_configs") + helper.copy("api_url") + helper.copy("api_user") + helper.copy("api_token") helper.copy("send_reaction") -class ConsumerNTFY(Plugin): +class ConsumerWordpress(Plugin): # Get configuration at startup async def start(self) -> None: self.config.load_and_update() @@ -34,7 +36,7 @@ class ConsumerNTFY(Plugin): # Check that config value exists if not self.config["allowed_regions"]: return False # Check that region is allowed - if not self.config["allowed_regions"][region]: return False + if not region in self.config["allowed_regions"]: 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 @@ -52,17 +54,8 @@ class ConsumerNTFY(Plugin): # Each command must have a state/territory designation and a message if len(tokens) < 2: return None - self.log.debug(region) - - # This is a list of regions to process for this specific message - # This is only used to consider __global__ - 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 (self.validateSender("__global__", evt.sender)) or (self.validateSender(region, evt.sender)): + return [region] # What gets called when !command_name message is sent @command.new(name=get_command_name, help="Report Something") @@ -73,35 +66,23 @@ class ConsumerNTFY(Plugin): # Iterate through each endpoint that the message should be pushed to # (if any) - for region in self.validateReport(evt, message): - # Detect no regions in reaction - if ntfy_posts_passed is None: ntfy_posts_passed = True - - # Grab region-specific conrfig - region_config = self.config["region_configs"][region] - - # Create notification text - split_message = message.split() - text = "[" + split_message[0] + "] " + ' '.join(message.split()[1:]) - - # Build URL - url = region_config["server_url"] + "/" + region_config["server_topic"] - - # Consider authentication - authentication = None - if region_config["server_use_authentication"]: - authentication = aiohttp.BasicAuth(region_config["server_username"], region_config["server_password"]) - - # Send notification - async with self.http.post(url, data=text, auth=authentication) as response: - if not response.status == 200: - ntfy_posts_passed = False + region = self.validateReport(evt, message) + if region is None: return - # Send reaction based on successful or failedPOSTs - if self.config["send_reaction"]: - if ntfy_posts_passed is True: - await evt.react("👍") - elif ntfy_posts_passed is False: - await evt.react("👎") + # Create post with only a title + split_message = message.split() + title = "[" + split_message[0] + "] " + ' '.join(message.split()[1:]) + body = {"title": title, "status": "publish"} + + auth = aiohttp.BasicAuth(self.config["api_user"], self.config["api_token"]) + + # Send notification + async with self.http.post(self.config["api_url"], json=body, auth=auth) as response: + if self.config["send_reaction"]: + if response.status == 201: + await evt.react("👍") + else : + await evt.react("👎") + self.log.debug(response.text) # That's all, folks \ No newline at end of file diff --git a/maubot.yaml b/maubot.yaml index 899e4fe..da68109 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,10 +1,10 @@ maubot: 0.1.0 -id: org.fiftyfiftyonearizona.reports.consumerntfy +id: org.fiftyfiftyonearizona.reports.consumerwordpress version: 1.1.0 license: MIT modules: - - consumerntfy -main_class: ConsumerNTFY + - consumerwordpress +main_class: ConsumerWordpress config: true extra_files: - base-config.yaml \ No newline at end of file diff --git a/readme.md b/readme.md index 4b621b7..c94aefa 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# FiftyFiftyOneArizona's Matrix-Based Reporting System, NTFY Consumer +# FiftyFiftyOneArizona's Matrix-Based Reporting System, Wordpress Consumer Please see https://git.fiftyfiftyonearizona.org/webmaster/matrix-report-documentation