Skip to content

Commit

Permalink
Fix file uploads and references to full_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Oct 16, 2023
1 parent 0727c1d commit 34dffe9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion client/doc/concepts/botapi-vs-mtproto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ In Telethon:
@client.on(events.NewMessage, Command("/start"))
async def command_start_handler(message: Message) -> None:
await message.respond(html=f"Hello, <b>{html.escape(message.sender.full_name)}</b>!")
await message.respond(html=f"Hello, <b>{html.escape(message.sender.name)}</b>!")
@dp.message()
async def echo_handler(message: types.Message) -> None:
Expand Down
2 changes: 1 addition & 1 deletion client/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
async def main():
async with Client('name', api_id, api_hash) as client:
me = await client.interactive_login()
await client.send_message(me, f'Hello, {me.full_name}!')
await client.send_message(me, f'Hello, {me.name}!')
@client.on(events.NewMessage, filters.Text(r'(?i)hello'))
async def handler(event):
Expand Down
4 changes: 2 additions & 2 deletions client/src/telethon/_impl/client/client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async def interactive_login(
if re.match(r"\d+:", phone_or_token):
user = await self.bot_sign_in(phone_or_token)
try:
print("Signed in as bot:", user.full_name)
print("Signed in as bot:", user.name)
except UnicodeEncodeError:
print("Signed in as bot")

Expand Down Expand Up @@ -188,7 +188,7 @@ async def interactive_login(
user = user_or_token

try:
print("Signed in as user:", user.full_name)
print("Signed in as user:", user.name)
except UnicodeEncodeError:
print("Signed in as user")

Expand Down
20 changes: 10 additions & 10 deletions client/src/telethon/_impl/client/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def add_event_handler(
.. code-block:: python
async def my_print_handler(event):
print(event.chat.full_name, event.text)
print(event.chat.name, event.text)
# Register a handler to be called on new messages
client.add_event_handler(my_print_handler, events.NewMessage)
Expand Down Expand Up @@ -395,7 +395,7 @@ async def delete_dialog(self, chat: ChatLike) -> None:
.. code-block:: python
async for dialog in client.iter_dialogs():
if 'dog pictures' in dialog.chat.full_name:
if 'dog pictures' in dialog.chat.name:
# You've realized you're more of a cat person
await client.delete_dialog(dialog.chat)
"""
Expand Down Expand Up @@ -609,7 +609,7 @@ def get_contacts(self) -> AsyncList[User]:
.. code-block:: python
async for user in client.get_contacts():
print(user.full_name, user.id)
print(user.name, user.id)
"""
return get_contacts(self)

Expand All @@ -629,7 +629,7 @@ def get_dialogs(self) -> AsyncList[Dialog]:
async for dialog in client.get_dialogs():
print(
dialog.chat.full_name,
dialog.chat.name,
dialog.last_message.text if dialog.last_message else ''
)
"""
Expand Down Expand Up @@ -719,7 +719,7 @@ async def get_me(self) -> Optional[User]:
if me.bot:
print('I am a bot')
print('My name is', me.full_name)
print('My name is', me.name)
if me.phone:
print('My phone number is', me.phone)
Expand Down Expand Up @@ -764,7 +764,7 @@ def get_messages(
from datetime import datetime
async for message in client.get_messages(chat, offset_date=datetime(2023, 1, 1)):
print(message.sender.full_name, ':', message.html_text)
print(message.sender.name, ':', message.html_text)
"""
return get_messages(
self, chat, limit, offset_id=offset_id, offset_date=offset_date
Expand Down Expand Up @@ -814,7 +814,7 @@ def get_participants(self, chat: ChatLike) -> AsyncList[Participant]:
.. code-block:: python
async for participant in client.get_participants(chat):
print(participant.user.full_name)
print(participant.user.name)
"""
return get_participants(self, chat)

Expand Down Expand Up @@ -897,7 +897,7 @@ async def interactive_login(
.. code-block:: python
me = await client.interactive_login()
print('Logged in as:', me.full_name)
print('Logged in as:', me.name)
# or, to make sure you're logged-in as a bot
await client.interactive_login('1234:ab56cd78ef90)
Expand Down Expand Up @@ -950,14 +950,14 @@ def on(
# Register a handler to be called on new messages
@client.on(events.NewMessage)
async def my_print_handler(event):
print(event.chat.full_name, event.text)
print(event.chat.name, event.text)
# Register a handler to be called on new messages if they contain "hello" or "/start"
from telethon.events.filters import Any, Text, Command
@client.on(events.NewMessage, Any(Text(r'hello'), Command('/start')))
async def my_other_print_handler(event):
print(event.chat.full_name, event.text)
print(event.chat.name, event.text)
.. seealso::
Expand Down
13 changes: 7 additions & 6 deletions client/src/telethon/_impl/client/client/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,26 +311,27 @@ async def upload(
) -> abcs.InputFile:
file_id = generate_random_id()

uploaded = 0
offset = 0
part = 0
total_parts = (size + MAX_CHUNK_SIZE - 1) // MAX_CHUNK_SIZE
buffer = bytearray()
to_store: Union[bytearray, bytes] = b""
hash_md5 = hashlib.md5()
is_big = size > BIG_FILE_SIZE

while uploaded != size:
while offset != size:
ret = fd.read(MAX_CHUNK_SIZE - len(buffer))
chunk = await ret if isawaitable(ret) else ret
assert isinstance(chunk, bytes)
if not chunk:
raise ValueError("unexpected end-of-file")

if len(chunk) == MAX_CHUNK_SIZE or uploaded + len(chunk) == size:
offset += len(chunk)
if not buffer and (offset == size or len(chunk) == MAX_CHUNK_SIZE):
to_store = chunk
else:
buffer += chunk
if len(buffer) == MAX_CHUNK_SIZE:
if offset == size or len(buffer) == MAX_CHUNK_SIZE:
to_store = buffer
else:
continue
Expand All @@ -340,14 +341,14 @@ async def upload(
functions.upload.save_big_file_part(
file_id=file_id,
file_part=part,
file_total_parts=part,
file_total_parts=total_parts,
bytes=to_store,
)
)
else:
await client(
functions.upload.save_file_part(
file_id=file_id, file_part=total_parts, bytes=to_store
file_id=file_id, file_part=part, bytes=to_store
)
)
hash_md5.update(to_store)
Expand Down
1 change: 0 additions & 1 deletion client/src/telethon/_impl/mtproto/mtp/encrypted.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ def _finalize_plain(self) -> bytes:
self._msg_count,
)

print("packed", self._msg_count)
self._msg_count = 0
result = bytes(self._buffer)
self._buffer.clear()
Expand Down

0 comments on commit 34dffe9

Please sign in to comment.