Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SergSm opened this issue Jul 27, 2022 · 8 comments
Open

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

SergSm opened this issue Jul 27, 2022 · 8 comments

Comments

@SergSm
Copy link

SergSm commented Jul 27, 2022

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

@stanislavec
Copy link

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

@SergSm
Copy link
Author

SergSm commented Aug 8, 2022

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 SergSm closed this as completed Aug 8, 2022
@SergSm SergSm reopened this Aug 8, 2022
@SergSm
Copy link
Author

SergSm commented Aug 15, 2022

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

@ohld
Copy link
Owner

ohld commented Nov 12, 2022

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

@ohld
Copy link
Owner

ohld commented Nov 26, 2022

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

@SergSm
Copy link
Author

SergSm commented Nov 28, 2022

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

@VeryBigSad
Copy link
Contributor

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.

@AntonLoktionov
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants