def de_json(cls, data: JSONDict, bot: 'Bot') -> Optional['Chat']: data = cls.parse_data(data) if not data: return None data['photo'] = ChatPhoto.de_json(data.get('photo'), bot) from telegram import Message data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot) data['permissions'] = ChatPermissions.de_json(data.get('permissions'), bot) return cls(bot=bot, **data)
def post(self, url: str, data: JSONDict, timeout: float = None) -> Union[JSONDict, bool]: """Request an URL. Args: url (:obj:`str`): The web location we want to retrieve. data (dict[str, str|int], optional): A dict of key/value pairs. timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as the read timeout from the server (instead of the one specified during creation of the connection pool). Returns: A JSON object. """ urlopen_kwargs = {} if timeout is not None: urlopen_kwargs['timeout'] = Timeout(read=timeout, connect=self._connect_timeout) if data is None: data = {} # Are we uploading files? files = False # pylint: disable=R1702 for key, val in data.copy().items(): if isinstance(val, InputFile): # Convert the InputFile to urllib3 field format data[key] = val.field_tuple files = True elif isinstance(val, (float, int)): # Urllib3 doesn't like floats it seems data[key] = str(val) elif key == 'media': # One media or multiple if isinstance(val, InputMedia): # Attach and set val to attached name data[key] = val.to_json() if isinstance(val.media, InputFile): # type: ignore data[val.media.attach] = val.media.field_tuple # type: ignore else: # Attach and set val to attached name for all media = [] for med in val: media_dict = med.to_dict() media.append(media_dict) if isinstance(med.media, InputFile): data[med.media.attach] = med.media.field_tuple # if the file has a thumb, we also need to attach it to the data if "thumb" in media_dict: data[med.thumb.attach] = med.thumb.field_tuple data[key] = json.dumps(media) files = True elif isinstance(val, list): # In case we're sending files, we need to json-dump lists manually # As we can't know if that's the case, we just json-dump here data[key] = json.dumps(val) # Use multipart upload if we're uploading files, otherwise use JSON if files: result = self._request_wrapper('POST', url, fields=data, **urlopen_kwargs) else: result = self._request_wrapper( 'POST', url, body=json.dumps(data).encode('utf-8'), headers={'Content-Type': 'application/json'}, **urlopen_kwargs, ) return self._parse(result)