Skip to content

Commit

Permalink
docs: Add basic documentation to handlers and utils
Browse files Browse the repository at this point in the history
* Trata de #4
  • Loading branch information
jvfe committed Jan 15, 2021
1 parent 3e52515 commit c8761ee
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
23 changes: 21 additions & 2 deletions bpb/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@


def start(update, context):

"""
Handle sending the start message
"""
context.bot.send_message(
chat_id=update.effective_chat.id,
text=START,
)


def get_meeting(update, context):

"""
Get current meetings set on context
"""
try:
datetime_obj = getattr(context.bot, "next_meeting")

Expand All @@ -66,6 +70,9 @@ def get_meeting(update, context):


def set_meeting(update, context):
"""
Set meetings and alarms
"""

try:
chat_id = update.message.chat_id
Expand Down Expand Up @@ -99,6 +106,9 @@ def set_meeting(update, context):


def clear_meetings(update, context):
"""
Clear meetings and alarms
"""

try:

Expand All @@ -117,13 +127,19 @@ def clear_meetings(update, context):


def links(update, context):
"""
Get important links
"""

context.bot.send_message(
chat_id=update.effective_chat.id, text=LINKS_IMPORTANTES, parse_mode="Markdown"
)


def welcome(update, context):
"""
Welcome a new group member
"""

new_member = update.message.new_chat_members[0]

Expand All @@ -135,6 +151,9 @@ def welcome(update, context):


def get_handlers():
"""
Aggregate and return handlers
"""

handlers = [
MessageHandler(Filters.status_update.new_chat_members, welcome),
Expand Down
53 changes: 52 additions & 1 deletion bpb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@


def prettify_date(dt_object):
"""
Pretty-format a datetime object
Args:
dt_object(datetime.datetime): A datetime object.
Returns:
str: A pretty-formatted date.
"""

return dt_object.strftime("%A %Hh%M, %d %b %Y")


def parse_date(date_args):
"""
Parse argument into a datetime object
Args:
date_args: A list or datetime object to be parsed.
Returns:
tuple: A tuple of a string-date and a datetime object.
"""

if isinstance(date_args, (list, tuple)):
date_args = " ".join(date_args)
elif isinstance(date_args, datetime):
Expand All @@ -21,6 +40,16 @@ def parse_date(date_args):


def get_meeting_range(date_args):
"""
Parse argument into datetimes and datetime range.
Args:
date_args: A list or datetime object to be parsed.
Returns:
tuple: A tuple of a string-date, a datetime object and
a list of tuple representing the next meetings.
"""

# Get message meeting and following ones
parsed_date, datetime_obj = parse_date(date_args)
Expand All @@ -35,18 +64,40 @@ def get_meeting_range(date_args):


def add_timedeltas(dt_object):
"""
Localize and buffer meeting datetime object
Adds a timedelta of +3 to localize to GMT-3 and
a timedelta of -30min for the reminder.
Args:
dt_object(datetime.datetime): A datetime object.
Returns:
datetime.datetime: A datetime object localized and buffered.
"""

return dt_object + timedelta(hours=3) - timedelta(minutes=30)


def generate_reminders(next_meetings):
"""
Generate list of datetimes for the alarms
Args:
next_meetings(list): The list made by get_meeting_range.
Returns:
list: A list of datetime objects corresponding to alarm times.
"""
alarm_times = [add_timedeltas(meeting[1]) for meeting in next_meetings]

return alarm_times


def alarm(context):

"""
Handle sending the alarm message
"""
job = context.job
context.bot.send_message(job.context, text=LEMBRETE)

0 comments on commit c8761ee

Please sign in to comment.