Updated README, minor changes and pip ready

The module is now ready to be installed via pip
README.md has been updated to reflect these changes
Minor changes to the interactive client example
Versioning is now done by editting TelegramClient's
__version__, rather than looking for the string
This commit is contained in:
Lonami 2016-09-18 11:59:12 +02:00
parent 6642f73a3d
commit d8757af5c5
6 changed files with 156 additions and 25 deletions

4
MANIFEST.in Normal file
View File

@ -0,0 +1,4 @@
include LICENSE
include README.md
recursive-include telethon *

View File

@ -4,9 +4,10 @@ The project's **core only** is based on TLSharp, a C# Telegram client implementa
# Table of contents
- [Why Telethon?](#why-telethon)
- [Requirements](#requirements)
- [Python modules](#python-modules)
- [Obtaining your `API ID` and `Hash`](#obtaining-your-api-id-and-hash)
- [Obtaining your Telegram `API ID` and `Hash`](#obtaining-your-telegram-api-id-and-hash)
- [Installing Telethon](#installing-telethon)
- [Via `pip`](#installing-telethon-via-pip)
- [Manually](#installing-telethon-manually)
- [Running Telethon](#running-telethon)
- [Advanced uses](#advanced-uses)
- [Using more than just `TelegramClient`](#using-more-than-just-telegramclient)
@ -31,26 +32,58 @@ Do you want check for new messages at a given time and find relevant ones? Write
Hungry for more API calls which the `TelegramClient` class doesn't _seem_ to have implemented?
Please read [this section](#using-more-than-just-telegramclient).
## Requirements
### Python modules
This project requires the following Python modules, which can be installed by issuing `sudo -H pip3 install <module>` on a
Linux terminal:
- `pyaes` ([GitHub](https://github.com/ricmoo/pyaes), [package index](https://pypi.python.org/pypi/pyaes))
### Obtaining your `API ID` and `Hash`
## Obtaining your Telegram `API ID` and `Hash`
In order to use Telethon, you first need to obtain your very 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 long as I'm aware.
4. Click on `Create application` at the end. Now that you have the `API ID` and `Hash`,
head to `api/` directory and create a copy of the `settings_example` file, naming it `settings` (lowercase!).
Then fill the file with the corresponding values (your `api_id`, `api_hash` and phone number in international format).
4. Click on `Create application` at the end.
Now that you know your `API ID` and `Hash`, you can continue installing Telethon.
## Installing Telethon
### Installing Telethon via `pip`
On a terminal, issue the following command:
```sh
sudo -H pip install telethon
```
You're ready to go.
### Installing Telethon manually
1. Install the required `pyaes` module: `sudo -H pip install pyaes`
([GitHub](https://github.com/ricmoo/pyaes), [package index](https://pypi.python.org/pypi/pyaes))
2. Clone Telethon's GitHub repository: `git clone https://github.com/LonamiWebs/Telethon.git`
3. Enter the cloned repository: `cd Telethon`
4. Run the code generator: `python3 telethon_generator/tl_generator.py`
5. Done!
## Running Telethon
First of all, you need to run the `tl_generator.py` (located under `telethon-generator/`) by issuing
`python3 tl_generator.py`. This will generate all the TLObjects from the given `scheme.tl` file.
When it's done, you can run `python3 try_telethon.py` to start the interactive example.
If you've installed Telethon via pip, launch an interactive python3 session and enter the following:
```python
>>> from telethon import InteractiveTelegramClient
>>> # 'sessionid' can be 'yourname'. It'll be saved as yourname.session
>>> # Also (obviously) replace the api_id and api_hash with your values
...
>>> client = InteractiveTelegramClient('sessionid', '+34600000000',
... api_id=12345, api_hash='0123456789abcdef0123456789abcdef')
┌───────────────────────────────────────────────────────────┐
│ Initialization │
└───────────────────────────────────────────────────────────┘
Initializing interactive example...
Connecting to Telegram servers...
>>> client.run()
```
If, on the other hand, you've installed Telethon manually, head to the `api/` directory and create a
copy of the `settings_example` file, naming it `settings` (lowercase!). Then fill the file with the
corresponding values (your `api_id`, `api_hash` and phone number in international format).
Then, simply run `python3 try_telethon.py` to start the interactive example.
## Advanced uses
### Using more than just `TelegramClient`
@ -137,8 +170,3 @@ replacing the one you can find in this same directory by the updated one. Don't
afterwards and specifying the new layer number to be used when creating the `TelegramClient`.
If the changes weren't too big, everything should still work the same way as it did before; but with extra features.
## Plans for the future
If everything works well, this probably ends up being a Python package :)
But as of now, and until that happens, help is highly appreciated!

88
setup.py Normal file
View File

@ -0,0 +1,88 @@
"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""
from telethon import TelegramClient
# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path
here = path.abspath(path.dirname(__file__))
# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(
name='Telethon',
# Versions should comply with PEP440.
version=TelegramClient.__version__,
description="Python3 Telegram's client implementation with full access to its API",
long_description=long_description,
# The project's main homepage.
url='https://github.com/LonamiWebs/Telethon',
download_url='https://github.com/LonamiWebs/Telethon/releases',
# Author details
author='Lonami Exo',
author_email='totufals@hotmail.com',
# Choose your license
license='MIT',
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 3 - Alpha',
# Indicate who your project is intended for
'Intended Audience :: Developers',
'Topic :: Communications :: Chat',
# Pick your license as you wish (should match "license" above)
'License :: OSI Approved :: MIT License',
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
# What does your project relate to?
keywords='telegram api chat client mtproto',
# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
packages=find_packages(exclude=[
'telethon_generator',
'telethon_tests',
'run_tests.py',
'try_telethon.py']),
# List run-time dependencies here. These will be installed by pip when
# your project is installed.
install_requires=['pyaes'],
# To provide executable scripts, use entry points in preference to the
# "scripts" keyword. Entry points provide cross-platform support and allow
# pip to create the appropriate form of executable for the target platform.
entry_points={
'console_scripts': [
'gen_tl = tl_generator:clean_and_generate',
],
}
)

View File

@ -176,7 +176,7 @@ class BadMessageError(Exception):
ErrorMessages = {
16: 'msg_id too low (most likely, client time is wrong it would be worthwhile to '
'synchronize it using msg_id notifications and re-send the original message '
'with the “correct” msg_id or wrap it in a container with a new msg_id if the '
'with the "correct" msg_id or wrap it in a container with a new msg_id if the '
'original message had waited too long on the client to be transmitted).',
17: 'msg_id too high (similar to the previous case, the client time has to be '

View File

@ -211,7 +211,15 @@ class InteractiveTelegramClient(TelegramClient):
@staticmethod
def update_handler(update_object):
if type(update_object) is UpdateShortMessage:
print('[User #{} sent {}]'.format(update_object.user_id, update_object.message))
if update_object.out:
print('You sent {} to user #{}'.format(update_object.message, update_object.user_id))
else:
print('[User #{} sent {}]'.format(update_object.user_id, update_object.message))
elif type(update_object) is UpdateShortChatMessage:
print('[Chat #{} sent {}]'.format(update_object.chat_id, update_object.message))
if update_object.out:
print('You sent {} to chat #{}'.format(update_object.message, update_object.chat_id))
else:
print('[Chat #{}, user #{} sent {}]'.format(update_object.chat_id,
update_object.from_id,
update_object.message))

View File

@ -34,6 +34,9 @@ from telethon.tl.all_tlobjects import layer
class TelegramClient:
# Current TelegramClient version
__version__ = '0.5'
# region Initialization
def __init__(self, session_user_id, api_id, api_hash):
@ -73,7 +76,7 @@ class TelegramClient:
query = InitConnectionRequest(api_id=self.api_id,
device_model=platform.node(),
system_version=platform.system(),
app_version='0.4',
app_version=self.__version__,
lang_code='en',
query=GetConfigRequest())