2015-11-05 13:52:33 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
This module contains the Broadcaster class.
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
import logging
|
2015-11-11 13:33:03 +01:00
|
|
|
from functools import wraps
|
2015-11-15 17:36:38 +01:00
|
|
|
from inspect import getargspec
|
|
|
|
from threading import Thread, BoundedSemaphore, Lock
|
|
|
|
from re import match
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-11-15 17:36:38 +01:00
|
|
|
from telegram import (TelegramError, Update, NullHandler)
|
|
|
|
|
|
|
|
H = NullHandler()
|
|
|
|
logging.getLogger(__name__).addHandler(H)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-11-11 13:33:03 +01:00
|
|
|
semaphore = None
|
2015-11-15 17:36:38 +01:00
|
|
|
running_async = 0
|
|
|
|
async_lock = Lock()
|
2015-11-06 00:24:01 +01:00
|
|
|
|
2015-11-11 13:33:03 +01:00
|
|
|
|
|
|
|
def run_async(func):
|
|
|
|
"""
|
|
|
|
Function decorator that will run the function in a new thread.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
func (function): The function to run in the thread.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
function:
|
|
|
|
"""
|
|
|
|
|
|
|
|
@wraps(func)
|
|
|
|
def pooled(*args, **kwargs):
|
2015-11-15 17:36:38 +01:00
|
|
|
"""
|
|
|
|
A wrapper to run a thread in a thread pool
|
|
|
|
"""
|
|
|
|
global running_async, async_lock
|
2015-11-11 13:33:03 +01:00
|
|
|
result = func(*args, **kwargs)
|
|
|
|
semaphore.release()
|
2015-11-15 17:36:38 +01:00
|
|
|
with async_lock:
|
|
|
|
running_async -= 1
|
2015-11-11 13:33:03 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
@wraps(func)
|
|
|
|
def async_func(*args, **kwargs):
|
2015-11-15 17:36:38 +01:00
|
|
|
"""
|
|
|
|
A wrapper to run a function in a thread
|
|
|
|
"""
|
|
|
|
global running_async, async_lock
|
2015-11-11 13:33:03 +01:00
|
|
|
thread = Thread(target=pooled, args=args, kwargs=kwargs)
|
|
|
|
semaphore.acquire()
|
2015-11-15 17:36:38 +01:00
|
|
|
with async_lock:
|
|
|
|
running_async += 1
|
2015-11-11 13:33:03 +01:00
|
|
|
thread.start()
|
|
|
|
return thread
|
|
|
|
|
|
|
|
return async_func
|
|
|
|
|
|
|
|
|
|
|
|
class Broadcaster:
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
|
|
|
This class broadcasts all kinds of updates to its registered handlers.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
|
|
|
|
Args:
|
2015-11-16 20:43:35 +01:00
|
|
|
bot (telegram.Bot): The bot object that should be passed to the
|
|
|
|
handlers update_queue (queue.Queue): The synchronized queue that will
|
|
|
|
contain the updates.
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-11 13:33:03 +01:00
|
|
|
def __init__(self, bot, update_queue, workers=4):
|
2015-11-05 13:52:33 +01:00
|
|
|
self.bot = bot
|
|
|
|
self.update_queue = update_queue
|
|
|
|
self.telegram_message_handlers = []
|
|
|
|
self.telegram_command_handlers = {}
|
|
|
|
self.telegram_regex_handlers = {}
|
|
|
|
self.string_regex_handlers = {}
|
|
|
|
self.string_command_handlers = {}
|
|
|
|
self.type_handlers = {}
|
|
|
|
self.unknown_telegram_command_handlers = []
|
|
|
|
self.unknown_string_command_handlers = []
|
|
|
|
self.error_handlers = []
|
2015-11-15 17:36:38 +01:00
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
self.running = False
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-11-11 13:33:03 +01:00
|
|
|
global semaphore
|
|
|
|
if not semaphore:
|
|
|
|
semaphore = BoundedSemaphore(value=workers)
|
|
|
|
else:
|
2015-11-15 17:36:38 +01:00
|
|
|
self.logger.info("Semaphore already initialized, skipping.")
|
|
|
|
|
2015-11-16 20:35:27 +01:00
|
|
|
class _Stop(object):
|
2015-11-15 17:36:38 +01:00
|
|
|
"""
|
|
|
|
A class which objects can be passed into the update queue to stop the
|
|
|
|
thread
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""
|
|
|
|
Thread target of thread 'broadcaster'. Runs in background and processes
|
|
|
|
the update queue.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.running = True
|
|
|
|
self.logger.info('Broadcaster thread started')
|
|
|
|
|
|
|
|
while True:
|
2015-11-15 19:12:42 +01:00
|
|
|
update = None
|
|
|
|
|
2015-11-15 17:36:38 +01:00
|
|
|
try:
|
|
|
|
# Pop update from update queue.
|
|
|
|
# Blocks if no updates are available.
|
|
|
|
update = self.update_queue.get()
|
|
|
|
|
|
|
|
if type(update) is self._Stop:
|
|
|
|
break
|
|
|
|
|
|
|
|
self.processUpdate(update)
|
|
|
|
|
|
|
|
# Broadcast any errors
|
|
|
|
except TelegramError as te:
|
2015-11-15 19:12:42 +01:00
|
|
|
self.broadcastError(update, te)
|
2015-11-15 17:36:38 +01:00
|
|
|
|
|
|
|
self.logger.info('Broadcaster thread stopped')
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stops the thread
|
|
|
|
"""
|
|
|
|
if self.running:
|
|
|
|
self.running = False
|
|
|
|
self.update_queue.put(self._Stop())
|
|
|
|
|
|
|
|
def processUpdate(self, update):
|
|
|
|
"""
|
|
|
|
Processes a single update.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
handled = False
|
|
|
|
|
|
|
|
# Custom type handlers
|
|
|
|
for t in self.type_handlers:
|
|
|
|
if isinstance(update, t):
|
|
|
|
self.broadcastType(update)
|
|
|
|
handled = True
|
|
|
|
|
|
|
|
# string update
|
|
|
|
if type(update) is str and update.startswith('/'):
|
|
|
|
self.broadcastStringCommand(update)
|
|
|
|
handled = True
|
|
|
|
elif type(update) is str:
|
|
|
|
self.broadcastStringRegex(update)
|
|
|
|
handled = True
|
|
|
|
|
|
|
|
# An error happened while polling
|
|
|
|
if isinstance(update, TelegramError):
|
2015-11-15 19:12:42 +01:00
|
|
|
self.broadcastError(None, update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Telegram update (regex)
|
|
|
|
if isinstance(update, Update):
|
|
|
|
self.broadcastTelegramRegex(update)
|
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Telegram update (command)
|
|
|
|
if isinstance(update, Update) \
|
|
|
|
and update.message.text.startswith('/'):
|
|
|
|
self.broadcastTelegramCommand(update)
|
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Telegram update (message)
|
|
|
|
elif isinstance(update, Update):
|
|
|
|
self.broadcastTelegramMessage(update)
|
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Update not recognized
|
|
|
|
if not handled:
|
2015-11-15 19:12:42 +01:00
|
|
|
self.broadcastError(update, TelegramError(
|
2015-11-15 17:36:38 +01:00
|
|
|
"Received update of unknown type %s" % type(update)))
|
2015-11-11 13:33:03 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
# Add Handlers
|
|
|
|
def addTelegramMessageHandler(self, handler):
|
|
|
|
"""
|
|
|
|
Registers a message handler in the Broadcaster.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
handler (function): A function that takes (Bot, Update) as
|
|
|
|
arguments.
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
self.telegram_message_handlers.append(handler)
|
|
|
|
|
|
|
|
def addTelegramCommandHandler(self, command, handler):
|
|
|
|
"""
|
|
|
|
Registers a command handler in the Broadcaster.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2015-11-15 17:36:38 +01:00
|
|
|
command (str): The command keyword that this handler should be
|
|
|
|
listening to.
|
2015-11-05 13:52:33 +01:00
|
|
|
handler (function): A function that takes (Bot, Update) as
|
|
|
|
arguments.
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
if command not in self.telegram_command_handlers:
|
|
|
|
self.telegram_command_handlers[command] = []
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
self.telegram_command_handlers[command].append(handler)
|
|
|
|
|
|
|
|
def addTelegramRegexHandler(self, matcher, handler):
|
|
|
|
"""
|
2015-11-16 20:43:35 +01:00
|
|
|
Registers a regex handler in the Broadcaster. If handlers will be
|
|
|
|
called if re.match(matcher, update.message.text) is True.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
2015-11-16 20:43:35 +01:00
|
|
|
matcher (__Regex): A regex string or compiled regex object that
|
|
|
|
matches on messages that handler should be listening to
|
2015-11-05 13:52:33 +01:00
|
|
|
handler (function): A function that takes (Bot, Update) as
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if matcher not in self.telegram_regex_handlers:
|
|
|
|
self.telegram_regex_handlers[matcher] = []
|
|
|
|
|
|
|
|
self.telegram_regex_handlers[matcher].append(handler)
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
def addStringCommandHandler(self, command, handler):
|
|
|
|
"""
|
|
|
|
Registers a string-command handler in the Broadcaster.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2015-11-15 17:36:38 +01:00
|
|
|
command (str): The command keyword that this handler should be
|
|
|
|
listening to.
|
2015-11-05 13:52:33 +01:00
|
|
|
handler (function): A function that takes (Bot, str) as arguments.
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
if command not in self.string_command_handlers:
|
|
|
|
self.string_command_handlers[command] = []
|
|
|
|
|
|
|
|
self.string_command_handlers[command].append(handler)
|
|
|
|
|
|
|
|
def addStringRegexHandler(self, matcher, handler):
|
|
|
|
"""
|
2015-11-16 20:43:35 +01:00
|
|
|
Registers a regex handler in the Broadcaster. If handlers will be
|
|
|
|
called if re.match(matcher, string) is True.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
2015-11-16 20:43:35 +01:00
|
|
|
matcher (__Regex): A regex string or compiled regex object that
|
|
|
|
matches on the string input that handler should be listening to
|
2015-11-05 13:52:33 +01:00
|
|
|
handler (function): A function that takes (Bot, Update) as
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if matcher not in self.string_regex_handlers:
|
|
|
|
self.string_regex_handlers[matcher] = []
|
|
|
|
|
|
|
|
self.string_regex_handlers[matcher].append(handler)
|
|
|
|
|
|
|
|
def addUnknownTelegramCommandHandler(self, handler):
|
|
|
|
"""
|
|
|
|
Registers a command handler in the Broadcaster, that will receive all
|
|
|
|
commands that have no associated handler.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
handler (function): A function that takes (Bot, Update) as
|
|
|
|
arguments.
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
self.unknown_telegram_command_handlers.append(handler)
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
def addUnknownStringCommandHandler(self, handler):
|
|
|
|
"""
|
2015-11-16 20:43:35 +01:00
|
|
|
Registers a string-command handler in the Broadcaster, that will
|
|
|
|
receive all commands that have no associated handler.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
handler (function): A function that takes (Bot, str) as arguments.
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
self.unknown_string_command_handlers.append(handler)
|
|
|
|
|
|
|
|
def addErrorHandler(self, handler):
|
|
|
|
"""
|
|
|
|
Registers an error handler in the Broadcaster.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
handler (function): A function that takes (Bot, TelegramError) as
|
|
|
|
arguments.
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
self.error_handlers.append(handler)
|
|
|
|
|
|
|
|
def addTypeHandler(self, the_type, handler):
|
|
|
|
"""
|
|
|
|
Registers a type handler in the Broadcaster. This allows you to send
|
|
|
|
any type of object into the update queue.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
the_type (type): The type this handler should listen to
|
|
|
|
handler (function): A function that takes (Bot, type) as arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if the_type not in self.type_handlers:
|
|
|
|
self.type_handlers[the_type] = []
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
self.type_handlers[the_type].append(handler)
|
|
|
|
|
|
|
|
# Remove Handlers
|
|
|
|
def removeTelegramMessageHandler(self, handler):
|
|
|
|
"""
|
|
|
|
De-registers a message handler.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if handler in self.telegram_message_handlers:
|
|
|
|
self.telegram_message_handlers.remove(handler)
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
def removeTelegramCommandHandler(self, command, handler):
|
|
|
|
"""
|
|
|
|
De-registers a command handler.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
command (str): The command
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if command in self.telegram_command_handlers \
|
|
|
|
and handler in self.telegram_command_handlers[command]:
|
|
|
|
self.telegram_command_handlers[command].remove(handler)
|
|
|
|
|
|
|
|
def removeTelegramRegexHandler(self, matcher, handler):
|
|
|
|
"""
|
|
|
|
De-registers a regex handler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
matcher (str): The regex matcher object
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if matcher in self.telegram_regex_handlers \
|
|
|
|
and handler in self.telegram_regex_handlers[matcher]:
|
|
|
|
self.telegram_regex_handlers[matcher].remove(handler)
|
|
|
|
|
|
|
|
def removeStringCommandHandler(self, command, handler):
|
|
|
|
"""
|
|
|
|
De-registers a string-command handler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
command (str): The command
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if command in self.string_command_handlers \
|
|
|
|
and handler in self.string_command_handlers[command]:
|
|
|
|
self.string_command_handlers[command].remove(handler)
|
|
|
|
|
|
|
|
def removeStringRegexHandler(self, matcher, handler):
|
|
|
|
"""
|
|
|
|
De-registers a regex handler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
matcher (str): The regex matcher object
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if matcher in self.string_regex_handlers \
|
|
|
|
and handler in self.string_regex_handlers[matcher]:
|
|
|
|
self.string_regex_handlers[matcher].remove(handler)
|
|
|
|
|
|
|
|
def removeUnknownTelegramCommandHandler(self, handler):
|
|
|
|
"""
|
|
|
|
De-registers an unknown-command handler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if handler in self.unknown_telegram_command_handlers:
|
|
|
|
self.unknown_telegram_command_handlers.remove(handler)
|
|
|
|
|
|
|
|
def removeUnknownStringCommandHandler(self, handler):
|
|
|
|
"""
|
|
|
|
De-registers an unknown-command handler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if handler in self.unknown_string_command_handlers:
|
|
|
|
self.unknown_string_command_handlers.remove(handler)
|
|
|
|
|
|
|
|
def removeErrorHandler(self, handler):
|
|
|
|
"""
|
|
|
|
De-registers an error handler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if handler in self.error_handlers:
|
|
|
|
self.error_handlers.remove(handler)
|
|
|
|
|
|
|
|
def removeTypeHandler(self, the_type, handler):
|
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
De-registers a type handler.
|
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
handler (any):
|
|
|
|
"""
|
|
|
|
|
|
|
|
if the_type in self.type_handlers \
|
|
|
|
and handler in self.type_handlers[the_type]:
|
|
|
|
self.type_handlers[the_type].remove(handler)
|
|
|
|
|
|
|
|
def broadcastTelegramCommand(self, update):
|
|
|
|
"""
|
|
|
|
Broadcasts an update that contains a command.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
command (str): The command keyword
|
|
|
|
update (telegram.Update): The Telegram update that contains the
|
|
|
|
command
|
|
|
|
"""
|
|
|
|
|
|
|
|
command = update.message.text.split(' ')[0][1:].split('@')[0]
|
|
|
|
|
|
|
|
if command in self.telegram_command_handlers:
|
|
|
|
self.broadcastTo(self.telegram_command_handlers[command], update)
|
|
|
|
else:
|
|
|
|
self.broadcastTo(self.unknown_telegram_command_handlers, update)
|
|
|
|
|
|
|
|
def broadcastTelegramRegex(self, update):
|
|
|
|
"""
|
2015-11-16 20:43:35 +01:00
|
|
|
Broadcasts an update to all regex handlers that match the message
|
|
|
|
string.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
command (str): The command keyword
|
|
|
|
update (telegram.Update): The Telegram update that contains the
|
|
|
|
command
|
|
|
|
"""
|
|
|
|
|
|
|
|
matching_handlers = []
|
|
|
|
|
|
|
|
for matcher in self.telegram_regex_handlers:
|
2015-11-15 17:36:38 +01:00
|
|
|
if match(matcher, update.message.text):
|
2015-11-05 13:52:33 +01:00
|
|
|
for handler in self.telegram_regex_handlers[matcher]:
|
|
|
|
matching_handlers.append(handler)
|
|
|
|
|
|
|
|
self.broadcastTo(matching_handlers, update)
|
|
|
|
|
|
|
|
def broadcastStringCommand(self, update):
|
|
|
|
"""
|
|
|
|
Broadcasts a string-update that contains a command.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (str): The string input
|
|
|
|
"""
|
|
|
|
|
|
|
|
command = update.split(' ')[0][1:]
|
|
|
|
|
|
|
|
if command in self.string_command_handlers:
|
|
|
|
self.broadcastTo(self.string_command_handlers[command], update)
|
|
|
|
else:
|
|
|
|
self.broadcastTo(self.unknown_string_command_handlers, update)
|
|
|
|
|
|
|
|
def broadcastStringRegex(self, update):
|
|
|
|
"""
|
2015-11-16 20:43:35 +01:00
|
|
|
Broadcasts an update to all string regex handlers that match the
|
|
|
|
string.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
command (str): The command keyword
|
|
|
|
update (telegram.Update): The Telegram update that contains the
|
|
|
|
command
|
|
|
|
"""
|
|
|
|
|
|
|
|
matching_handlers = []
|
|
|
|
|
|
|
|
for matcher in self.string_regex_handlers:
|
2015-11-15 17:36:38 +01:00
|
|
|
if match(matcher, update):
|
2015-11-05 13:52:33 +01:00
|
|
|
for handler in self.string_regex_handlers[matcher]:
|
|
|
|
matching_handlers.append(handler)
|
|
|
|
|
|
|
|
self.broadcastTo(matching_handlers, update)
|
|
|
|
|
|
|
|
def broadcastType(self, update):
|
|
|
|
"""
|
|
|
|
Broadcasts an update of any type.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (any): The update
|
|
|
|
"""
|
|
|
|
|
|
|
|
for t in self.type_handlers:
|
|
|
|
if isinstance(update, t):
|
|
|
|
self.broadcastTo(self.type_handlers[t], update)
|
|
|
|
else:
|
2015-11-15 19:12:42 +01:00
|
|
|
self.broadcastError(update, TelegramError(
|
2015-11-05 13:52:33 +01:00
|
|
|
"Received update of unknown type %s" % type(update)))
|
|
|
|
|
|
|
|
def broadcastTelegramMessage(self, update):
|
|
|
|
"""
|
|
|
|
Broadcasts an update that contains a regular message.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (telegram.Update): The Telegram update that contains the
|
|
|
|
message.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.broadcastTo(self.telegram_message_handlers, update)
|
|
|
|
|
2015-11-15 19:12:42 +01:00
|
|
|
def broadcastError(self, update, error):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
|
|
|
Broadcasts an error.
|
|
|
|
|
|
|
|
Args:
|
2015-11-15 19:12:42 +01:00
|
|
|
update (any): The pdate that caused the error
|
2015-11-05 13:52:33 +01:00
|
|
|
error (telegram.TelegramError): The Telegram error that was raised.
|
|
|
|
"""
|
|
|
|
|
|
|
|
for handler in self.error_handlers:
|
2015-11-15 19:12:42 +01:00
|
|
|
handler(self.bot, update, error)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
def broadcastTo(self, handlers, update):
|
|
|
|
"""
|
|
|
|
Broadcasts an update to a list of handlers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
handlers (list): A list of handler-functions.
|
|
|
|
update (any): The update to be broadcasted
|
|
|
|
"""
|
|
|
|
|
|
|
|
for handler in handlers:
|
2015-11-15 17:36:38 +01:00
|
|
|
self.call_handler(handler, update)
|
|
|
|
|
|
|
|
def call_handler(self, handler, update):
|
|
|
|
kwargs = {}
|
|
|
|
if 'update_queue' in getargspec(handler).args:
|
|
|
|
kwargs['update_queue'] = self.update_queue
|
|
|
|
|
|
|
|
handler(self.bot, update, **kwargs)
|