Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
0442abd2ed |
3 changed files with 88 additions and 70 deletions
|
@ -2,32 +2,29 @@
|
||||||
# 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
|
||||||
|
|
||||||
# Whether or not the bot should pay attention to only specific state/territory designators
|
# Which reigons the bot should pay attention to
|
||||||
# This bot will include the designator in the pushed notificaion if this is disabled,
|
# Additionally, optionally whitelist specific accounts for each reigon
|
||||||
# or if the allowed_locations contains multiple strings
|
allowed_reigons:
|
||||||
allowed_locations_enabled: false
|
|
||||||
allowed_locations:
|
|
||||||
- arizona
|
|
||||||
- california
|
|
||||||
|
|
||||||
# Which accounts should be paid attention to on a state/territory-by-state/territory basis.
|
|
||||||
# If the state/territory is not in allowed_senders, then the bot will allow all senders
|
|
||||||
# Otherwise only listed senders
|
|
||||||
allowed_senders:
|
|
||||||
foo:
|
foo:
|
||||||
- "@reports:fiftyfiftyonerarizona.org"
|
- "@reports:fiftyfiftyonerarizona.org"
|
||||||
bar:
|
bar:
|
||||||
- "@reports:example.org"
|
- "@reports:example.org"
|
||||||
- "@reports:example.com"
|
- "@reports:example.com"
|
||||||
|
buzz:
|
||||||
|
aaa:
|
||||||
|
|
||||||
# Where and how the plugin will send the data to
|
# Bot-specific configurations per reigon
|
||||||
server_url: "https://ntfy.sh"
|
reigon_configs:
|
||||||
server_topic: "changeMe"
|
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
|
# If the plugin should use a username and password to send notifications
|
||||||
server_use_authentication: false
|
server_use_authentication: true
|
||||||
server_username: "foo"
|
server_username: "no"
|
||||||
server_password: "bar"
|
server_password: "way"
|
||||||
|
|
||||||
# If this bot should send a reaction to the message if the notification was successful
|
# If this bot should send a reaction to the message
|
||||||
|
# if the notification was successful
|
||||||
send_reaction: true
|
send_reaction: true
|
119
consumerntfy.py
119
consumerntfy.py
|
@ -11,15 +11,8 @@ import aiohttp
|
||||||
class Config(BaseProxyConfig):
|
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_locations_enabled")
|
helper.copy("allowed_reigons")
|
||||||
helper.copy("allowed_locations")
|
helper.copy("reigon_configs")
|
||||||
helper.copy("allowed_senders")
|
|
||||||
helper.copy("server_url")
|
|
||||||
helper.copy("server_topic")
|
|
||||||
helper.copy("command_prefix")
|
|
||||||
helper.copy("server_use_authentication")
|
|
||||||
helper.copy("server_username")
|
|
||||||
helper.copy("server_password")
|
|
||||||
helper.copy("send_reaction")
|
helper.copy("send_reaction")
|
||||||
|
|
||||||
class ConsumerNTFY(Plugin):
|
class ConsumerNTFY(Plugin):
|
||||||
|
@ -36,51 +29,79 @@ class ConsumerNTFY(Plugin):
|
||||||
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 reigon
|
||||||
|
def validateSender(self, reigon: str, sender: str):
|
||||||
|
# Check that config value exists
|
||||||
|
if not self.config["allowed_reigons"]: return False
|
||||||
|
# Check that reigon is allowed
|
||||||
|
if not self.config["allowed_reigons"][reigon]: return False
|
||||||
|
# All senders allowed for this reigon
|
||||||
|
if len(self.config["allowed_reigons"][reigon]) == 0: return True
|
||||||
|
# Check that sender is allowed for reigon
|
||||||
|
if not sender in self.config["allowed_reigons"][reigon]: return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Does the necesary config checks for the given event
|
||||||
|
# Returns list of reigons to process (strings)
|
||||||
|
# Currently just the specified reigon and "__global__"
|
||||||
|
def validateReport(self, evt: MessageEvent, message: str):
|
||||||
|
# Split command (minus !command_name) into tokens
|
||||||
|
tokens = message.split()
|
||||||
|
reigon = tokens[0].lower()
|
||||||
|
|
||||||
|
# Each command must have a state/territory designation and a message
|
||||||
|
if len(tokens) < 2: return None
|
||||||
|
|
||||||
|
self.log.debug(reigon)
|
||||||
|
|
||||||
|
# This is a list of reigons to process for this specific message
|
||||||
|
# This is only used to consider __global__
|
||||||
|
reigons_to_process = []
|
||||||
|
if (self.validateSender("__global__", evt.sender)):
|
||||||
|
reigons_to_process.append("__global__")
|
||||||
|
if (self.validateSender(reigon, evt.sender)):
|
||||||
|
reigons_to_process.append(reigon)
|
||||||
|
|
||||||
|
return reigons_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")
|
||||||
@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:
|
||||||
# Split command (minus !command_name) into tokens
|
# If all have passed
|
||||||
tokens = message.split()
|
ntfy_posts_passed = None
|
||||||
st_desig = tokens[0].lower()
|
|
||||||
|
|
||||||
# Each command must have a state/territory designation and a message
|
|
||||||
if len(tokens) < 2: return
|
|
||||||
|
|
||||||
# Check locations whitelist
|
|
||||||
if self.config["allowed_locations_enabled"]:
|
|
||||||
if not tokens[0].lower() in self.config["allowed_locations"]:
|
|
||||||
return
|
|
||||||
|
|
||||||
# Check allowed senders
|
|
||||||
allowed_senders = self.config["allowed_senders"]
|
|
||||||
|
|
||||||
print(evt.sender)
|
|
||||||
|
|
||||||
if st_desig in self.config["allowed_senders"]:
|
|
||||||
print(f"Allowed senders for {st_desig}: {self.config['allowed_senders'].get(st_desig, [])}")
|
|
||||||
if not evt.sender in self.config['allowed_senders'].get(st_desig, []):
|
|
||||||
return
|
|
||||||
|
|
||||||
# Sending notification to NTFY
|
|
||||||
|
|
||||||
url = self.config["server_url"] + "/" + self.config["server_topic"]
|
|
||||||
body = ' '.join(tokens[1:])
|
|
||||||
if len(self.config["allowed_locations"]) != 1:
|
|
||||||
body = tokens[0] + ": " + body
|
|
||||||
|
|
||||||
# Consider authentication
|
|
||||||
authentication = None
|
|
||||||
if self.config["server_use_authentication"]:
|
|
||||||
authentication = aiohttp.BasicAuth(self.config["server_username"], self.config["server_password"])
|
|
||||||
|
|
||||||
# Send notification
|
|
||||||
for trash in range(3): # Try to send 3 times
|
|
||||||
async with self.http.post(url, data=body, auth=authentication) as response:
|
|
||||||
if response.status == 200:
|
|
||||||
await evt.react("👍")
|
|
||||||
return
|
|
||||||
|
|
||||||
|
# Iterate through each endpoint that the message should be pushed to
|
||||||
|
# (if any)
|
||||||
|
for reigon in self.validateReport(evt, message):
|
||||||
|
# Detect no reigons in reaction
|
||||||
|
if ntfy_posts_passed is None: ntfy_posts_passed = True
|
||||||
|
|
||||||
|
# Grab reigon-specific conrfig
|
||||||
|
reigon_config = self.config["reigon_configs"][reigon]
|
||||||
|
|
||||||
|
# Create notification text
|
||||||
|
split_message = message.split()
|
||||||
|
text = "[" + split_message[0] + "] " + ' '.join(message.split()[1:])
|
||||||
|
|
||||||
|
# Build URL
|
||||||
|
url = reigon_config["server_url"] + "/" + reigon_config["server_topic"]
|
||||||
|
|
||||||
|
# Consider authentication
|
||||||
|
authentication = None
|
||||||
|
if reigon_config["server_use_authentication"]:
|
||||||
|
authentication = aiohttp.BasicAuth(reigon_config["server_username"], reigon_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
|
# That's all, folks
|
|
@ -1,6 +1,6 @@
|
||||||
maubot: 0.1.0
|
maubot: 0.1.0
|
||||||
id: org.fiftyfiftyonearizona.reports.consumerntfy
|
id: org.fiftyfiftyonearizona.reports.consumerntfy
|
||||||
version: 1.0.0
|
version: 1.1.0
|
||||||
license: MIT
|
license: MIT
|
||||||
modules:
|
modules:
|
||||||
- consumerntfy
|
- consumerntfy
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue