예제 #1
0
 def get_branch(self, branch_name: str,
                context: typing.Union[str, dict]) -> Branch:
     branch = self.names[branch_name]()
     if isinstance(context, str):
         context = json.loads(context)
     branch.context = context
     branch.key = branch_name
     return branch
예제 #2
0
 def get_payload_json(
     self,
     throw_error: bool = False,
     unpack_failure: typing.Callable[[str], dict] = lambda payload: payload,
 ) -> typing.Union[dict, None]:
     try:
         return json.loads(self.payload)
     except (json.decoder.JSONDecodeError, TypeError) as e:
         if throw_error:
             raise e
     return unpack_failure(self.payload)
예제 #3
0
파일: element.py 프로젝트: gdfd-asr/vk_bot
    def __init__(
        self,
        title: str = None,
        description: str = None,
        photo_id: str = None,
        buttons: typing.List[dict] = None,
        action: dict = None,
    ):
        if not buttons:
            raise TemplateError("Buttons are required")
        if not photo_id and not (title or description):
            raise TemplateError("photo_id or title is required")

        if isinstance(buttons, str):
            buttons = json.loads(buttons)

        if isinstance(buttons, dict):
            buttons = buttons.get("buttons")[0]  # taking only first row

        self.raw: typing.Dict = {
            k: v for k, v in locals().items() if v is not None and k != "self"
        }