2018-01-20 13:47:17 +03:00
|
|
|
.. _getting-started:
|
|
|
|
|
2017-11-20 07:12:31 +03:00
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
===============
|
|
|
|
Getting Started
|
|
|
|
===============
|
2017-11-20 07:12:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
Simple Installation
|
2018-01-05 02:59:53 +03:00
|
|
|
*******************
|
2017-11-20 07:12:31 +03:00
|
|
|
|
2018-01-12 12:08:40 +03:00
|
|
|
``pip3 install telethon``
|
2017-11-20 07:12:31 +03:00
|
|
|
|
2017-11-20 12:26:31 +03:00
|
|
|
**More details**: :ref:`installation`
|
2017-11-20 07:12:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
Creating a client
|
2018-01-05 02:59:53 +03:00
|
|
|
*****************
|
2017-11-20 07:12:31 +03:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from telethon import TelegramClient
|
|
|
|
|
|
|
|
# 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)
|
2018-01-11 14:45:59 +03:00
|
|
|
client.start()
|
2017-11-20 07:12:31 +03:00
|
|
|
|
2017-11-20 12:26:31 +03:00
|
|
|
**More details**: :ref:`creating-a-client`
|
2017-11-20 07:12:31 +03:00
|
|
|
|
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
Basic Usage
|
|
|
|
***********
|
|
|
|
|
2017-11-20 07:12:31 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
# Getting information about yourself
|
|
|
|
print(client.get_me().stringify())
|
|
|
|
|
|
|
|
# Sending a message (you can use 'me' or 'self' to message yourself)
|
|
|
|
client.send_message('username', 'Hello World from Telethon!')
|
2017-11-20 07:12:31 +03:00
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
# Sending a file
|
2017-11-20 07:12:31 +03:00
|
|
|
client.send_file('username', '/home/myself/Pictures/holidays.jpg')
|
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
# Retrieving messages from a chat
|
|
|
|
from telethon import utils
|
2018-04-25 10:55:34 +03:00
|
|
|
for message in client.iter_messages('username', limit=10):
|
2018-01-20 13:47:17 +03:00
|
|
|
print(utils.get_display_name(message.sender), message.message)
|
|
|
|
|
|
|
|
# Listing all the dialogs (conversations you have open)
|
|
|
|
for dialog in client.get_dialogs(limit=10):
|
2018-06-16 22:51:13 +03:00
|
|
|
print(utils.get_display_name(dialog.entity), dialog.draft.text)
|
2018-01-20 13:47:17 +03:00
|
|
|
|
|
|
|
# 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():
|
2018-04-25 10:55:34 +03:00
|
|
|
messages = client.get_messages('username')
|
2017-11-20 07:12:31 +03:00
|
|
|
client.download_media(messages[0])
|
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
**More details**: :ref:`telegram-client`
|
2018-01-20 13:47:17 +03:00
|
|
|
|
2018-06-19 22:24:26 +03:00
|
|
|
See :ref:`telethon-client` for all available friendly methods.
|
2018-05-17 13:00:22 +03:00
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
|
2018-03-24 14:41:42 +03:00
|
|
|
Handling Updates
|
|
|
|
****************
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from telethon import events
|
|
|
|
|
|
|
|
# We need to have some worker running
|
|
|
|
client.updates.workers = 1
|
|
|
|
|
|
|
|
@client.on(events.NewMessage(incoming=True, pattern='(?i)hi'))
|
|
|
|
def handler(event):
|
|
|
|
event.reply('Hello!')
|
|
|
|
|
|
|
|
# If you want to handle updates you can't let the script end.
|
|
|
|
input('Press enter to exit.')
|
|
|
|
|
|
|
|
**More details**: :ref:`working-with-updates`
|
|
|
|
|
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
----------
|
|
|
|
|
|
|
|
You can continue by clicking on the "More details" link below each
|
|
|
|
snippet of code or the "Next" button at the bottom of the page.
|