Wordpress consumer
This commit is contained in:
parent
24f6f25506
commit
2b6dafbd37
4 changed files with 35 additions and 59 deletions
|
@ -5,25 +5,20 @@ 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
|
# Additionally, optionally whitelist specific accounts for each region
|
||||||
allowed_regions:
|
allowed_regions:
|
||||||
|
__global__:
|
||||||
|
- "@reports:fiftyfiftyonearizona.org"
|
||||||
foo:
|
foo:
|
||||||
- "@reports:fiftyfiftyonerarizona.org"
|
- "@reports:fiftyfiftyonearizona.org"
|
||||||
bar:
|
bar:
|
||||||
- "@reports:example.org"
|
- "@reports:example.org"
|
||||||
- "@reports:example.com"
|
- "@reports:example.com"
|
||||||
buzz:
|
buzz:
|
||||||
aaa:
|
aaa:
|
||||||
|
|
||||||
# Bot-specific configurations per region
|
# Bot-specific configurations
|
||||||
region_configs:
|
api_url: "https://example.org/wp-json/wp/v2/yourposttype"
|
||||||
arizona:
|
api_user: "youruser"
|
||||||
# Where and how the plugin will send the data to
|
api_token: "your token"
|
||||||
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"
|
|
||||||
|
|
||||||
# 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
|
# if the notification was successful
|
||||||
|
|
|
@ -12,10 +12,12 @@ 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("region_configs")
|
helper.copy("api_url")
|
||||||
|
helper.copy("api_user")
|
||||||
|
helper.copy("api_token")
|
||||||
helper.copy("send_reaction")
|
helper.copy("send_reaction")
|
||||||
|
|
||||||
class ConsumerNTFY(Plugin):
|
class ConsumerWordpress(Plugin):
|
||||||
# Get configuration at startup
|
# Get configuration at startup
|
||||||
async def start(self) -> None:
|
async def start(self) -> None:
|
||||||
self.config.load_and_update()
|
self.config.load_and_update()
|
||||||
|
@ -34,7 +36,7 @@ class ConsumerNTFY(Plugin):
|
||||||
# Check that config value exists
|
# Check that config value exists
|
||||||
if not self.config["allowed_regions"]: return False
|
if not self.config["allowed_regions"]: return False
|
||||||
# Check that region is allowed
|
# 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
|
# All senders allowed for this region
|
||||||
if len(self.config["allowed_regions"][region]) == 0: return True
|
if len(self.config["allowed_regions"][region]) == 0: return True
|
||||||
# Check that sender is allowed for region
|
# 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
|
# Each command must have a state/territory designation and a message
|
||||||
if len(tokens) < 2: return None
|
if len(tokens) < 2: return None
|
||||||
|
|
||||||
self.log.debug(region)
|
if (self.validateSender("__global__", evt.sender)) or (self.validateSender(region, evt.sender)):
|
||||||
|
return [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
|
|
||||||
|
|
||||||
# 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,35 +66,23 @@ 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):
|
region = self.validateReport(evt, message)
|
||||||
# Detect no regions in reaction
|
if region is None: return
|
||||||
if ntfy_posts_passed is None: ntfy_posts_passed = True
|
|
||||||
|
|
||||||
# Grab region-specific conrfig
|
# Create post with only a title
|
||||||
region_config = self.config["region_configs"][region]
|
split_message = message.split()
|
||||||
|
title = "[" + split_message[0] + "] " + ' '.join(message.split()[1:])
|
||||||
|
body = {"title": title, "status": "publish"}
|
||||||
|
|
||||||
# Create notification text
|
auth = aiohttp.BasicAuth(self.config["api_user"], self.config["api_token"])
|
||||||
split_message = message.split()
|
|
||||||
text = "[" + split_message[0] + "] " + ' '.join(message.split()[1:])
|
|
||||||
|
|
||||||
# Build URL
|
# Send notification
|
||||||
url = region_config["server_url"] + "/" + region_config["server_topic"]
|
async with self.http.post(self.config["api_url"], json=body, auth=auth) as response:
|
||||||
|
if self.config["send_reaction"]:
|
||||||
# Consider authentication
|
if response.status == 201:
|
||||||
authentication = None
|
await evt.react("👍")
|
||||||
if region_config["server_use_authentication"]:
|
else :
|
||||||
authentication = aiohttp.BasicAuth(region_config["server_username"], region_config["server_password"])
|
await evt.react("👎")
|
||||||
|
self.log.debug(response.text)
|
||||||
# Send notification
|
|
||||||
async with self.http.post(url, data=text, auth=authentication) as response:
|
|
||||||
if not response.status == 200:
|
|
||||||
ntfy_posts_passed = False
|
|
||||||
|
|
||||||
# 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("👎")
|
|
||||||
|
|
||||||
# That's all, folks
|
# That's all, folks
|
|
@ -1,10 +1,10 @@
|
||||||
maubot: 0.1.0
|
maubot: 0.1.0
|
||||||
id: org.fiftyfiftyonearizona.reports.consumerntfy
|
id: org.fiftyfiftyonearizona.reports.consumerwordpress
|
||||||
version: 1.1.0
|
version: 1.1.0
|
||||||
license: MIT
|
license: MIT
|
||||||
modules:
|
modules:
|
||||||
- consumerntfy
|
- consumerwordpress
|
||||||
main_class: ConsumerNTFY
|
main_class: ConsumerWordpress
|
||||||
config: true
|
config: true
|
||||||
extra_files:
|
extra_files:
|
||||||
- base-config.yaml
|
- base-config.yaml
|
|
@ -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
|
Please see https://git.fiftyfiftyonearizona.org/webmaster/matrix-report-documentation
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue