Skip to content

Commit

Permalink
fix: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-zahedi committed Mar 28, 2024
1 parent b432f67 commit e610280
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
run: |
VERSION=${{ needs.versioning_pipeline.outputs.version }}
echo "Version: $VERSION"
sed -i "s/__version__ = \"1.0.0\"/__version__ = \"$VERSION\"/g" setup.py
sed -i "s/version='1.0.0',/version='$VERSION',/g" setup.py
- name: Install build
run: python -m pip install build
- name: Build dist
Expand Down
70 changes: 35 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

⭐️ Thanks **everyone** who has starred the project, it means a lot!

This project is to help you use [Telethon](https://docs.telethon.dev/en/stable/index.html).
This project is to help you use [Telethon](https://docs.telethon.dev/en/stable/index.html).

Django-Telethon is an asyncio Python 3 MTProto library to interact with Telegram's API as a user or through a bot account (bot API alternative).

Expand All @@ -31,7 +31,7 @@ Django-Telethon is a session storage implementation backend for Django ORM to us
pip install django-telethon
```

**OR**
**OR**

* You can use the following command to set it up locally so that you can fix bugs or whatever and send pull requests:

Expand All @@ -49,7 +49,7 @@ For better understanding, please read the:

### settings.py


``` python
INSTALLED_APPS = [
# ....
Expand Down Expand Up @@ -115,11 +115,11 @@ Read more (proxy, bot and etc) [Here](https://docs.telethon.dev/en/stable/basic/
from django_telethon.sessions import DjangoSession
from django_telethon.models import App, ClientSession
from telethon.errors import SessionPasswordNeededError

# Use your own values from my.telegram.org
API_ID = 12345
API_HASH = '0123456789abcdef0123456789abcdef'

app, is_created = App.objects.update_or_create(
api_id=API_ID,
api_hash=API_HASH
Expand All @@ -129,7 +129,7 @@ Read more (proxy, bot and etc) [Here](https://docs.telethon.dev/en/stable/basic/
)
telegram_client = TelegramClient(DjangoSession(client_session=cs), app.api_id, app.api_hash)
telegram_client.connect()

if not telegram_client.is_user_authorized():
phone = input('Enter your phone number: ')
telegram_client.send_code_request(phone)
Expand All @@ -138,7 +138,7 @@ Read more (proxy, bot and etc) [Here](https://docs.telethon.dev/en/stable/basic/
telegram_client.sign_in(phone, code)
except SessionPasswordNeededError:
password = input('Enter your password: ')
telegram_client.sign_in(password=password)
telegram_client.sign_in(password=password)
```
#### Doing stuffs
Expand Down Expand Up @@ -171,38 +171,38 @@ async def handler(event):
```shell script
python manage.py runtelegram
```
1. go to [admin panel](http://127.0.0.1:8000/admin/) and [telegram app section](http://127.0.0.1:8000/admin/django_telethon/app/). create a new app. get data from the [your Telegram account](https://my.telegram.org/auth).
1. Request code from telegram:
```python
import requests
import json

url = "http://127.0.0.1:8000/telegram/send-code-request/"

payload = json.dumps({
"phone_number": "+12345678901",
"client_session_name": "name of the client session"
})
headers = {
'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
```
1. Send this request for sign in:
```python
import requests
import json

url = "http://127.0.0.1:8000/telegram/login-user-request/"

payload = json.dumps({
"phone_number": "+12345678901",
"client_session_name": "name of the client session",
Expand All @@ -212,32 +212,32 @@ async def handler(event):
headers = {
'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

```
#### Bot login
#### Bot login
Send this request for sign in:
```python
import requests
import json

url = "http://127.0.0.1:8000/telegram/login-bot-request/"

payload = json.dumps({
"bot_token": "bot token",
"client_session_name": "name of the client session",
})
headers = {
'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

```
Expand Down Expand Up @@ -275,43 +275,43 @@ python manage.py runtelegram
## Listen to events
After login telegram client the signal `telegram_client_registered` is emitted.
After login telegram client the signal `telegram_client_registered` is emitted.
1. You can listen to this signal by using the following code for example put this code to your ```receivers.py``` file in app directory:
```python
from functools import partial
from django.dispatch import receiver
from telethon import events
from django_telethon.signals import telegram_client_registered
async def event_handler(event, client_session):
print(client_session.name, event.raw_text, sep=' | ')
# if you need access to telegram client, you can use event.client
# telegram_client = event.client
await event.respond('!pong')
@receiver(telegram_client_registered)
def receiver_telegram_registered(telegram_client, client_session, *args, **kwargs):
handler = partial(event_handler, client_session=client_session)
telegram_client.add_event_handler(
handler,
events.NewMessage(incoming=True, pattern='ping'),
)
```
1. In the `apps.py` file, add the following code:
```python
from django.apps import AppConfig
class MyAppConfig(AppConfig):
...
def ready(self):
from .receivers import receiver_telegram_registered # noqa: F401
```
Expand Down
21 changes: 13 additions & 8 deletions django_telethon/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
from .models import App, ClientSession, Entity, Login, SentFile, Session, UpdateState


@admin.register(App)
class AppAdmin(admin.ModelAdmin):
list_display = (
'api_id',
'api_hash',
)


@admin.register(ClientSession)
class ClientSessionAdmin(admin.ModelAdmin):
list_display = ['id', 'name', 'login_status']
list_filter = ('login_status',)


@admin.register(Login)
class LoginAdmin(admin.ModelAdmin):
list_display = [
'id',
Expand All @@ -30,6 +33,7 @@ class LoginAdmin(admin.ModelAdmin):
list_filter = ['created_at', 'client_session__name']


@admin.register(Session)
class SessionAdmin(admin.ModelAdmin):
list_display = [
'client_session',
Expand All @@ -43,6 +47,7 @@ class SessionAdmin(admin.ModelAdmin):
list_filter = ['client_session__name']


@admin.register(Entity)
class EntityAdmin(admin.ModelAdmin):
list_display = [
'id',
Expand All @@ -55,9 +60,15 @@ class EntityAdmin(admin.ModelAdmin):
]
raw_id_fields = ['client_session']
list_filter = ['client_session__name']
search_fields = ('hash_value', 'username', 'phone', 'name', )
search_fields = (
'hash_value',
'username',
'phone',
'name',
)


@admin.register(UpdateState)
class UpdateStateAdmin(admin.ModelAdmin):
list_display = [
'id',
Expand All @@ -71,6 +82,7 @@ class UpdateStateAdmin(admin.ModelAdmin):
list_filter = ['client_session__name']


@admin.register(SentFile)
class SentFileAdmin(admin.ModelAdmin):
list_display = [
'id',
Expand All @@ -85,10 +97,3 @@ class SentFileAdmin(admin.ModelAdmin):
list_filter = ['client_session__name']


admin.site.register(App, AppAdmin)
admin.site.register(ClientSession, ClientSessionAdmin)
admin.site.register(Login, LoginAdmin)
admin.site.register(Session, SessionAdmin)
admin.site.register(Entity, EntityAdmin)
admin.site.register(UpdateState, UpdateStateAdmin)
admin.site.register(SentFile, SentFileAdmin)
2 changes: 0 additions & 2 deletions django_telethon/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ def get_telethon_config(key, default=None):

if missing_settings:
raise ValueError(f"'Telethon' {', '.join(missing_settings)} must be set if RABBITMQ_ACTIVE is True.")


1 change: 0 additions & 1 deletion django_telethon/management/commands/runtelegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ async def _entry_point():
logging.exception(e, exc_info=True)



class Command(BaseCommand):
help = 'Run telegram'

Expand Down
Loading

0 comments on commit e610280

Please sign in to comment.