Last active
September 30, 2020 15:35
-
-
Save adboio/231081db41bf02bbd42231af280be6f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@staticmethod | |
def required_slots(tracker: Tracker) -> List[Text]: | |
# this should run as soon as the form starts | |
if not tracker.get_slot('time'): | |
return ["time", "call_hhmmss"] | |
# this should happen every other time | |
else: | |
t = None # initialize t as None | |
t_str = tracker.get_slot('time') # get the duckling time string from slot | |
try: | |
# try to convert to date object | |
t = dt.strptime(t_str, "%Y-%m-%dT%H:%M:%S.%f%z") | |
except Exception as e: | |
SlotSet("time", None) | |
return ["time", "call_hhmmss"] | |
# if we got here, we got a real date object | |
if t.hour == 0 and t.minute == 0 and t.second == 0: | |
# here, we know the user said just a date rather than a datetime | |
# set call_date | |
SlotSet("call_date", t.strftime("%Y-%m-%d")) | |
# leave call_hhmmss null | |
# leave call_time null | |
# return this so the user is asked for the specific time | |
return ["time", "call_hhmmss"] | |
else: | |
# here, we know it was a valid date+time, so we're good | |
print("Found entirely valid date") | |
# first, check if call_date was already set | |
if tracker.get_slot('call_date'): | |
print("Call date was already set") | |
# if we already have a date, just append the new time, | |
# and save that to call_time | |
call_date = tracker.get_slot("call_date") | |
# call_date is already set | |
# set call_hhmmss via time | |
call_hhmmss = t.strftime("%H:%M:%S.%f%z") | |
SlotSet("call_hhmmss", call_hhmmss) | |
# set call_time as call_date + call_hhmmss | |
time_str = call_date + 'T' + call_hhmmss | |
SlotSet("call_time", time_str) | |
# returning this is fine because at this point, all four | |
# values will already be filled | |
return ["time", "call_time", "call_date", "call_hhmmss"] | |
else: | |
print("Call date was not yet set") | |
# this is a good scenario, user provided all info immediately | |
# set call_date via time | |
call_date = t.strftime("%Y-%m-%d") | |
SlotSet("call_date", call_date) | |
# set call_hhmmss via time | |
call_hhmmss = t.strftime("%H:%M:%S.%f%z") | |
SlotSet("call_hhmmss", call_hhmmss) | |
# set call_time as call_date + call_hhmmss | |
time_str = call_date + 'T' + call_hhmmss | |
SlotSet("call_time", time_str) | |
# returning this is fine because at this point, all four | |
# values will already be filled | |
return ["time", "call_time", "call_date", "call_hhmmss"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment