Open SergSm opened 2 years ago
It's great question! Just one hour ago i took template and set it to pre-release version 20.0a2
The main problem is to rewrite tgbot/dispatcher.py
to application instance, using Application.builder()
Updating items in database using Models need s@sync_to_async
decorator to be set in tgbot/models.py
I'm not Professional python developer, but it was a great and not such difficult experience So, you can try!
P.S. yep, it's fully async, so handlers need to be async/awaited
It's great question! Just one hour ago i took template and set it to pre-release version 20.0a2
The main problem is to rewrite
tgbot/dispatcher.py
to application instance, usingApplication.builder()
Updating items in database using Models need s@sync_to_async
decorator to be set intgbot/models.py
I'm not Professional python developer, but it was a great and not such difficult experience So, you can try!
P.S. yep, it's fully async, so handlers need to be async/awaited
Thanks for your answer!
Could you show the code?
I added django.setup()
in my bot to access django and rewrote all the code related to database in an async way
https://gist.github.com/SergSm/68783e59720afdd57114a179fdd835ab
It works somehow but I think that I misunderstand something and I don’t have to run bot in a separate webserver process.
Is there another way to trigger bot without need for running a separate webserver process?
I found the way to serve external requests by running uvicorn programatically in the same async context like that:
async with application_tg:
await application_tg.start()
await server.serve() # uvicorn
await application_tg.stop()
In addition to the said above:
I can't access python-telegram-bot Application() instance from views.py
because it is inside of main() function.
I tried to use global
keyword but without any results:
https://gist.github.com/SergSm/6843fadf505b826f83a10bf7eebc3fa0
Please make PR with ideas! Should be useful for the community
To make this work we need to wait at least django 4.2 to be released since Django ORM is still only sync. Also the new PTB 20 has new backward incompatible syntax and still not have a stable release
Maybe.. I managed to make it work in a some perverted way but we should definitely wait for a stable PTB release. Django 4.2 is not a must have but it will decrease a number of some ugly hacks related to a database
PTB v20.0 was released a few days ago! Although I think we should wait for Django 4.2, since it'll contain lots of useful async ORM methods (.asave(), .aadd(), .aget(), full list of model changes here), which, as @SergSm said, will decrease the amount of ugly hacks. It's expected April 2023, so 3 months from now.
Any news about updating to PTB and Django 4.2 or 5 versions?
It would be nice to rewrite the template according to a new python-telegram-bot ^20.
The new 20x version of python-telegram-bot is fully asynchronous