88 lines
No EOL
3 KiB
Python
88 lines
No EOL
3 KiB
Python
from maubot import Plugin, MessageEvent
|
|
from maubot.handlers import command
|
|
|
|
# Needed for configuration
|
|
from typing import Type
|
|
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
|
|
|
import aiohttp
|
|
|
|
# Ensures a running instance gets an updated config from the Maubot interface
|
|
class Config(BaseProxyConfig):
|
|
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
|
helper.copy("command_prefix")
|
|
helper.copy("allowed_regions")
|
|
helper.copy("api_url")
|
|
helper.copy("api_user")
|
|
helper.copy("api_token")
|
|
helper.copy("send_reaction")
|
|
|
|
class ConsumerWordpress(Plugin):
|
|
# Get configuration at startup
|
|
async def start(self) -> None:
|
|
self.config.load_and_update()
|
|
|
|
# Get config
|
|
@classmethod
|
|
def get_config_class(cls) -> Type[BaseProxyConfig]:
|
|
return Config
|
|
|
|
# Get !command_name setting from config to register it
|
|
def get_command_name(self) -> str:
|
|
return self.config["command_prefix"]
|
|
|
|
# Checks if a sender if allowed to send for a particular region
|
|
def validateSender(self, region: str, sender: str):
|
|
# Check that config value exists
|
|
if not self.config["allowed_regions"]: return False
|
|
# Check that region is allowed
|
|
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
|
|
if not sender in self.config["allowed_regions"][region]: return False
|
|
return True
|
|
|
|
# Does the necesary config checks for the given event
|
|
# Returns list of regions to process (strings)
|
|
# Currently just the specified region and "__global__"
|
|
def validateReport(self, evt: MessageEvent, message: str):
|
|
# Split command (minus !command_name) into tokens
|
|
tokens = message.split()
|
|
region = tokens[0].lower()
|
|
|
|
# Each command must have a state/territory designation and a message
|
|
if len(tokens) < 2: return None
|
|
|
|
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")
|
|
@command.argument("message", pass_raw=True)
|
|
async def report(self, evt: MessageEvent, message: str) -> None:
|
|
# If all have passed
|
|
ntfy_posts_passed = None
|
|
|
|
# Iterate through each endpoint that the message should be pushed to
|
|
# (if any)
|
|
region = self.validateReport(evt, message)
|
|
if region is None: return
|
|
|
|
# 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 |