From 57a4fb7c40025131c92a09b3712dcf6432f257ef Mon Sep 17 00:00:00 2001 From: Lonami Date: Sun, 10 Sep 2017 14:15:04 +0200 Subject: [PATCH] Created Creating a Client (markdown) --- Creating-a-Client.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Creating-a-Client.md diff --git a/Creating-a-Client.md b/Creating-a-Client.md new file mode 100644 index 0000000..2a26f7a --- /dev/null +++ b/Creating-a-Client.md @@ -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! \ No newline at end of file