Skip to content
Snippets Groups Projects
Commit d450e323 authored by MxMarx's avatar MxMarx
Browse files

recognize '2w' as weeks

Cleaned up date parsing.

Also react with :thumbsup: regardless of is verbose is true so it's more clear to people what to react to in order to subscribe
parent 5181f85a
No related branches found
No related tags found
No related merge requests found
...@@ -205,6 +205,8 @@ class ReminderBot(Plugin): ...@@ -205,6 +205,8 @@ class ReminderBot(Plugin):
again: Is this a reminder that was rescheduled? again: Is this a reminder that was rescheduled?
agenda: Is this an agenda instead of a reminder? agenda: Is this an agenda instead of a reminder?
""" """
confirmation_event = await evt.react("\U0001F44D")
if self.config["verbose"]: if self.config["verbose"]:
action = "add this to the agenda for" if agenda else "remind" if reminder.message else "ping" action = "add this to the agenda for" if agenda else "remind" if reminder.message else "ping"
...@@ -227,8 +229,7 @@ class ReminderBot(Plugin): ...@@ -227,8 +229,7 @@ class ReminderBot(Plugin):
confirmation_event = await evt.reply(f"{msg}\n\n" confirmation_event = await evt.reply(f"{msg}\n\n"
f"(others can \U0001F44D the message above to get pinged too)") f"(others can \U0001F44D the message above to get pinged too)")
else:
confirmation_event = await evt.react("\U0001F44D")
await reminder.set_confirmation(confirmation_event) await reminder.set_confirmation(confirmation_event)
......
...@@ -125,13 +125,18 @@ class Reminder(object): ...@@ -125,13 +125,18 @@ class Reminder(object):
logger.debug(f"User {self.creator} is rate limited skipping reminder: {self.message}") logger.debug(f"User {self.creator} is rate limited skipping reminder: {self.message}")
else: else:
# Build the message with the format "(users to ping) ⬆️(link to the reminder): message text [next run] # Build the message with the format "(users to ping) ⬆️(link to the reminder): message text [next run]
# Note: ️using "⬆️" as a link seems to have trouble rendering in element for android, but "⬆" works and element on my PC still renders it as the emoji
targets = list(self.subscribed_users.values()) targets = list(self.subscribed_users.values())
link = f"https://matrix.to/#/{self.room_id}/{self.event_id}" link = f"https://matrix.to/#/{self.room_id}/{self.event_id}"
users = " ".join([(await make_pill(user_id=user_id, client=self.bot.client)) users = " ".join([(await make_pill(user_id=user_id, client=self.bot.client))
for user_id in targets]) for user_id in targets])
body = f"{users}: [⬆]({link}) {self.message}" body = f"{users}: [⬆]({link}) {self.message}"
if self.recur_every or self.cron_tab: if self.recur_every or self.cron_tab:
body += f"\n\nReminding again {self.formatted_time(user_info)}" body += f"\n\nReminding again {self.formatted_time(user_info)}." \
f" Reply `!{self.bot.base_command[0]} {self.bot.cancel_command[0]}` to stop."
# Warn the user before rate limiting happens # Warn the user before rate limiting happens
if reminder_count == self.bot.config["rate_limit"]: if reminder_count == self.bot.config["rate_limit"]:
body += f"\n\n*You've reached the rate limit " \ body += f"\n\n*You've reached the rate limit " \
......
...@@ -178,7 +178,9 @@ def parse_date(str_with_time: str, user_info: UserInfo, search_text: bool=False) ...@@ -178,7 +178,9 @@ def parse_date(str_with_time: str, user_info: UserInfo, search_text: bool=False)
# Until dateparser makes it so locales can be used in the searcher, use this to get date order # Until dateparser makes it so locales can be used in the searcher, use this to get date order
date_order = validate_locale(user_info.locale).info["date_order"] date_order = validate_locale(user_info.locale).info["date_order"]
date_str = str_with_time
# Replace "3w" with "3wk" to satisfy dateparser
str_with_time = re.sub(r"(\b\d+)\s?w\b", r"\1wk", str_with_time, count=1)
settings = {'TIMEZONE': user_info.timezone, settings = {'TIMEZONE': user_info.timezone,
'TO_TIMEZONE': 'UTC', 'TO_TIMEZONE': 'UTC',
...@@ -186,33 +188,32 @@ def parse_date(str_with_time: str, user_info: UserInfo, search_text: bool=False) ...@@ -186,33 +188,32 @@ def parse_date(str_with_time: str, user_info: UserInfo, search_text: bool=False)
'PREFER_DATES_FROM': 'future', 'PREFER_DATES_FROM': 'future',
'RETURN_AS_TIMEZONE_AWARE': True} 'RETURN_AS_TIMEZONE_AWARE': True}
if search_text: # dateparser.parse is more reliable than search_dates. If the date is at the beginning of the message,
# dateparser.parse is more reliable than search_dates. If the date is at the beginning of the message, # try dateparser.parse on the first 8 words and use the date from the longest sequence that successfully parses.
# try dateparser.parse on the first 6 words and use the date from the longest sequence that successfully parses. date = []
# If this doesn't work or the date isn't at the beginning of the string, fallback to search_dates date_str = []
date = [] for i in reversed(list(islice(re.finditer(r"\S+", str_with_time), 8))):
for i in islice(re.finditer(r"\S+", str_with_time), 6): extracted_date = dateparser.parse(str_with_time[:i.end()], locales=[user_info.locale], settings=settings)
extracted_date = dateparser.parse(str_with_time[:i.end()], locales=[user_info.locale], settings=settings) if extracted_date:
if extracted_date: date = extracted_date
date = extracted_date date_str = str_with_time[:i.end()]
date_str = str_with_time[:i.end()] break
if not date: # If the above doesn't work or the date isn't at the beginning of the string, fallback to search_dates
results = search_dates(str_with_time, languages=[user_info.locale.split('-')[0]], settings=settings) if not date:
if not results: extracted_date = search_dates(str_with_time, languages=[user_info.locale.split('-')[0]], settings=settings)
raise CommandSyntaxError("Unable to extract date from string", CommandSyntax.PARSE_DATE_EXAMPLES) if extracted_date:
date_str, date = results[0] date_str, date = extracted_date[0]
else:
date = dateparser.parse(str_with_time, locales=[user_info.locale], settings=settings) if not date:
if not date: raise CommandSyntaxError("Unable to extract date from string", CommandSyntax.PARSE_DATE_EXAMPLES)
raise CommandSyntaxError(f"The given time `{str_with_time}` is invalid.", CommandSyntax.PARSE_DATE_EXAMPLES)
# Round datetime object to the nearest second for nicer display # Round datetime object to the nearest second for nicer display
date = date.replace(microsecond=0) date = date.replace(microsecond=0)
# Disallow times in the past # Disallow times in the past
if date < datetime.now(tz=pytz.UTC): if date < datetime.now(tz=pytz.UTC):
raise CommandSyntaxError(f"Sorry, `{date}` is in the past and I don't have a time machine (yet...)") raise CommandSyntaxError(f"Sorry, `{format_time(date, user_info)}` is in the past and I don't have a time machine (yet...)")
return date, date_str return date, date_str
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment