Giter VIP home page Giter VIP logo

Comments (8)

VeryBigSad avatar VeryBigSad commented on August 20, 2024 4

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.

from django-telegram-bot.

stanislavec avatar stanislavec commented on August 20, 2024 1

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

from django-telegram-bot.

ohld avatar ohld commented on August 20, 2024 1

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

from django-telegram-bot.

SergSm avatar SergSm commented on August 20, 2024

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?

from django-telegram-bot.

SergSm avatar SergSm commented on August 20, 2024

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

from django-telegram-bot.

ohld avatar ohld commented on August 20, 2024

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

from django-telegram-bot.

SergSm avatar SergSm commented on August 20, 2024

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

from django-telegram-bot.

AntonLoktionov avatar AntonLoktionov commented on August 20, 2024

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

from django-telegram-bot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.