Cleanup parsing.py
This commit is contained in:
parent
b8493440bd
commit
bc0f71d5be
1 changed files with 42 additions and 40 deletions
|
@ -1,29 +1,28 @@
|
|||
import random, re
|
||||
from gacha_response import gacha_response
|
||||
|
||||
|
||||
def parse_notification(notification,client):
|
||||
'''Oarses any notifications received by the bot and sends any commands to
|
||||
gacha_response()'''
|
||||
|
||||
# We get the type of notification to filter the ones that we actually want to parse
|
||||
# We get the type of notification to filter the ones that we actually want
|
||||
# to parse
|
||||
|
||||
notif_type = notification.get("type")
|
||||
if not((notif_type == "mention") or (notif_type == "reply")):
|
||||
|
||||
return # Ignora todo lo que no sea una mención
|
||||
|
||||
# We want the visibility to be related to the type that was received (so if people don't want to dump a bunch of notes on home they don't have to)
|
||||
if not notif_type in ('mention', 'reply'):
|
||||
return # Ignore anything that isn't a mention
|
||||
|
||||
# We want the visibility to be related to the type that was received (so if
|
||||
# people don't want to dump a bunch of notes on home they don't have to)
|
||||
visibility = notification["note"]["visibility"]
|
||||
if visibility == "specified":
|
||||
visibility = "specified"
|
||||
else:
|
||||
if visibility != "specified":
|
||||
visibility = "home"
|
||||
|
||||
|
||||
|
||||
# Get the full Activitypub ID of the user
|
||||
user = notification.get("user", {})
|
||||
username = user.get("username", "unknown")
|
||||
host = user.get("host")
|
||||
# Local users may not have a hostname attached
|
||||
full_user = f"@{username}" if not host else f"@{username}@{host}"
|
||||
|
||||
note_obj = notification.get("note", {})
|
||||
|
@ -32,38 +31,41 @@ def parse_notification(notification,client):
|
|||
|
||||
note = note_text.strip().lower() if note_text else ""
|
||||
|
||||
# Will want to move bot name to a config file or find a way to derermine
|
||||
# AP ID at runtime
|
||||
waifumelon_variants = [
|
||||
"@waifumelon", # simple mention
|
||||
"@waifumelon@mai.waifuism.life", # fully qualified
|
||||
]
|
||||
|
||||
if any(variant in note for variant in waifumelon_variants):
|
||||
# Find command and arguments after the mention
|
||||
# Removes all mentions (regex = mentions that start with @ and may contain @domain)
|
||||
cleaned_text = re.sub(r"@\w+(?:@\S+)?", "", note).strip()
|
||||
parts = cleaned_text.split()
|
||||
print(cleaned_text)
|
||||
print(parts)
|
||||
# Make sure the notification text explicitly mentions the bot
|
||||
if not any(variant in note for variant in waifumelon_variants):
|
||||
return
|
||||
|
||||
command = parts[0].lower() if parts else None
|
||||
arguments = parts[1:] if len(parts) > 1 else []
|
||||
# Find command and arguments after the mention
|
||||
# Removes all mentions (regex = mentions that start with @ and may contain @domain)
|
||||
cleaned_text = re.sub(r"@\w+(?:@\S+)?", "", note).strip()
|
||||
parts = cleaned_text.split()
|
||||
|
||||
command = parts[0].lower() if parts else None
|
||||
arguments = parts[1:] if len(parts) > 1 else []
|
||||
|
||||
# TODO: move response generation to a different function
|
||||
response = gacha_response(command.lower(),full_user, arguments, note_obj)
|
||||
if not response:
|
||||
return
|
||||
|
||||
if isinstance(response, str):
|
||||
client.notes_create(
|
||||
text=response,
|
||||
reply_id=note_id,
|
||||
visibility=visibility
|
||||
)
|
||||
else:
|
||||
command = None
|
||||
if command:
|
||||
response = gacha_response(command.lower(),full_user, arguments, note_obj)
|
||||
if response:
|
||||
if type(response) == str:
|
||||
client.notes_create(
|
||||
text=response,
|
||||
reply_id=note_id,
|
||||
visibility=visibility
|
||||
)
|
||||
else:
|
||||
client.notes_create(
|
||||
text=response[0],
|
||||
reply_id=note_id,
|
||||
visibility=visibility,
|
||||
file_ids=response[1]
|
||||
#visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately
|
||||
|
||||
)
|
||||
client.notes_create(
|
||||
text=response[0],
|
||||
reply_id=note_id,
|
||||
visibility=visibility,
|
||||
file_ids=response[1]
|
||||
#visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue