|
| 1 | +import json |
| 2 | + |
| 3 | + |
| 4 | +def get_named_parameter(event, name): |
| 5 | + return next(item for item in event["parameters"] if item["name"] == name)["value"] |
| 6 | + |
| 7 | + |
| 8 | +def get_named_property(event, name): |
| 9 | + return next( |
| 10 | + item |
| 11 | + for item in event["requestBody"]["content"]["application/json"]["properties"] |
| 12 | + if item["name"] == name |
| 13 | + )["value"] |
| 14 | + |
| 15 | + |
| 16 | +def create_success_response(event, payload): |
| 17 | + response_code = 200 |
| 18 | + action_group = event["actionGroup"] |
| 19 | + api_path = event["apiPath"] |
| 20 | + |
| 21 | + response_body = {"application/json": {"body": json.dumps(payload)}} |
| 22 | + |
| 23 | + action_response = { |
| 24 | + "messageVersion": "1.0", |
| 25 | + "response": { |
| 26 | + "actionGroup": action_group, |
| 27 | + "apiPath": api_path, |
| 28 | + "httpMethod": event["httpMethod"], |
| 29 | + "httpStatusCode": response_code, |
| 30 | + "responseBody": response_body, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + return action_response |
| 35 | + |
| 36 | + |
| 37 | +def create_success_function_response(event, payload): |
| 38 | + action_group = event["actionGroup"] |
| 39 | + function = event["function"] |
| 40 | + |
| 41 | + response_body = {"TEXT": {"body": json.dumps(payload)}} |
| 42 | + |
| 43 | + function_response = { |
| 44 | + "messageVersion": "1.0", |
| 45 | + "response": { |
| 46 | + "actionGroup": action_group, |
| 47 | + "function": function, |
| 48 | + "functionResponse": { |
| 49 | + "responseState": "REPROMPT", |
| 50 | + "responseBody": response_body, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + return function_response |
| 56 | + |
| 57 | + |
| 58 | +def create_error_response(event, errorString, code=500): |
| 59 | + response_code = code |
| 60 | + action_group = event["actionGroup"] |
| 61 | + api_path = event["apiPath"] |
| 62 | + payload = {"error": errorString} |
| 63 | + |
| 64 | + response_body = {"application/json": {"body": json.dumps(payload)}} |
| 65 | + |
| 66 | + action_response = { |
| 67 | + "messageVersion": "1.0", |
| 68 | + "response": { |
| 69 | + "actionGroup": action_group, |
| 70 | + "apiPath": api_path, |
| 71 | + "httpMethod": event["httpMethod"], |
| 72 | + "httpStatusCode": response_code, |
| 73 | + "responseBody": response_body, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + return action_response |
| 78 | + |
| 79 | + |
| 80 | +def get_employee_time_off_balance(): |
| 81 | + resp = { |
| 82 | + "remaining_balance": 10.25, |
| 83 | + } |
| 84 | + |
| 85 | + return resp |
| 86 | + |
| 87 | + |
| 88 | +def create_employee_time_off_request(event): |
| 89 | + ndays = get_named_parameter(event, "number_of_days") |
| 90 | + sdate = get_named_parameter(event, "start_date") |
| 91 | + print(f"Got ndays: {ndays}, sdate: {sdate}") |
| 92 | + |
| 93 | + resp = {"start_date": sdate, "number_of_days": ndays, "id": 456} |
| 94 | + |
| 95 | + return resp |
| 96 | + |
| 97 | + |
| 98 | +def lambda_handler(event, context): |
| 99 | + promptAttributes = None |
| 100 | + if "promptAttributes" in event: |
| 101 | + print(f"Got prompt details: {promptAttributes}") |
| 102 | + promptAttributes = event["promptAttributes"] |
| 103 | + |
| 104 | + actionGroup = event["actionGroup"] |
| 105 | + if actionGroup == "RequestTimeOff": |
| 106 | + print("Got action group: RequestTimeOff") |
| 107 | + return create_success_function_response( |
| 108 | + event, create_employee_time_off_request(event) |
| 109 | + ) |
| 110 | + elif actionGroup == "GetTimeOff": |
| 111 | + print("Got action group: GetTimeOffBalance") |
| 112 | + return create_success_function_response(event, get_employee_time_off_balance()) |
| 113 | + else: |
| 114 | + print(f"Unknown action group: {actionGroup}") |
| 115 | + return create_error_response(event, "Unknown action group", 400) |
0 commit comments