mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-11-04 01:47:27 +03:00 
			
		
		
		
	Add convenience NewMessage attrs to get media of specific types
This commit is contained in:
		
							parent
							
								
									841aed13da
								
							
						
					
					
						commit
						09f0f86f1e
					
				| 
						 | 
					@ -482,6 +482,52 @@ class NewMessage(_EventBuilder):
 | 
				
			||||||
                if isinstance(doc, types.Document):
 | 
					                if isinstance(doc, types.Document):
 | 
				
			||||||
                    return doc
 | 
					                    return doc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def _document_by_attribute(self, kind, condition=None):
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            Helper method to return the document only if it has an attribute
 | 
				
			||||||
 | 
					            that's an instance of the given kind, and passes the condition.
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            doc = self.document
 | 
				
			||||||
 | 
					            if doc:
 | 
				
			||||||
 | 
					                for attr in doc.attributes:
 | 
				
			||||||
 | 
					                    if isinstance(attr, kind):
 | 
				
			||||||
 | 
					                        if not condition or condition(doc):
 | 
				
			||||||
 | 
					                            return doc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @property
 | 
				
			||||||
 | 
					        def audio(self):
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            If the message media is a document with an Audio attribute,
 | 
				
			||||||
 | 
					            this returns the (:obj:`Document`) object.
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            return self._document_by_attribute(types.DocumentAttributeAudio,
 | 
				
			||||||
 | 
					                                               lambda attr: not attr.voice)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @property
 | 
				
			||||||
 | 
					        def voice(self):
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            If the message media is a document with a Voice attribute,
 | 
				
			||||||
 | 
					            this returns the (:obj:`Document`) object.
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            return self._document_by_attribute(types.DocumentAttributeAudio,
 | 
				
			||||||
 | 
					                                               lambda attr: attr.voice)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @property
 | 
				
			||||||
 | 
					        def video(self):
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            If the message media is a document with a Video attribute,
 | 
				
			||||||
 | 
					            this returns the (:obj:`Document`) object.
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            return self._document_by_attribute(types.DocumentAttributeVideo)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @property
 | 
				
			||||||
 | 
					        def sticker(self):
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            If the message media is a document with a Sticker attribute,
 | 
				
			||||||
 | 
					            this returns the (:obj:`Document`) object.
 | 
				
			||||||
 | 
					            """
 | 
				
			||||||
 | 
					            return self._document_by_attribute(types.DocumentAttributeSticker)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @property
 | 
					        @property
 | 
				
			||||||
        def out(self):
 | 
					        def out(self):
 | 
				
			||||||
            """
 | 
					            """
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user