Comments (7)
Ok. Thanks for suggestion. Will do it this weekend and let u know.
from python-telegram.
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.
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.
👍 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.
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.
Yes, exactly :) Also, change updateType
to type
, please :) (Maybe it will be more convenient to continue in the PR)
from python-telegram.
Fixed in #19
from python-telegram.
Related Issues (20)
- change channel username using link order active usernames? HOT 1
- Failed to connect when trying to deploy on pythonanywhere HOT 1
- how to detect message reaction update
- Support Apple Silicon (arm64) HOT 3
- sometimes message are not sent. HOT 13
- echo-bot and loop HOT 3
- Error occurred when tg.stop() HOT 1
- Add `py.typed` file HOT 2
- [Question] A way to understand if a message has been read (given the message_id and the chat_id) HOT 3
- How to create a new conversation ? HOT 1
- AttributeError: module 'signal' has no attribute 'SIGQUIT' HOT 2
- use parse_mode(MarkdownV2) in send_message HOT 1
- Not able to send message on Telegram channel HOT 2
- How to download photo from messagePhoto? HOT 1
- Where I can find last supported tdlib version HOT 3
- Does this client include `delete_file` functionality?
- tdlib fails with SIGSEGV in a simple* python script HOT 1
- bro how to run project
- issues with version
- issue related to windows
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-telegram.