Giter Site home page Giter Site logo

Comments (7)

sekarpdkt avatar sekarpdkt commented on May 18, 2024 1

Ok. Thanks for suggestion. Will do it this weekend and let u know.

from python-telegram.

alexander-akhmetov avatar alexander-akhmetov commented on May 18, 2024

Hi,

or we can create another method for add_message_handler like add_call_handlerand append functions to new list like _call_handler. In else condition use it.

It would be nice to have this! Maybe it's better to name this method like add_update_handler and not to call it in the else condition, but every time for every update (there is already an _update_handlers list for that):

   
    def add_update_handler(self, func: Callable):
       # later here we can add a second parameter: type
       # to register handlers for specific types
        if func not in self._update_handlers:
            self._message_handlers.append(func)

    def _run_handlers(self, update: Dict[Any, Any]) -> None:
        if update.get('@type') == 'updateNewMessage':
            for handler in self._message_handlers:
                self._workers_queue.put(
                    (handler, update),
                    timeout=self._queue_put_timeout,
                )
        
        for handler in self._update_handlers:
                self._workers_queue.put(
                    (handler, update),
                    timeout=self._queue_put_timeout,
                )

If you have time, you can create a PR, if not, I will assign this issue to myself. :)

from python-telegram.

sekarpdkt avatar sekarpdkt commented on May 18, 2024

I did this way. If ok, will send a PR

self._update_handlers: List[Callable] = {}

I declared it as a dictionary. Then


    def _run_handlers(self, update: Dict[Any, Any]) -> None:
        if update.get('@type') == 'updateNewMessage':
            for handler in self._message_handlers:
                self._workers_queue.put(
                    (handler, update),
                    timeout=self._queue_put_timeout,
                )
        try:
            for handler in self._update_handlers[update.get('@type')]:
                    self._workers_queue.put(
                        (handler, update),
                        timeout=self._queue_put_timeout,
                    )
                    print(update.get('@type'))

        except KeyError:
            pass;
        
    
   
    def add_update_handler(self, updateType,func: Callable):
        try:
            if func not in self._update_handlers[updateType]:
                self._update_handlers[updateType].append(func)
        except KeyError:
            self._update_handlers[updateType]=[]
            self._update_handlers[updateType].append(func)
            
    def add_message_handler(self, func: Callable) -> None:
        if func not in self._message_handlers:
            self._message_handlers.append(func)

from python-telegram.

alexander-akhmetov avatar alexander-akhmetov commented on May 18, 2024

👍 Overall it looks nice, exactly what I meant in the comment # later here we can add a second parameter: type to register handlers for specific types.

I would try to avoid big try-except block in the _run_handlers method and used self._update_handlers.get(update_type, []) instead. And I think add_message_handler can add functions to the _update_handlers with a relevant type.

from python-telegram.

sekarpdkt avatar sekarpdkt commented on May 18, 2024

Ok.. got the point. As add update will be called only once, it should be ok. Where as run handlers will handle all calls, keeping it light weight is what you mean, I think.


    def _run_handlers(self, update: Dict[Any, Any]) -> None:

        for handler in self._update_handlers.get(update.get('@type'),[]):
                self._workers_queue.put(
                    (handler, update),
                    timeout=self._queue_put_timeout,
                )

    def add_update_handler(self, updateType,func: Callable):
        try:
            if func not in self._update_handlers[updateType]:
                self._update_handlers[updateType].append(func)
        except KeyError:
            self._update_handlers[updateType]=[]
            self._update_handlers[updateType].append(func)
            
    def add_message_handler(self, func: Callable) -> None:
        self.add_update_handler('updateNewMessage',func);
            

from python-telegram.

alexander-akhmetov avatar alexander-akhmetov commented on May 18, 2024

Yes, exactly :) Also, change updateType to type, please :) (Maybe it will be more convenient to continue in the PR)

from python-telegram.

alexander-akhmetov avatar alexander-akhmetov commented on May 18, 2024

Fixed in #19

from python-telegram.

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.