Find the first of contacts based on query, which can be string, ContactQueryFilter, or callable function. Args: query: the query body to build filter Examples: >>> # 1. find contacts based query string, will match one of: contact
(cls,
query: Union[str, ContactQueryFilter, Callable[[Contact], bool]]
)
| 183 | |
| 184 | @classmethod |
| 185 | async def find(cls, |
| 186 | query: Union[str, ContactQueryFilter, Callable[[Contact], bool]] |
| 187 | ) -> Optional[Contact]: |
| 188 | """ |
| 189 | Find the first of contacts based on query, which can be string, ContactQueryFilter, or callable<filter> function. |
| 190 | |
| 191 | Args: |
| 192 | query: the query body to build filter |
| 193 | |
| 194 | Examples: |
| 195 | >>> # 1. find contacts based query string, will match one of: contact_id, weixin, name and alias |
| 196 | >>> # what's more, contact_id and weixin will follow extract match, name and alias will follow fuzzy match |
| 197 | >>> Contact.find('your-contact-id/weixin') # find: <your-contact-id> == contact.contact_id |
| 198 | >>> Contact.find('name/alias') # find: <name> in contact.name |
| 199 | |
| 200 | >>> # 2. find contacts based ContactQueryFilter object, will match all fields |
| 201 | >>> query = ContactQueryFilter(id='your-contact-id', weixin='weixin') # find: <your-contact-id> == contact.contact_id |
| 202 | >>> query = ContactQueryFilter(name='your-contact-name') # find: <your-contact-name> in contact.name |
| 203 | >>> Contact.find(query) |
| 204 | |
| 205 | >>> # 3. find contacts based on callable query function |
| 206 | >>> def filter_contacts(contact: Contact) -> bool: |
| 207 | >>> if contact.contact_id == "your-contact-id": |
| 208 | >>> return True |
| 209 | >>> return False |
| 210 | >>> Contact.find(filter_contacts) |
| 211 | Returns: |
| 212 | Contact: the contact object filtered by query or None |
| 213 | """ |
| 214 | log.info('find() <%s, %s>', cls, query) |
| 215 | |
| 216 | contacts = await cls.find_all(query) |
| 217 | if not contacts: |
| 218 | return None |
| 219 | return contacts[0] |
| 220 | |
| 221 | @classmethod |
| 222 | async def find_all(cls: Type[Contact], |