2019-05-09 13:24:37 +03:00
|
|
|
===========
|
|
|
|
Quick-Start
|
|
|
|
===========
|
|
|
|
|
|
|
|
Let's see a longer example to learn some of the methods that the library
|
|
|
|
has to offer. These are known as "friendly methods", and you should always
|
|
|
|
use these if possible.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
from telethon import TelegramClient
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# Remember to use your own values from my.telegram.org!
|
|
|
|
api_id = 12345
|
|
|
|
api_hash = '0123456789abcdef0123456789abcdef'
|
2019-08-14 00:33:39 +03:00
|
|
|
client = TelegramClient('anon', api_id, api_hash)
|
2019-05-09 13:24:37 +03:00
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
async def main():
|
2019-05-09 13:24:37 +03:00
|
|
|
# Getting information about yourself
|
2019-08-14 00:33:39 +03:00
|
|
|
me = await client.get_me()
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# "me" is an 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)
|
|
|
|
|
|
|
|
# You can print all the dialogs/conversations that you are part of:
|
2019-08-14 00:33:39 +03:00
|
|
|
async for dialog in client.iter_dialogs():
|
2019-05-09 13:24:37 +03:00
|
|
|
print(dialog.name, 'has ID', dialog.id)
|
|
|
|
|
|
|
|
# You can send messages to yourself...
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message('me', 'Hello, myself!')
|
2019-05-09 13:24:37 +03:00
|
|
|
# ...to some chat ID
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message(-100123456, 'Hello, group!')
|
2019-05-09 13:24:37 +03:00
|
|
|
# ...to your contacts
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message('+34600123123', 'Hello, friend!')
|
2019-05-09 13:24:37 +03:00
|
|
|
# ...or even to any username
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message('TelethonChat', 'Hello, Telethon!')
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# You can, of course, use markdown in your messages:
|
2019-08-14 00:33:39 +03:00
|
|
|
message = await client.send_message(
|
2019-05-09 13:24:37 +03:00
|
|
|
'me',
|
|
|
|
'This message has **bold**, `code`, __italics__ and '
|
|
|
|
'a [nice website](https://lonamiwebs.github.io)!',
|
|
|
|
link_preview=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
2019-08-14 00:33:39 +03:00
|
|
|
await message.reply('Cool!')
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# Or send files, songs, documents, albums...
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_file('me', '/home/me/Pictures/holidays.jpg')
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# You can print the message history of any chat:
|
2019-08-14 00:33:39 +03:00
|
|
|
async for message in client.iter_messages('me'):
|
2019-05-09 13:24:37 +03:00
|
|
|
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:
|
2019-08-14 00:33:39 +03:00
|
|
|
path = await message.download_media()
|
|
|
|
print('File saved to', path) # printed after download is done
|
|
|
|
|
|
|
|
with client:
|
|
|
|
client.loop.run_until_complete(main())
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
Here, we show how to sign in, get information about yourself, send
|
|
|
|
messages, files, getting chats, printing messages, and downloading
|
|
|
|
files.
|
|
|
|
|
|
|
|
You should make sure that you understand what the code shown here
|
|
|
|
does, take note on how methods are called and used and so on before
|
|
|
|
proceeding. We will see all the available methods later on.
|
2019-08-14 00:33:39 +03:00
|
|
|
|
|
|
|
.. important::
|
|
|
|
|
|
|
|
Note that Telethon is an asynchronous library, and as such, you should
|
|
|
|
get used to it and learn a bit of basic `asyncio`. This will help a lot.
|
|
|
|
As a quick start, this means you generally want to write all your code
|
|
|
|
inside some ``async def`` like so:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
client = ...
|
|
|
|
|
|
|
|
async def do_something(me):
|
|
|
|
...
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
# 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.
|
|
|
|
me = await client.get_me()
|
|
|
|
await do_something(me)
|
|
|
|
|
|
|
|
with client:
|
|
|
|
client.loop.run_until_complete(main())
|
|
|
|
|
|
|
|
After you understand this, you may use the ``telethon.sync`` hack if you
|
|
|
|
want do so (see :ref:`compatibility-and-convenience`), but note you may
|
|
|
|
run into other issues (iPython, Anaconda, etc. have some issues with it).
|