Get rid of client.loop

Instead, use the asyncio-intended way of implicit loop.
This commit is contained in:
Lonami Exo
2022-01-16 13:51:23 +01:00
parent 6eadc8aed8
commit a62627534e
19 changed files with 140 additions and 177 deletions

View File

@@ -8,70 +8,70 @@ use these if possible.
.. code-block:: python
import asyncio
from telethon import TelegramClient
# Remember to use your own values from my.telegram.org!
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
# Getting information about yourself
me = await client.get_me()
async with TelegramClient('anon', api_id, api_hash).start() as client:
# Getting information about yourself
me = await client.get_me()
# "me" is a user object. You can pretty-print
# any Telegram object with the "stringify" method:
print(me.stringify())
# "me" is a user object. You can pretty-print
# any Telegram object with the "stringify" method:
print(me.stringify())
# When you print something, you see a representation of it.
# You can access all attributes of Telegram objects with
# the dot operator. For example, to get the username:
username = me.username
print(username)
print(me.phone)
# When you print something, you see a representation of it.
# You can access all attributes of Telegram objects with
# the dot operator. For example, to get the username:
username = me.username
print(username)
print(me.phone)
# You can print all the dialogs/conversations that you are part of:
async for dialog in client.iter_dialogs():
print(dialog.name, 'has ID', dialog.id)
# You can print all the dialogs/conversations that you are part of:
async for dialog in client.iter_dialogs():
print(dialog.name, 'has ID', dialog.id)
# You can send messages to yourself...
await client.send_message('me', 'Hello, myself!')
# ...to some chat ID
await client.send_message(-100123456, 'Hello, group!')
# ...to your contacts
await client.send_message('+34600123123', 'Hello, friend!')
# ...or even to any username
await client.send_message('username', 'Testing Telethon!')
# You can send messages to yourself...
await client.send_message('me', 'Hello, myself!')
# ...to some chat ID
await client.send_message(-100123456, 'Hello, group!')
# ...to your contacts
await client.send_message('+34600123123', 'Hello, friend!')
# ...or even to any username
await client.send_message('username', 'Testing Telethon!')
# You can, of course, use markdown in your messages:
message = await client.send_message(
'me',
'This message has **bold**, `code`, __italics__ and '
'a [nice website](https://example.com)!',
link_preview=False
)
# You can, of course, use markdown in your messages:
message = await client.send_message(
'me',
'This message has **bold**, `code`, __italics__ and '
'a [nice website](https://example.com)!',
link_preview=False
)
# Sending a message returns the sent message object, which you can use
print(message.raw_text)
# Sending a message returns the sent message object, which you can use
print(message.raw_text)
# You can reply to messages directly if you have a message object
await message.reply('Cool!')
# You can reply to messages directly if you have a message object
await message.reply('Cool!')
# Or send files, songs, documents, albums...
await client.send_file('me', '/home/me/Pictures/holidays.jpg')
# Or send files, songs, documents, albums...
await client.send_file('me', '/home/me/Pictures/holidays.jpg')
# You can print the message history of any chat:
async for message in client.iter_messages('me'):
print(message.id, message.text)
# You can print the message history of any chat:
async for message in client.iter_messages('me'):
print(message.id, message.text)
# You can download media from messages, too!
# The method will return the path where the file was saved.
if message.photo:
path = await message.download_media()
print('File saved to', path) # printed after download is done
# You can download media from messages, too!
# The method will return the path where the file was saved.
if message.photo:
path = await message.download_media()
print('File saved to', path) # printed after download is done
with client:
client.loop.run_until_complete(main())
asyncio.run(main())
Here, we show how to sign in, get information about yourself, send
@@ -100,8 +100,8 @@ proceeding. We will see all the available methods later on.
# Most of your code should go here.
# You can of course make and use your own async def (do_something).
# They only need to be async if they need to await things.
async with client:
async with client.start():
me = await client.get_me()
await do_something(me)
client.loop.run_until_complete(main())
asyncio.run(main())

View File

@@ -49,6 +49,7 @@ We can finally write some code to log into our account!
.. code-block:: python
import asyncio
from telethon import TelegramClient
# Use your own values from my.telegram.org
@@ -57,10 +58,10 @@ We can finally write some code to log into our account!
async def main():
# The first parameter is the .session file name (absolute paths allowed)
async with TelegramClient('anon', api_id, api_hash) as client:
async with TelegramClient('anon', api_id, api_hash).start() as client:
await client.send_message('me', 'Hello, myself!')
client.loop.run_until_complete(main())
asyncio.run(main())
In the first line, we import the class name so we can create an instance
@@ -98,21 +99,19 @@ You will still need an API ID and hash, but the process is very similar:
.. code-block:: python
import asyncio
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
bot_token = '12345:0123456789abcdef0123456789abcdef'
# We have to manually call "start" if we want an explicit bot token
bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
async def main():
# But then we can use the client instance as usual
async with bot:
...
async with TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token) as bot:
... # bot is your client
client.loop.run_until_complete(main())
asyncio.run(main())
To get a bot account, you need to talk

View File

@@ -74,7 +74,7 @@ Or we call `client.get_input_entity()
async def main():
peer = await client.get_input_entity('someone')
client.loop.run_until_complete(main())
asyncio.run(main())
.. note::

View File

@@ -156,8 +156,8 @@ you can save it in a variable directly:
.. code-block:: python
string = '1aaNk8EX-YRfwoRsebUkugFvht6DUPi_Q25UOCzOAqzc...'
with TelegramClient(StringSession(string), api_id, api_hash) as client:
client.loop.run_until_complete(client.send_message('me', 'Hi'))
async with TelegramClient(StringSession(string), api_id, api_hash).start() as client:
await client.send_message('me', 'Hi')
These strings are really convenient for using in places like Heroku since

View File

@@ -759,3 +759,5 @@ they are meant to work on lists.
also mark read only supports single now. a list would just be max anyway.
removed max id since it's not really of much use.
client loop has been removed. embrace implicit loop as asyncio does now

View File

@@ -20,10 +20,10 @@ Each mixin has its own methods, which you all can use.
async def main():
# Now you can use all client methods listed below, like for example...
await client.send_message('me', 'Hello to myself!')
async with client.start():
await client.send_message('me', 'Hello to myself!')
with client:
client.loop.run_until_complete(main())
asyncio.run(main())
You **don't** need to import these `AuthMethods`, `MessageMethods`, etc.