style: Apply black formatting to add_contact function
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
afb02be510
commit
359b86315e
1 changed files with 27 additions and 16 deletions
43
main.py
43
main.py
|
|
@ -1300,7 +1300,12 @@ async def get_message_context(
|
||||||
title="Add Contact", openWorldHint=True, destructiveHint=True, idempotentHint=True
|
title="Add Contact", openWorldHint=True, destructiveHint=True, idempotentHint=True
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
async def add_contact(phone: Optional[str] = None, first_name: str = "", last_name: str = "", username: Optional[str] = None) -> str:
|
async def add_contact(
|
||||||
|
phone: Optional[str] = None,
|
||||||
|
first_name: str = "",
|
||||||
|
last_name: str = "",
|
||||||
|
username: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Add a new contact to your Telegram account.
|
Add a new contact to your Telegram account.
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -1308,7 +1313,7 @@ async def add_contact(phone: Optional[str] = None, first_name: str = "", last_na
|
||||||
first_name: The contact's first name.
|
first_name: The contact's first name.
|
||||||
last_name: The contact's last name (optional).
|
last_name: The contact's last name (optional).
|
||||||
username: The Telegram username (without @). Use this for adding contacts without phone numbers.
|
username: The Telegram username (without @). Use this for adding contacts without phone numbers.
|
||||||
|
|
||||||
Note: Either phone or username must be provided. If username is provided, the function will resolve it
|
Note: Either phone or username must be provided. If username is provided, the function will resolve it
|
||||||
and add the contact using contacts.addContact API (which supports adding contacts without phone numbers).
|
and add the contact using contacts.addContact API (which supports adding contacts without phone numbers).
|
||||||
"""
|
"""
|
||||||
|
|
@ -1316,36 +1321,38 @@ async def add_contact(phone: Optional[str] = None, first_name: str = "", last_na
|
||||||
# Normalize None to empty string for easier checking
|
# Normalize None to empty string for easier checking
|
||||||
phone = phone or ""
|
phone = phone or ""
|
||||||
username = username or ""
|
username = username or ""
|
||||||
|
|
||||||
# Validate that at least one identifier is provided
|
# Validate that at least one identifier is provided
|
||||||
if not phone and not username:
|
if not phone and not username:
|
||||||
return "Error: Either phone or username must be provided."
|
return "Error: Either phone or username must be provided."
|
||||||
|
|
||||||
# If username is provided, use it for username-based contact addition
|
# If username is provided, use it for username-based contact addition
|
||||||
if username:
|
if username:
|
||||||
# Remove @ if present
|
# Remove @ if present
|
||||||
username_clean = username.lstrip("@")
|
username_clean = username.lstrip("@")
|
||||||
if not username_clean:
|
if not username_clean:
|
||||||
return "Error: Username cannot be empty."
|
return "Error: Username cannot be empty."
|
||||||
|
|
||||||
# Resolve username to get user information
|
# Resolve username to get user information
|
||||||
try:
|
try:
|
||||||
resolve_result = await client(functions.contacts.ResolveUsernameRequest(username=username_clean))
|
resolve_result = await client(
|
||||||
|
functions.contacts.ResolveUsernameRequest(username=username_clean)
|
||||||
|
)
|
||||||
|
|
||||||
# Extract user from the result
|
# Extract user from the result
|
||||||
if not resolve_result.users:
|
if not resolve_result.users:
|
||||||
return f"Error: User with username @{username_clean} not found."
|
return f"Error: User with username @{username_clean} not found."
|
||||||
|
|
||||||
user = resolve_result.users[0]
|
user = resolve_result.users[0]
|
||||||
if not isinstance(user, User):
|
if not isinstance(user, User):
|
||||||
return f"Error: Resolved entity is not a user."
|
return f"Error: Resolved entity is not a user."
|
||||||
|
|
||||||
user_id = user.id
|
user_id = user.id
|
||||||
access_hash = user.access_hash
|
access_hash = user.access_hash
|
||||||
|
|
||||||
# Use contacts.addContact to add the contact by user ID
|
# Use contacts.addContact to add the contact by user ID
|
||||||
from telethon.tl.types import InputUser
|
from telethon.tl.types import InputUser
|
||||||
|
|
||||||
result = await client(
|
result = await client(
|
||||||
functions.contacts.AddContactRequest(
|
functions.contacts.AddContactRequest(
|
||||||
id=InputUser(user_id=user_id, access_hash=access_hash),
|
id=InputUser(user_id=user_id, access_hash=access_hash),
|
||||||
|
|
@ -1354,16 +1361,20 @@ async def add_contact(phone: Optional[str] = None, first_name: str = "", last_na
|
||||||
phone="", # Empty phone for username-based contacts
|
phone="", # Empty phone for username-based contacts
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if hasattr(result, "updates") and result.updates:
|
if hasattr(result, "updates") and result.updates:
|
||||||
return f"Contact {first_name} {last_name} (@{username_clean}) added successfully."
|
return (
|
||||||
|
f"Contact {first_name} {last_name} (@{username_clean}) added successfully."
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
return f"Contact {first_name} {last_name} (@{username_clean}) added successfully (no updates returned)."
|
return f"Contact {first_name} {last_name} (@{username_clean}) added successfully (no updates returned)."
|
||||||
|
|
||||||
except Exception as resolve_e:
|
except Exception as resolve_e:
|
||||||
logger.exception(f"add_contact (username resolve) failed (username={username_clean})")
|
logger.exception(
|
||||||
|
f"add_contact (username resolve) failed (username={username_clean})"
|
||||||
|
)
|
||||||
return log_and_format_error("add_contact", resolve_e, username=username_clean)
|
return log_and_format_error("add_contact", resolve_e, username=username_clean)
|
||||||
|
|
||||||
elif phone:
|
elif phone:
|
||||||
# Original phone-based contact addition
|
# Original phone-based contact addition
|
||||||
from telethon.tl.types import InputPhoneContact
|
from telethon.tl.types import InputPhoneContact
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue