2018-01-05 02:59:53 +03:00
|
|
|
.. _telegram-client:
|
|
|
|
|
|
|
|
==============
|
|
|
|
TelegramClient
|
|
|
|
==============
|
|
|
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
************
|
|
|
|
|
2018-03-14 21:38:36 +03:00
|
|
|
.. note::
|
|
|
|
|
2018-06-19 22:24:26 +03:00
|
|
|
Make sure to use the friendly methods described in :ref:`telethon-client`!
|
2018-05-17 13:00:22 +03:00
|
|
|
This section is just an introduction to using the client, but all the
|
2018-06-19 22:24:26 +03:00
|
|
|
available methods are in the :ref:`telethon-client` reference, including
|
2018-05-17 13:00:22 +03:00
|
|
|
detailed descriptions to what they do.
|
2018-03-14 21:38:36 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
The :ref:`TelegramClient <telethon-client>` is the
|
|
|
|
central class of the library, the one you will be using most of the time. For
|
|
|
|
this reason, it's important to know what it offers.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
Since we're working with Python, one must not forget that we can do
|
|
|
|
``help(client)`` or ``help(TelegramClient)`` at any time for a more
|
|
|
|
detailed description and a list of all the available methods. Calling
|
|
|
|
``help()`` from an interactive Python session will always list all the
|
|
|
|
methods for any object, even yours!
|
|
|
|
|
|
|
|
Interacting with the Telegram API is done through sending **requests**,
|
|
|
|
this is, any "method" listed on the API. There are a few methods (and
|
2018-06-20 12:05:33 +03:00
|
|
|
growing!) on the :ref:`TelegramClient <telethon-client>` class that abstract
|
|
|
|
you from the need of manually importing the requests you need.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-22 15:44:59 +03:00
|
|
|
For instance, retrieving your own user can be done in a single line
|
2018-06-25 22:14:58 +03:00
|
|
|
(assuming you have ``from telethon import sync`` or ``import telethon.sync``):
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
myself = client.get_me()
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
Internally, this method has sent a request to Telegram, who replied with
|
|
|
|
the information about your own user, and then the desired information
|
|
|
|
was extracted from their response.
|
|
|
|
|
|
|
|
If you want to retrieve any other user, chat or channel (channels are a
|
|
|
|
special subset of chats), you want to retrieve their "entity". This is
|
|
|
|
how the library refers to either of these:
|
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
# The method will infer that you've passed an username
|
|
|
|
# It also accepts phone numbers, and will get the user
|
|
|
|
# from your contact list.
|
|
|
|
lonami = client.get_entity('lonami')
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
The so called "entities" are another important whole concept on its own,
|
2018-01-20 13:47:17 +03:00
|
|
|
but for now you don't need to worry about it. Simply know that they are
|
|
|
|
a good way to get information about an user, chat or channel.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
Many other common methods for quick scripts are also available:
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
# Note that you can use 'me' or 'self' to message yourself
|
|
|
|
client.send_message('username', 'Hello World from Telethon!')
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
# .send_message's parse mode defaults to markdown, so you
|
|
|
|
# can use **bold**, __italics__, [links](https://example.com), `code`,
|
|
|
|
# and even [mentions](@username)/[mentions](tg://user?id=123456789)
|
|
|
|
client.send_message('username', '**Using** __markdown__ `too`!')
|
2018-04-06 20:00:21 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
client.send_file('username', '/home/myself/Pictures/holidays.jpg')
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
# The utils package has some goodies, like .get_display_name()
|
|
|
|
from telethon import utils
|
|
|
|
for message in client.iter_messages('username', limit=10):
|
|
|
|
print(utils.get_display_name(message.sender), message.message)
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
# Dialogs are the conversations you have open
|
|
|
|
for dialog in client.get_dialogs(limit=10):
|
|
|
|
print(dialog.name, dialog.draft.text)
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
# Default path is the working directory
|
|
|
|
client.download_profile_photo('username')
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
# Call .disconnect() when you're done
|
|
|
|
client.disconnect()
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
Remember that you can call ``.stringify()`` to any object Telegram returns
|
|
|
|
to pretty print it. Calling ``str(result)`` does the same operation, but on
|
|
|
|
a single line.
|
|
|
|
|
|
|
|
|
|
|
|
Available methods
|
|
|
|
*****************
|
|
|
|
|
2018-04-06 20:00:21 +03:00
|
|
|
The :ref:`reference <telethon-package>` lists all the "handy" methods
|
2018-06-20 12:05:33 +03:00
|
|
|
available for you to use in the :ref:`TelegramClient <telethon-client>` class.
|
|
|
|
These are simply wrappers around the "raw" Telegram API, making it much more
|
|
|
|
manageable and easier to work with.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
Please refer to :ref:`accessing-the-full-api` if these aren't enough,
|
|
|
|
and don't be afraid to read the source code of the InteractiveTelegramClient_
|
|
|
|
or even the TelegramClient_ itself to learn how it works.
|
|
|
|
|
2018-06-19 22:24:26 +03:00
|
|
|
See the mentioned :ref:`telethon-client` to find the available methods.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
.. _InteractiveTelegramClient: https://github.com/LonamiWebs/Telethon/blob/master/telethon_examples/interactive_telegram_client.py
|
|
|
|
.. _TelegramClient: https://github.com/LonamiWebs/Telethon/blob/master/telethon/telegram_client.py
|