mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-23 15:54:12 +03:00
Add add_event_handler and deprecate add_update_handler
This commit is contained in:
parent
1179c9e21b
commit
b93e1b5f50
|
@ -1,7 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
import warnings
|
|
||||||
from datetime import timedelta, datetime
|
from datetime import timedelta, datetime
|
||||||
from signal import signal, SIGINT, SIGTERM, SIGABRT
|
from signal import signal, SIGINT, SIGTERM, SIGABRT
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
@ -589,24 +588,6 @@ class TelegramBareClient:
|
||||||
self.updates.process(self(GetStateRequest()))
|
self.updates.process(self(GetStateRequest()))
|
||||||
self._last_state = datetime.now()
|
self._last_state = datetime.now()
|
||||||
|
|
||||||
def add_update_handler(self, handler):
|
|
||||||
"""Adds an update handler (a function which takes a TLObject,
|
|
||||||
an update, as its parameter) and listens for updates"""
|
|
||||||
if self.updates.workers is None:
|
|
||||||
warnings.warn(
|
|
||||||
"You have not setup any workers, so you won't receive updates."
|
|
||||||
" Pass update_workers=4 when creating the TelegramClient,"
|
|
||||||
" or set client.self.updates.workers = 4"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.updates.handlers.append(handler)
|
|
||||||
|
|
||||||
def remove_update_handler(self, handler):
|
|
||||||
self.updates.handlers.remove(handler)
|
|
||||||
|
|
||||||
def list_update_handlers(self):
|
|
||||||
return self.updates.handlers[:]
|
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region Constant read
|
# region Constant read
|
||||||
|
|
|
@ -7,6 +7,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import warnings
|
||||||
from collections import OrderedDict, UserList
|
from collections import OrderedDict, UserList
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
@ -32,7 +33,7 @@ except ImportError:
|
||||||
hachoir = None
|
hachoir = None
|
||||||
|
|
||||||
from . import TelegramBareClient
|
from . import TelegramBareClient
|
||||||
from . import helpers, utils
|
from . import helpers, utils, events
|
||||||
from .errors import (
|
from .errors import (
|
||||||
RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError,
|
RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError,
|
||||||
PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError,
|
PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError,
|
||||||
|
@ -1760,26 +1761,17 @@ class TelegramClient(TelegramBareClient):
|
||||||
|
|
||||||
def on(self, event):
|
def on(self, event):
|
||||||
"""
|
"""
|
||||||
|
Decorator helper method around add_event_handler().
|
||||||
Turns the given entity into a valid Telegram user or chat.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
event (:obj:`_EventBuilder` | :obj:`type`):
|
event (:obj:`_EventBuilder` | :obj:`type`):
|
||||||
The event builder class or instance to be used,
|
The event builder class or instance to be used,
|
||||||
for instance ``events.NewMessage``.
|
for instance ``events.NewMessage``.
|
||||||
"""
|
"""
|
||||||
if isinstance(event, type):
|
|
||||||
event = event()
|
|
||||||
|
|
||||||
event.resolve(self)
|
|
||||||
|
|
||||||
def decorator(f):
|
def decorator(f):
|
||||||
self._event_builders.append((event, f))
|
self.add_event_handler(f, event)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
if self._on_handler not in self.updates.handlers:
|
|
||||||
self.add_update_handler(self._on_handler)
|
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
def _on_handler(self, update):
|
def _on_handler(self, update):
|
||||||
|
@ -1789,6 +1781,49 @@ class TelegramClient(TelegramBareClient):
|
||||||
event._client = self
|
event._client = self
|
||||||
callback(event)
|
callback(event)
|
||||||
|
|
||||||
|
def add_event_handler(self, callback, event):
|
||||||
|
"""
|
||||||
|
Registers the given callback to be called on the specified event.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (:obj:`callable`):
|
||||||
|
The callable function accepting one parameter to be used.
|
||||||
|
|
||||||
|
event (:obj:`_EventBuilder` | :obj:`type`):
|
||||||
|
The event builder class or instance to be used,
|
||||||
|
for instance ``events.NewMessage``.
|
||||||
|
"""
|
||||||
|
if self.updates.workers is None:
|
||||||
|
warnings.warn(
|
||||||
|
"You have not setup any workers, so you won't receive updates."
|
||||||
|
" Pass update_workers=1 when creating the TelegramClient,"
|
||||||
|
" or set client.self.updates.workers = 1"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.updates.handler = self._on_handler
|
||||||
|
if isinstance(event, type):
|
||||||
|
event = event()
|
||||||
|
|
||||||
|
event.resolve(self)
|
||||||
|
self._event_builders.append((event, callback))
|
||||||
|
|
||||||
|
def add_update_handler(self, handler):
|
||||||
|
"""Adds an update handler (a function which takes a TLObject,
|
||||||
|
an update, as its parameter) and listens for updates"""
|
||||||
|
warnings.warn(
|
||||||
|
'add_update_handler is deprecated, use the @client.on syntax '
|
||||||
|
'or add_event_handler(callback, events.Raw) instead (see '
|
||||||
|
'https://telethon.rtfd.io/en/latest/extra/basic/working-'
|
||||||
|
'with-updates.html)'
|
||||||
|
)
|
||||||
|
self.add_event_handler(handler, events.Raw)
|
||||||
|
|
||||||
|
def remove_update_handler(self, handler):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def list_update_handlers(self):
|
||||||
|
return []
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region Small utilities to make users' life easier
|
# region Small utilities to make users' life easier
|
||||||
|
|
|
@ -22,12 +22,12 @@ class UpdateState:
|
||||||
workers is None: Updates will *not* be stored on self.
|
workers is None: Updates will *not* be stored on self.
|
||||||
workers = 0: Another thread is responsible for calling self.poll()
|
workers = 0: Another thread is responsible for calling self.poll()
|
||||||
workers > 0: 'workers' background threads will be spawned, any
|
workers > 0: 'workers' background threads will be spawned, any
|
||||||
any of them will invoke all the self.handlers.
|
any of them will invoke the self.handler.
|
||||||
"""
|
"""
|
||||||
self._workers = workers
|
self._workers = workers
|
||||||
self._worker_threads = []
|
self._worker_threads = []
|
||||||
|
|
||||||
self.handlers = []
|
self.handler = None
|
||||||
self._updates_lock = RLock()
|
self._updates_lock = RLock()
|
||||||
self._updates = Queue()
|
self._updates = Queue()
|
||||||
|
|
||||||
|
@ -106,10 +106,8 @@ class UpdateState:
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
update = self.poll(timeout=UpdateState.WORKER_POLL_TIMEOUT)
|
update = self.poll(timeout=UpdateState.WORKER_POLL_TIMEOUT)
|
||||||
# TODO Maybe people can add different handlers per update type
|
if update and self.handler:
|
||||||
if update:
|
self.handler(update)
|
||||||
for handler in self.handlers:
|
|
||||||
handler(update)
|
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user