Created Creating a Client (markdown)

Lonami 2017-09-10 14:15:04 +02:00
parent 1120b00228
commit 57a4fb7c40

33
Creating-a-Client.md Normal file

@ -0,0 +1,33 @@
The very first thing you want to do is to create a `TelegramClient`. This class will be your main interface with Telegram's API, and creating one is very simple:
```python
from telethon import TelegramClient
# Use your own values here
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
phone_number = '+34600000000'
client = TelegramClient('some_name', api_id, api_hash)
```
Note that `'some_name'` will be used to save your session (persistent information such as access key and others) as `'some_name.session'` in your disk. This is simply a JSON file which you can (but shouldn't) modify.
Before using the client, you must be connected to Telegram. Doing so is very easy:
```python
client.connect() # Must return True, otherwise, try again
```
You may or may not be authorized yet. You must be authorized before you're able to send any request:
```python
client.is_user_authorized() # Returns True if you can send requests
```
If you're not authorized, you need to `.sign_in()`:
```python
client.send_code_request(phone_number)
myself = client.sign_in(phone_number, input('Enter code: '))
# If .sign_in raises PhoneNumberUnoccupiedError, use .sign_up instead
```
`myself` is your Telegram user. You can view all the information about yourself by doing `print(myself.stringify())`. You're now ready to use the `client` as you wish!