Update README.rst to show asyncio code (#456)

This commit is contained in:
Lonami Exo 2017-11-25 18:47:41 +01:00
parent c67f78eab7
commit e0802d1a2d

View File

@ -29,26 +29,30 @@ Creating a client
phone = '+34600000000'
client = TelegramClient('session_name', api_id, api_hash)
client.connect()
# If you already have a previous 'session_name.session' file, skip this.
client.sign_in(phone=phone)
me = client.sign_in(code=77777) # Put whatever code you received here.
async def main():
await client.connect()
# Skip this if you already have a previous 'session_name.session' file
await client.sign_in(phone_number)
me = await client.sign_in(code=input('Code: '))
Doing stuff
-----------
Note that this assumes you're inside an "async def" method. Check out the
`Python documentation <https://docs.python.org/3/library/asyncio-dev.html>`_
if you're new with ``asyncio``.
.. code:: python
print(me.stringify())
client.send_message('username', 'Hello! Talking to you from Telethon')
client.send_file('username', '/home/myself/Pictures/holidays.jpg')
await client.send_message('username', 'Hello! Talking to you from Telethon')
await client.send_file('username', '/home/myself/Pictures/holidays.jpg')
client.download_profile_photo(me)
total, messages, senders = client.get_message_history('username')
client.download_media(messages[0])
await client.download_profile_photo(me)
total, messages, senders = await client.get_message_history('username')
await client.download_media(messages[0])
Next steps
@ -57,4 +61,6 @@ Next steps
Do you like how Telethon looks? Check the
`wiki over GitHub <https://github.com/LonamiWebs/Telethon/wiki>`_ for a
more in-depth explanation, with examples, troubleshooting issues, and more
useful information.
useful information. Note that the examples there are written for the threaded
version, not the one using asyncio. However, you just need to await every
remote call.