Skip to content

Commit

Permalink
fixed datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
noahbraunfeld committed Aug 5, 2019
1 parent a4d2d94 commit c6272a7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
17 changes: 7 additions & 10 deletions BlockCreator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def date_to_words(year: str, month: str, day: str) -> tuple:

class BlockBuilder:
"""A simple python script for creating slack blocks easily and quickly"""
time = datetime.now() # Gets the current time

def __init__(self, block: list = []):
"""Creates a BlockBuilder object"""
Expand Down Expand Up @@ -201,11 +200,7 @@ def img_section(self,

return BlockBuilder(block=self.block)

def datepicker(self,
text: str = "text",
year: int = time.year,
month: int = time.month,
day: int = time.day):
def datepicker(self, text: str = "text", year=None, month=None, day=None):
"""
creates a datepicker element (default today's date) with a section
Expand All @@ -216,8 +211,9 @@ def datepicker(self,
:usage: datepicker(text="section text") # * LEAVING THE REST BLANK TO GET TODAY'S DATE AS DEFAULT
"""
assert month <= 12
assert day <= 31
time = datetime.now()
# assert int(month) <= 12
# assert int(day) <= 31
self.block.append({
"type": "section",
"text": {
Expand All @@ -226,7 +222,8 @@ def datepicker(self,
},
"accessory": {
"type": "datepicker",
"initial_date": f"{year}-{month}-{day}",
"initial_date": f"{time.year}-{time.month}-{time.day}",
#"initial_date": "2019-8-5",
"placeholder": {
"type": "plain_text",
"text": "Select a date",
Expand All @@ -250,7 +247,7 @@ def dropdown(self,
:usage: dropdown(section_text="section text", button_text="dropdown menu text", options=(["option_1", "value_1"],...,["option_n", "value_n"]))
"""
assert len(options) > 0
# assert len(options) > 0

builder = {
"type": "section",
Expand Down
2 changes: 1 addition & 1 deletion Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def parse_date(date: str) -> tuple:
"""
Converts date-string into a tuple, which contains an integer and a split date
:param date: A date-string in the format 'YYYY-MM-DD'
:param date: A date-string in the format `YYYY-MM-DD`\n
:rtype: tuple of integer date first, then the date array
"""
split_date = date.split('-')
Expand Down
48 changes: 22 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def handle_message(event_data):
logging.debug(pformat(event_data)) # * DEBUG

message = event_data["event"] # gets event payload
user = message.get("user") # Gets user id of user or None if not a user

if message.get("user"): # Makes sure that message is not sent by a bot
user = message.get("user") # Gets user id of user
if user: # Makes sure that message is not sent by a bot

channel = message.get(
"channel") # Gets channel that message was sent in
Expand All @@ -70,15 +70,12 @@ def handle_message(event_data):
name_value=(("Next", "yes0"), ("Cancel",
"no0"))).to_block() # Block

client.api_call(api_method="chat.postMessage",
json={
"channel": channel,
"blocks": block
})
client.chat_postMessage(channel=channel, blocks=block)
block = None
if message.get(
"text"
) == "view on call": # See who is on call on whichever dates

db.push_to_collection('scheduled_users')

block = BlockBuilder(
Expand Down Expand Up @@ -106,12 +103,8 @@ def handle_message(event_data):
'img', user_images[users['user_id']],
'_error displaying user image_'
), ('text',
f'<@{users["user_id"]}>. _Contact them if you have any concerns_'
f'<@{users["user_id"]}> is on call from the *{date_to_words(start_date[0], start_date[1], start_date[2])[0]}* to the *{date_to_words(end_date[0], end_date[1], end_date[2])[0]}*.\n_Contact them if you have any concerns_'
)))
block.section(
text=
f'>from the *{date_to_words(start_date[0], start_date[1], start_date[2])[0]}* to the *{date_to_words(end_date[0], end_date[1], end_date[2])[0]}*'
).divider()

block = block.to_block()
logging.debug(pformat(user_images))
Expand All @@ -123,8 +116,10 @@ def handle_message(event_data):
})

block = None

if message.get(
"text") == "reset on call": # Remove user from on call list

db.database['scheduled_users'].update_one(
filter={'user_id': user},
update={'$set': {
Expand All @@ -136,7 +131,9 @@ def handle_message(event_data):
user=user,
channel=channel,
text=f'Sucessfully removed you from on call list')

if message.get("text") == "help me schedule": # Help Command

block = BlockBuilder([]).section(
text='_Beep Boop_. I am a bot who schedules things!'
).divider().section(
Expand All @@ -155,19 +152,18 @@ def handle_message(event_data):
block = None


@slack_event_adapter.on(event="message.im")
def handle_dm(event_data):
logging.debug(pformat(event_data))
event = event_data.get("event")
user_id = event.get("user")
if user_id:
im_channel = client.im_open(user=user_id)["channel"]["id"]
channel = event.get('channel')
text = event.get('text')
logging.debug(pformat(im_channel))
print(pformat(im_channel))

client.chat_postMessage(channel=im_channel, text="hello to you!")
# @slack_event_adapter.on(event="message.im")
# def handle_dm(event_data):
# logging.debug(pformat(event_data))
# event = event_data.get("event")
# user_id = event.get("user")
# if user_id:
# im_channel = client.im_open(user=user_id)["channel"]["id"]
# channel = event.get('channel')
# text = event.get('text')
# logging.debug(pformat(im_channel))
# print(pformat(im_channel))
# client.chat_postMessage(channel=im_channel, text="hello to you!")


@app.route("/slack/interactive", methods=['GET', 'POST'])
Expand Down Expand Up @@ -198,7 +194,7 @@ def handle_interaction():
logging.debug(pformat(req))
global datepickers

if actions[0].get('type') == 'button':
if actions[0].get('type') == 'button': # Uses a dict-cache to save values of previous datepicker
handle_button_click(value=actions[0].get('value'),
user=user['id'],
channel=channel,
Expand Down

0 comments on commit c6272a7

Please sign in to comment.