mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-07-10 16:12:22 +03:00
Destroyed Creating a Client (markdown)
parent
c9ee79f4ab
commit
289da02d00
|
@ -1,43 +0,0 @@
|
|||
Before working with Telegram's API, you need to get your own API ID and hash:
|
||||
1. Follow [this link](https://my.telegram.org) and login with your phone number.
|
||||
2. Click under *API Development tools*.
|
||||
3. A *Create new application* window will appear. Fill in your application details. There is no need to enter any *URL*, and only the first two fields (*App title* and *Short name*) can be changed later as far as I'm aware.
|
||||
4. Click on *Create application* at the end.
|
||||
|
||||
Remember that your **API hash is secret** and Telegram won't let you revoke it. Don't post it anywhere!
|
||||
|
||||
Once that's ready, the next step 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
|
||||
# If .sign_in raises SessionPasswordNeeded error, call .sign_in(password=...)
|
||||
# You can import both exceptions from telethon.errors.
|
||||
```
|
||||
|
||||
`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!
|
Loading…
Reference in New Issue
Block a user