def score_validation(payload): error_block = {} for criteria in list(payload["view"]["state"]["values"].items()): # Check if string contains non-digits if not list(criteria[1].items())[0][1]["value"].isdecimal(): error_block[ f"{str(criteria[0])}" ] = "Please enter only non-negative integers in this field." # Check if string is not equal to "0" and contains leading zeros elif (list(criteria[1].items())[0][1]["value"] != "0") and ( list(criteria[1].items())[0][1]["value"] != list(criteria[1].items())[0][1]["value"].lstrip("0") ): error_block[ f"{str(criteria[0])}" ] = "Please remove any leading zeros in this field." # Check if score exceeds the upper limit elif int(list(criteria[1].items())[0][1]["value"]) > settings.MAX_CRITERIA_SCORE: error_block[ f"{str(criteria[0])}" ] = f"Please enter a value between 0 and {settings.MAX_CRITERIA_SCORE} inclusive." # Return warnings if inputs are invalid if bool(error_block): return JSONResponse({"response_action": "errors", "errors": error_block}) # Emit normal action if inputs are validated else: # f-string does not work here emit(actions, "{}:validated_score".format(payload["type"]), payload=payload) return Response()
async def post_actions(request: Request) -> Response: form = await request.form() form_data = json.loads(form["payload"]) # have the convenience of pydantic validation action = SlackAction(**form_data) _events = [action.type] if action.actions: _events.extend(_add_action_triggers(action)) if action.callback_id: _events.append(f"{action.type}:{action.callback_id}") if action.view: view_callback_id = action.view.get("callback_id") if view_callback_id: _events.append(f"{action.type}:{view_callback_id}") for _event in _events: emit(actions, _event, payload=action) registered_handlers = set(R.callbacks.keys()) registered_events = set(_events) handlers_to_call = registered_handlers.intersection(registered_events) if len(handlers_to_call) > 1: raise ValueError(f"Multiple response handlers found.") if handlers_to_call: handle = handlers_to_call.pop() response = R.handle(handle, action.dict()) assert isinstance( response, Response), "Please return a starlette.responses.Response" return response return Response()
async def post_events(message: typing.Union[SlackEnvelope, SlackChallenge]): if isinstance(message, SlackChallenge): return message.challenge emit(events, message.event["type"], payload=message) return Response()
async def post_commands(request: Request): form = await request.form() command = SlackCommand(**form) emit(commands, command.command.lstrip("/"), command) return Response()