mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 05:19:41 +00:00
Document the magic sync module
This commit is contained in:
@@ -21,18 +21,14 @@ Creating a client
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import asyncio
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
from telethon import TelegramClient
|
||||
from telethon import TelegramClient, sync
|
||||
|
||||
# These example values won't work. You must get your own api_id and
|
||||
# api_hash from https://my.telegram.org, under API Development.
|
||||
api_id = 12345
|
||||
api_hash = '0123456789abcdef0123456789abcdef'
|
||||
|
||||
client = TelegramClient('session_name', api_id, api_hash)
|
||||
loop.run_until_complete(client.start())
|
||||
client = TelegramClient('session_name', api_id, api_hash).start()
|
||||
|
||||
**More details**: :ref:`creating-a-client`
|
||||
|
||||
@@ -42,36 +38,33 @@ Basic Usage
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def main():
|
||||
# Getting information about yourself
|
||||
me = await client.get_me()
|
||||
print(me.stringify())
|
||||
# Getting information about yourself
|
||||
me = client.get_me()
|
||||
print(me.stringify())
|
||||
|
||||
# Sending a message (you can use 'me' or 'self' to message yourself)
|
||||
await client.send_message('username', 'Hello World from Telethon!')
|
||||
# Sending a message (you can use 'me' or 'self' to message yourself)
|
||||
client.send_message('username', 'Hello World from Telethon!')
|
||||
|
||||
# Sending a file
|
||||
await client.send_file('username', '/home/myself/Pictures/holidays.jpg')
|
||||
# Sending a file
|
||||
client.send_file('username', '/home/myself/Pictures/holidays.jpg')
|
||||
|
||||
# Retrieving messages from a chat
|
||||
from telethon import utils
|
||||
async for message in client.iter_messages('username', limit=10):
|
||||
print(utils.get_display_name(message.sender), message.message)
|
||||
# Retrieving messages from a chat
|
||||
from telethon import utils
|
||||
for message in client.iter_messages('username', limit=10):
|
||||
print(utils.get_display_name(message.sender), message.message)
|
||||
|
||||
# Listing all the dialogs (conversations you have open)
|
||||
async for dialog in client.get_dialogs(limit=10):
|
||||
print(dialog.name, dialog.draft.text)
|
||||
# Listing all the dialogs (conversations you have open)
|
||||
for dialog in client.get_dialogs(limit=10):
|
||||
print(dialog.name, dialog.draft.text)
|
||||
|
||||
# Downloading profile photos (default path is the working directory)
|
||||
await client.download_profile_photo('username')
|
||||
# Downloading profile photos (default path is the working directory)
|
||||
client.download_profile_photo('username')
|
||||
|
||||
# Once you have a message with .media (if message.media)
|
||||
# you can download it using client.download_media(),
|
||||
# or even using message.download_media():
|
||||
messages = await client.get_messages('username')
|
||||
await messages[0].download_media()
|
||||
|
||||
loop.run_until_complete(main())
|
||||
# Once you have a message with .media (if message.media)
|
||||
# you can download it using client.download_media(),
|
||||
# or even using message.download_media():
|
||||
messages = client.get_messages('username')
|
||||
messages[0].download_media()
|
||||
|
||||
**More details**: :ref:`telegram-client`
|
||||
|
||||
@@ -86,8 +79,8 @@ Handling Updates
|
||||
from telethon import events
|
||||
|
||||
@client.on(events.NewMessage(incoming=True, pattern='(?i)hi'))
|
||||
async def handler(event):
|
||||
await event.reply('Hello!')
|
||||
def handler(event):
|
||||
event.reply('Hello!')
|
||||
|
||||
client.run_until_disconnected()
|
||||
|
||||
|
Reference in New Issue
Block a user