ali-zahedi / django-telethon Goto Github PK
View Code? Open in Web Editor NEWIntegrate Django with Telethon(Pure Python 3 MTProto API Telegram client library, for bots too!).
License: MIT License
Integrate Django with Telethon(Pure Python 3 MTProto API Telegram client library, for bots too!).
License: MIT License
Hi.
Nice project.
How to send a telegram message when my project starts or using a celery task?
Hi, this is an architecture question and not an issue.
I'm building a django app with a bot that replies to users and now and then sends random promo messages to them.
For message replies is straightforward as we just listen to events as in your example, my question is imagine that a product is created, fires a django signal and I want to send a message to the user, is there a way we can get the telegram_client instance from 'telegram_client_registered' and use it to send messages? Or do I need to instantiate a new TelegramClient just to send messages?
Hi, I already logged in with send-code-request and send-code-request. The session is already in DB. But when I try:
....
cs = ClientSession.objects.update_or_create(name='Sesion1')
session = DjangoSession(client_session=cs)
telegram_client = TelegramClient(session, api_id=int(app.api_id), api_hash=app.api_hash)
....
I get the following error back:
Field 'id' expected a number but got <ClientSession: Sesion1>
What can it be?
django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_telethon_updatestate_pkey"
backend-1 | Traceback (most recent call last):
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
backend-1 | response = get_response(request)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
backend-1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/contrib/admin/options.py", line 688, in wrapper
backend-1 | return self.admin_site.admin_view(view)(*args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/utils/decorators.py", line 134, in _wrapper_view
backend-1 | response = view_func(request, *args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/views/decorators/cache.py", line 62, in _wrapper_view_func
backend-1 | response = view_func(request, *args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/contrib/admin/sites.py", line 242, in inner
backend-1 | return view(request, *args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/utils/decorators.py", line 46, in _wrapper
backend-1 | return bound_method(*args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/utils/decorators.py", line 134, in _wrapper_view
backend-1 | response = view_func(request, *args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/contrib/admin/options.py", line 1959, in changelist_view
backend-1 | response = self.response_action(
backend-1 | ^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/contrib/admin/options.py", line 1593, in response_action
backend-1 | response = func(self, request, queryset)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/home/app/backend/telegram/admin.py", line 18, in check_obj
backend-1 | check_user(obj.id)
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/celery/local.py", line 182, in call
backend-1 | return self._get_current_object()(*a, **kw)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/celery/app/task.py", line 411, in call
backend-1 | return self.run(*args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/home/app/backend/telegram/tasks.py", line 41, in check_user
backend-1 | checking()
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/asgiref/sync.py", line 277, in call
backend-1 | return call_result.result()
backend-1 | ^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 449, in result
backend-1 | return self.__get_result()
backend-1 | ^^^^^^^^^^^^^^^^^^^
backend-1 | File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
backend-1 | raise self._exception
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/asgiref/sync.py", line 353, in main_wrap
backend-1 | result = await self.awaitable(*args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/home/app/backend/telegram/tasks.py", line 37, in checking
backend-1 | async with telegram_client:
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/telethon/client/auth.py", line 660, in aexit
backend-1 | await self.disconnect()
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/telethon/client/telegrambaseclient.py", line 732, in _disconnect_coro
backend-1 | self._save_states_and_entities()
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/telethon/client/telegrambaseclient.py", line 697, in _save_states_and_entities
backend-1 | self.session.set_update_state(0, types.updates.State(**ss, unread_count=0))
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django_telethon/sessions.py", line 105, in set_update_state
backend-1 | self.client_session.updatestate_set.update_or_create(
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/fields/related_descriptors.py", line 825, in update_or_create
backend-1 | return super(RelatedManager, self.db_manager(db)).update_or_create(**kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
backend-1 | return getattr(self.get_queryset(), name)(*args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 949, in update_or_create
backend-1 | obj, created = self.select_for_update().get_or_create(defaults, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 923, in get_or_create
backend-1 | return self.create(**params), True
backend-1 | ^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 658, in create
backend-1 | obj.save(force_insert=True, using=self.db)
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/base.py", line 814, in save
backend-1 | self.save_base(
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/base.py", line 877, in save_base
backend-1 | updated = self._save_table(
backend-1 | ^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/base.py", line 1020, in _save_table
backend-1 | results = self._do_insert(
backend-1 | ^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/base.py", line 1061, in _do_insert
backend-1 | return manager._insert(
backend-1 | ^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
backend-1 | return getattr(self.get_queryset(), name)(*args, **kwargs)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/query.py", line 1805, in _insert
backend-1 | return query.get_compiler(using=using).execute_sql(returning_fields)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1822, in execute_sql
backend-1 | cursor.execute(sql, params)
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/backends/utils.py", line 102, in execute
backend-1 | return super().execute(sql, params)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/backends/utils.py", line 67, in execute
backend-1 | return self._execute_with_wrappers(
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
backend-1 | return executor(sql, params, many, context)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/backends/utils.py", line 84, in _execute
backend-1 | with self.db.wrap_database_errors:
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/utils.py", line 91, in exit
backend-1 | raise dj_exc_value.with_traceback(traceback) from exc_value
backend-1 | File "/root/.cache/pypoetry/virtualenvs/backend-qadtrZ8a-py3.12/lib/python3.12/site-packages/django/db/backends/utils.py", line 89, in _execute
backend-1 | return self.cursor.execute(sql, params)
backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-1 | django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_telethon_updatestate_pkey"
backend-1 | DETAIL: Key (id)=(0) already exists.
backend-1 |
main problem with /telethon/client/telegrambaseclient.py", line 697, in _save_states_and_entities
telethon use index 0,
i can't find the file "manage.py". where is it?
I'm trying to activate the bot (the code has already been received, the session has been created) using the code from the example and substituting my values for the session name and token
import requests
import json
url = "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)
I am getting this error
[27/Sep/2022 21:45:14] "GET /tgadmin/jsi18n/ HTTP/1.1" 200 3343
Internal Server Error: /telegram/login-bot-request/
Traceback (most recent call last):
File "D:\home\dev\python\mebot_origin\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "D:\home\dev\python\mebot_origin\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\home\dev\python\mebot_origin\venv\lib\site-packages\django_telethon\views.py", line 86, in login_bot_view
if client_session.login_status != LoginStatus.LOGIN_REQUIRED:
AttributeError: 'tuple' object has no attribute 'login_status'
What am I doing wrong?
I get this error when i try to migrate database with mysql for the first time:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, django_telethon, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying django_telethon.0001_initial...Traceback (most recent call last):
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute
return self.cursor.execute(query, args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 330, in query
db.query(q)
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py", line 255, in query
mysql.connection.query(self, query)
MySQLdb.OperationalError: (1170, "BLOB/TEXT column 'md5_digest' used in key specification without a key length")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Behrad\Projects\healfitbot\manage.py", line 22, in
main()
File "C:\Users\Behrad\Projects\healfitbot\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management_init.py", line 442, in execute_from_command_line
utility.execute()
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management_init.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 458, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 106, in wrapper
res = handle_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\commands\migrate.py", line 356, in handle
post_migrate_state = executor.migrate(
^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\executor.py", line 135, in migrate
state = self._migrate_all_forwards(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\executor.py", line 167, in _migrate_all_forwards
state = self.apply_migration(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\migrations\executor.py", line 249, in apply_migration
with self.connection.schema_editor(
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\base\schema.py", line 166, in exit
self.execute(sql)
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\base\schema.py", line 201, in execute
cursor.execute(sql, params)
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 102, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py", line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute
return self.cursor.execute(query, args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
db.query(q)
File "C:\Users\Behrad\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py", line 255, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'md5_digest' used in key specification without a key length")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.