def lambda_handler(request, context): """Main Lambda handler. Since you can expect both v2 and v3 directives for a period of time during the migration and transition of your existing users, this main Lambda handler must be modified to support both v2 and v3 requests. """ try: logger.info("Directive:") logger.info(json.dumps(request, indent=4, sort_keys=True)) # logger.info(request) version = get_directive_version(request) if version == "3": logger.info("Received v3 directive!") if request["directive"]["header"]["name"] == "Discover": response = handle_discovery_v3(request) else: response = handle_non_discovery_v3(request) logger.info("Response:") # logger.info(response) logger.info(json.dumps(response, indent=4, sort_keys=True)) if version == "3": logger.info("Validate v3 response") validate_message(request, response) return response except ValueError as error: logger.error(error) raise
def lambda_handler(request, context): try: logger.info("Directive:") logger.info(json.dumps(request, indent=4, sort_keys=True)) directive_name = request["directive"]["header"]["name"] if get_directive_version(request) != "3": raise ValueError("API must be version 3") logger.info("Received v3 directive!") response = DIRECTIVE_HANDLERS[directive_name](request).as_dict() logger.info("Response:") logger.info(json.dumps(response, indent=4, sort_keys=True)) logger.info("Validate v3 response") validate_message(request, response) return response except KeyError as error: logger.info("Directive: " + str(directive_name) + " not supported.") logger.error(error) except ValueError as error: logger.error(error) raise
def lambda_handler(request, context): customer_id="" try: formatted_log("Request",request) version = get_directive_version(request) if version == "3": if request["directive"]["header"]["name"] == "Discover": response = v3_code.handle_discovery_v3(request,customer_id) else: response = v3_code.handle_non_discovery_v3(request,customer_id) formatted_log("Validate v3 response",response) validate_message(request, response) else: logger.error("Received unsupported directive!") return response except ValueError as error: logger.error("ERROR IN DIRECTIVE") logger.error(request) logger.error(error) raise
def lambda_handler(req, context): """ This is where the program begins and where the program will end, its return value will be forwarded to Alexa :param req: the message from Alexa :param context: execution context of the message, not used :return: json dict that if valid, will be turned into Alexa speech response to user """ try: logger.info("Directive:") logger.info(json.dumps(req, indent=4, sort_keys=True)) version = utils.get_directive_version(req) if version == "3": try: masterHandler = lambda_master_handler.MasterHandler() except connectTimeoutException: logger.warning("Connection timeout") logger.info("Received v3 directive!") if req["directive"]["header"]["name"] == "Discover": # discovery is used to help Alexa knows what devices exist as well as what can be done with them response = masterHandler.handleDiscoveryV3(req) else: response = masterHandler.handleNonDiscoveryV3(req, context) # logger.info("Resp:") # logger.info(json.dumps(response, indent=4, sort_keys=True)) # check the response against amazon json schema validate_message(req, response) # disconnect from mqtt server try: while False == masterHandler._mqttManager._myAWSIoTMQTTClient.disconnect( ): pass except disconnectTimeoutException: logger.warning("Disconnect timed out") else: # we shouldn't be receiving v2 directive for the devices that are being used logger.info("Received v2 directive!") response = masterHandler.handleErrorResponse( req, "Unsupported Directive Version", "V2 directive Received") return response except ValueError as error: logger.error(error) raise
def api() -> str: errors = validate_message(request) if errors is not None: print(errors) raise InvalidUsage(errors) activities = request.json.get("activities") response = {"activities": activities} return jsonify(response)
def lambda_handler(request,context): try: logger.info("Directive:") logger.info(json.dumps(request, indent=4, sort_keys=True)) if request["directive"]["header"]["name"] == "Discover": response = handleDiscovery(request) else: response = handleNonDiscovery(request) logger.info("Response:") logger.info(json.dumps(response, indent=4, sort_keys=True)) validate_message(request, response) return response except ValueError as error: logger.error(error) raise
def lambda_handler(request, context): """Main Lambda handler. Since you can expect both v2 and v3 directives for a period of time during the migration and transition of your existing users, this main Lambda handler must be modified to support both v2 and v3 requests. """ try: logger.info("Directive:") logger.info(json.dumps(request, indent=4, sort_keys=True)) version = get_directive_version(request) if version == "3": logger.info("Received v3 directive!") if request["directive"]["header"]["name"] == "Discover": response = handle_discovery_v3(request) else: response = handle_non_discovery_v3(request) else: logger.info("Received v2 directive!") if request["header"]["namespace"] == "Alexa.ConnectedHome.Discovery": response = handle_discovery() else: response = handle_non_discovery(request) logger.info("Response:") logger.info(json.dumps(response, indent=4, sort_keys=True)) if version == "3": logger.info("Validate v3 response") validate_message(request, response) return response except ValueError as error: logger.error(error) raise
def lambda_handler(request, context): ci = pyASH.ControllerInterface(AcceptGrantHandler, DiscoverHandler) ci.register_callback(ReportStateHandler, 'Alexa', 'ReportState') ci.register_callback(SpeakerHandler, 'Alexa.Speaker') ci.register_callback(PowerHandler, 'Alexa.PowerController') ci.register_callback(InputHandler, 'Alexa.InputController') ci.register_callback(SceneHandler, 'Alexa.SceneController') try: d = pyASH.Request(request) except (ValueError, TypeError) as error: # Bad directive received logger.error('lambda_handler received a bad directive: ' + str(error)) raise if d.payloadVersion != '3': errmsg = 'Received invalid directive version. Only version 3 is supported. This directive was version '+version+'.' logger.warn(errmsg) return pyASH.ErrorResponse('Alexa', 'INVALID_DIRECTIVE', errmsg).get_json() logger.info('Received directive') logger.info(json.dumps(request,indent=4)) response = ci.process_directive(request).get_json() try: validate_message(request, response) except jsonschema.exceptions.ValidationError: logger.warn('lambda_handler: response message failed validation. Response was ' + json.dumps(response, indent=4)) raise except jsonschema.exceptions.SchemaError: logger.warn('lambda_handler: schema failed validation') raise logger.info('Produced response') logger.info(json.dumps(response,indent=4)) return response