django-telegram-bot icon indicating copy to clipboard operation
django-telegram-bot copied to clipboard

[async] Upgrading to python-telegram-bot ^20 ?

Open SergSm opened this issue 2 years ago • 8 comments

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

SergSm avatar Jul 27 '22 04:07 SergSm

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

stanislavec avatar Aug 07 '22 16:08 stanislavec

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

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?

SergSm avatar Aug 08 '22 04:08 SergSm

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

SergSm avatar Aug 15 '22 03:08 SergSm

Please make PR with ideas! Should be useful for the community

ohld avatar Nov 12 '22 17:11 ohld

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

ohld avatar Nov 26 '22 10:11 ohld

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

SergSm avatar Nov 28 '22 03:11 SergSm

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.

VeryBigSad avatar Jan 03 '23 11:01 VeryBigSad

Any news about updating to PTB and Django 4.2 or 5 versions?

AntonLoktionov avatar Jan 13 '24 12:01 AntonLoktionov