Change to producer-stopice.net
This commit is contained in:
parent
24f6f25506
commit
13d6846f5f
5 changed files with 39691 additions and 136 deletions
|
@ -2,29 +2,13 @@
|
|||
# DO NOT CHANGE THIS UNLESS YOU KNOW WHAT YOU ARE DOING
|
||||
command_prefix: report
|
||||
|
||||
# Which regions the bot should pay attention to
|
||||
# Additionally, optionally whitelist specific accounts for each region
|
||||
allowed_regions:
|
||||
foo:
|
||||
- "@reports:fiftyfiftyonerarizona.org"
|
||||
bar:
|
||||
- "@reports:example.org"
|
||||
- "@reports:example.com"
|
||||
buzz:
|
||||
aaa:
|
||||
# How many seconds to wait between refreshes
|
||||
refresh_delay: 60
|
||||
|
||||
# Bot-specific configurations per region
|
||||
region_configs:
|
||||
# Which rooms to send to, per region
|
||||
send_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"
|
||||
|
||||
# If this bot should send a reaction to the message
|
||||
# if the notification was successful
|
||||
send_reaction: true
|
||||
- "!example:example.com"
|
||||
- "!example2:example.com"
|
||||
california:
|
||||
- "!example:example.com"
|
107
consumerntfy.py
107
consumerntfy.py
|
@ -1,107 +0,0 @@
|
|||
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("region_configs")
|
||||
helper.copy("send_reaction")
|
||||
|
||||
class ConsumerNTFY(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 self.config["allowed_regions"][region]: 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
|
||||
|
||||
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
|
||||
|
||||
# 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)
|
||||
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
|
||||
|
||||
# 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
|
12
maubot.yaml
12
maubot.yaml
|
@ -1,10 +1,16 @@
|
|||
maubot: 0.1.0
|
||||
id: org.fiftyfiftyonearizona.reports.consumerntfy
|
||||
id: org.fiftyfiftyonearizona.reports.producerstopicenet
|
||||
version: 1.1.0
|
||||
license: MIT
|
||||
modules:
|
||||
- consumerntfy
|
||||
main_class: ConsumerNTFY
|
||||
- producerstopicenet
|
||||
dependencies:
|
||||
- beautifulsoup4>=4.13.4
|
||||
- regex>=2024.11.6
|
||||
main_class: ProducerStopIceNet
|
||||
config: true
|
||||
database: true
|
||||
database_type: asyncpg
|
||||
webapp: true
|
||||
extra_files:
|
||||
- base-config.yaml
|
39668
producerstopicenet.py
Normal file
39668
producerstopicenet.py
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,5 +1,9 @@
|
|||
# FiftyFiftyOneArizona's Matrix-Based Reporting System, NTFY Consumer
|
||||
# FiftyFiftyOneArizona's Matrix-Based Reporting System, stopice.net producer
|
||||
|
||||
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 parses the HTML of the [stopice.net](https://stopice.net) website to nab its reports. It supports a custom refresh delay, sending to multiple rooms, and sending to rooms based on region designation.
|
||||
|
||||
Depends on `beautifulsoup4` and `regex` packages.
|
||||
|
||||
`producerstopicenet.py` is near 40,000 lines long because I couldn't figure out how to get python to work with multiple files and I have a mapping between ZIP codes and two-letter state designations.
|
Loading…
Add table
Add a link
Reference in a new issue