2015-11-05 13:52:33 +01:00
|
|
|
#!/usr/bin/env python
|
2015-12-21 21:18:53 +01:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
|
|
|
# Copyright (C) 2015 Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
This module contains the Dispatcher class.
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
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):
|
|
|
|
"""
|
2015-12-21 19:36:17 +01:00
|
|
|
Function decorator that will run the function in a new thread. A function
|
|
|
|
decorated with this will have to include **kwargs in their parameter list,
|
|
|
|
which will contain all optional parameters.
|
2015-11-11 13:33:03 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
func (function): The function to run in the thread.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
function:
|
|
|
|
"""
|
|
|
|
|
|
|
|
@wraps(func)
|
2015-12-21 19:36:17 +01:00
|
|
|
def pooled(*pargs, **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-12-21 19:36:17 +01:00
|
|
|
result = func(*pargs, **kwargs)
|
2015-11-11 13:33:03 +01:00
|
|
|
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)
|
2015-12-21 19:36:17 +01:00
|
|
|
def async_func(*pargs, **kwargs):
|
2015-11-15 17:36:38 +01:00
|
|
|
"""
|
|
|
|
A wrapper to run a function in a thread
|
|
|
|
"""
|
|
|
|
global running_async, async_lock
|
2015-12-21 19:36:17 +01:00
|
|
|
thread = Thread(target=pooled, args=pargs, kwargs=kwargs)
|
2015-11-11 13:33:03 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
class Dispatcher:
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
This class dispatches all kinds of updates to its registered handlers.
|
2015-12-16 16:18:48 +01:00
|
|
|
A handler is a function that usually takes the following parameters
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-12-16 16:18:48 +01:00
|
|
|
bot:
|
|
|
|
The telegram.Bot instance that received the message
|
|
|
|
update:
|
|
|
|
The update that should be handled by the handler
|
2015-11-22 13:57:59 +01:00
|
|
|
|
2015-12-16 16:18:48 +01:00
|
|
|
Error handlers take an additional parameter
|
|
|
|
|
|
|
|
error:
|
|
|
|
The TelegramError instance that was raised during processing the
|
2015-11-22 13:57:59 +01:00
|
|
|
update
|
|
|
|
|
|
|
|
All handlers, except error handlers, can also request more information by
|
|
|
|
appending one or more of the following arguments in their argument list for
|
2015-12-16 16:18:48 +01:00
|
|
|
convenience
|
|
|
|
|
|
|
|
update_queue:
|
|
|
|
The Queue instance which contains all new updates and is
|
2015-11-22 14:47:38 +01:00
|
|
|
processed by the Dispatcher. Be careful with this - you might
|
2015-11-22 13:57:59 +01:00
|
|
|
create an infinite loop.
|
2015-12-16 16:18:48 +01:00
|
|
|
args:
|
|
|
|
If the update is an instance str or telegram.Update, this will be
|
2015-11-22 13:57:59 +01:00
|
|
|
a list that contains the content of the message split on spaces,
|
|
|
|
except the first word (usually the command).
|
|
|
|
Example: '/add item1 item2 item3' -> ['item1', 'item2', 'item3']
|
|
|
|
For other updates, args will be None
|
|
|
|
|
2015-12-21 20:25:31 +01:00
|
|
|
For regex-based handlers, you can also request information about the match.
|
|
|
|
For all other handlers, these will be None
|
|
|
|
|
|
|
|
groups:
|
|
|
|
A tuple that contains the result of
|
|
|
|
re.match(matcher, ...).groups()
|
|
|
|
groupdict:
|
|
|
|
A dictionary that contains the result of
|
|
|
|
re.match(matcher, ...).groupdict()
|
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2015-11-16 20:43:35 +01:00
|
|
|
bot (telegram.Bot): The bot object that should be passed to the
|
2015-12-21 21:40:41 +01:00
|
|
|
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-12-31 14:52:28 +01:00
|
|
|
self.__lock = Lock()
|
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):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Thread target of thread 'dispatcher'. Runs in background and processes
|
2015-11-15 17:36:38 +01:00
|
|
|
the update queue.
|
|
|
|
"""
|
|
|
|
|
2015-12-31 14:52:28 +01:00
|
|
|
self.__lock.acquire()
|
|
|
|
if not self.running:
|
|
|
|
self.running = True
|
|
|
|
self.__lock.release()
|
|
|
|
self.logger.info('Dispatcher thread started')
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-12-31 14:52:28 +01:00
|
|
|
while True:
|
|
|
|
update = None
|
2015-11-15 19:12:42 +01:00
|
|
|
|
2015-12-31 14:52:28 +01:00
|
|
|
try:
|
|
|
|
# Pop update from update queue.
|
|
|
|
# Blocks if no updates are available.
|
|
|
|
update = self.update_queue.get()
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-12-31 14:52:28 +01:00
|
|
|
if type(update) is self._Stop:
|
|
|
|
break
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-12-31 14:52:28 +01:00
|
|
|
self.processUpdate(update)
|
|
|
|
self.logger.debug('Processed Update: %s' % update)
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-12-31 14:52:28 +01:00
|
|
|
# Dispatch any errors
|
|
|
|
except TelegramError as te:
|
2015-12-31 15:03:40 +01:00
|
|
|
self.logger.warn("Error was raised while processing "
|
|
|
|
"Update.")
|
2015-12-31 14:52:28 +01:00
|
|
|
self.dispatchError(update, te)
|
2015-12-29 14:27:46 +01:00
|
|
|
|
2015-12-31 15:03:40 +01:00
|
|
|
# All other errors should not stop the thread, just print them
|
2015-12-31 14:52:28 +01:00
|
|
|
except:
|
2016-01-04 01:32:00 +01:00
|
|
|
self.logger.exception("An uncaught error was raised while "
|
|
|
|
"processing an update")
|
2015-12-31 14:52:28 +01:00
|
|
|
else:
|
|
|
|
self.__lock.release()
|
2015-11-22 14:47:38 +01:00
|
|
|
self.logger.info('Dispatcher thread stopped')
|
2015-11-15 17:36:38 +01:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stops the thread
|
|
|
|
"""
|
2015-12-31 14:52:28 +01:00
|
|
|
with self.__lock:
|
|
|
|
if self.running:
|
|
|
|
self.running = False
|
|
|
|
self.update_queue.put(self._Stop())
|
2015-11-15 17:36:38 +01:00
|
|
|
|
|
|
|
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):
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchType(update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
|
|
|
|
# string update
|
|
|
|
if type(update) is str and update.startswith('/'):
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchStringCommand(update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
elif type(update) is str:
|
2015-12-21 20:25:31 +01:00
|
|
|
self.dispatchRegex(update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
|
|
|
|
# An error happened while polling
|
|
|
|
if isinstance(update, TelegramError):
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchError(None, update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Telegram update (regex)
|
|
|
|
if isinstance(update, Update):
|
2015-12-21 20:25:31 +01:00
|
|
|
self.dispatchRegex(update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Telegram update (command)
|
|
|
|
if isinstance(update, Update) \
|
|
|
|
and update.message.text.startswith('/'):
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTelegramCommand(update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Telegram update (message)
|
|
|
|
elif isinstance(update, Update):
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTelegramMessage(update)
|
2015-11-15 17:36:38 +01:00
|
|
|
handled = True
|
|
|
|
|
|
|
|
# Update not recognized
|
|
|
|
if not handled:
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchError(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):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Registers a message handler in the Dispatcher.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2015-11-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, Update, *args) as
|
2015-11-05 13:52:33 +01:00
|
|
|
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):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Registers a command handler in the Dispatcher.
|
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-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, Update, *args) as
|
2015-11-05 13:52:33 +01:00
|
|
|
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-22 14:47:38 +01:00
|
|
|
Registers a regex handler in the Dispatcher. If handlers will be
|
2015-11-16 20:43:35 +01:00
|
|
|
called if re.match(matcher, update.message.text) is True.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
2015-11-24 21:07:13 +01:00
|
|
|
matcher (str/__Regex): A regex string or compiled regex object that
|
2015-11-16 20:43:35 +01:00
|
|
|
matches on messages that handler should be listening to
|
2015-11-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, Update, *args) as
|
2015-11-05 13:52:33 +01:00
|
|
|
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):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Registers a string-command handler in the Dispatcher.
|
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-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, str, *args) as
|
|
|
|
arguments.
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
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-22 14:47:38 +01:00
|
|
|
Registers a regex handler in the Dispatcher. If handlers will be
|
2015-11-16 20:43:35 +01:00
|
|
|
called if re.match(matcher, string) is True.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
2015-11-24 21:07:13 +01:00
|
|
|
matcher (str/__Regex): A regex string or compiled regex object that
|
2015-11-16 20:43:35 +01:00
|
|
|
matches on the string input that handler should be listening to
|
2015-11-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, Update, *args) as
|
2015-11-05 13:52:33 +01:00
|
|
|
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):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Registers a command handler in the Dispatcher, that will receive all
|
2015-11-05 13:52:33 +01:00
|
|
|
commands that have no associated handler.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2015-11-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, Update, *args) as
|
2015-11-05 13:52:33 +01:00
|
|
|
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-22 14:47:38 +01:00
|
|
|
Registers a string-command handler in the Dispatcher, that will
|
2015-11-16 20:43:35 +01:00
|
|
|
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:
|
2015-11-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, str, *args) as
|
|
|
|
arguments.
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
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):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Registers an error handler in the Dispatcher.
|
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):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Registers a type handler in the Dispatcher. This allows you to send
|
2015-11-05 13:52:33 +01:00
|
|
|
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
|
2015-11-22 13:57:59 +01:00
|
|
|
handler (function): A function that takes (Bot, type, *args) as
|
|
|
|
arguments.
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
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:
|
2015-11-24 21:07:13 +01:00
|
|
|
matcher (str/__Regex): The regex matcher object or string
|
2015-11-05 13:52:33 +01:00
|
|
|
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:
|
2015-11-24 21:07:13 +01:00
|
|
|
matcher (str/__Regex): The regex matcher object or string
|
2015-11-05 13:52:33 +01:00
|
|
|
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)
|
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
def dispatchTelegramCommand(self, update):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Dispatches an update that contains a command.
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
command (str): The command keyword
|
|
|
|
update (telegram.Update): The Telegram update that contains the
|
|
|
|
command
|
|
|
|
"""
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
command = update.message.text.split(' ')[0][1:].split('@')[0]
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
if command in self.telegram_command_handlers:
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTo(self.telegram_command_handlers[command], update)
|
2015-11-05 13:52:33 +01:00
|
|
|
else:
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTo(self.unknown_telegram_command_handlers, update)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-12-21 20:25:31 +01:00
|
|
|
def dispatchRegex(self, update):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-12-21 20:25:31 +01:00
|
|
|
Dispatches an update to all string or telegram regex handlers that
|
|
|
|
match the string/message content.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
2015-12-21 20:25:31 +01:00
|
|
|
update (str, Update): The update that should be checked for matches
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
|
|
|
|
2015-12-21 20:25:31 +01:00
|
|
|
if isinstance(update, Update):
|
|
|
|
handlers = self.telegram_regex_handlers
|
|
|
|
to_match = update.message.text
|
|
|
|
elif isinstance(update, str):
|
|
|
|
handlers = self.string_regex_handlers
|
|
|
|
to_match = update
|
|
|
|
|
|
|
|
for matcher in handlers:
|
|
|
|
m = match(matcher, to_match)
|
|
|
|
if m:
|
|
|
|
for handler in handlers[matcher]:
|
|
|
|
self.call_handler(handler,
|
|
|
|
update,
|
|
|
|
groups=m.groups(),
|
|
|
|
groupdict=m.groupdict())
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
def dispatchStringCommand(self, update):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Dispatches a string-update that contains a command.
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
update (str): The string input
|
|
|
|
"""
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
command = update.split(' ')[0][1:]
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
if command in self.string_command_handlers:
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTo(self.string_command_handlers[command], update)
|
2015-11-05 13:52:33 +01:00
|
|
|
else:
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTo(self.unknown_string_command_handlers, update)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
def dispatchType(self, update):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Dispatches an update of any type.
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
update (any): The update
|
|
|
|
"""
|
|
|
|
|
|
|
|
for t in self.type_handlers:
|
|
|
|
if isinstance(update, t):
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTo(self.type_handlers[t], update)
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
def dispatchTelegramMessage(self, update):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Dispatches an update that contains a regular message.
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
|
|
|
update (telegram.Update): The Telegram update that contains the
|
|
|
|
message.
|
|
|
|
"""
|
2015-11-21 16:04:06 +01:00
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
self.dispatchTo(self.telegram_message_handlers, update)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-11-22 14:47:38 +01:00
|
|
|
def dispatchError(self, update, error):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Dispatches an error.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
def dispatchTo(self, handlers, update, **kwargs):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Dispatches an update to a list of handlers.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
handlers (list): A list of handler-functions.
|
2015-11-22 14:47:38 +01:00
|
|
|
update (any): The update to be dispatched
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
for handler in handlers:
|
2015-12-21 19:36:17 +01:00
|
|
|
self.call_handler(handler, update, **kwargs)
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
def call_handler(self, handler, update, **kwargs):
|
2015-11-22 13:57:59 +01:00
|
|
|
"""
|
|
|
|
Calls an update handler. Checks the handler for keyword arguments and
|
|
|
|
fills them, if possible.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
handler (function): An update handler function
|
|
|
|
update (any): An update
|
|
|
|
"""
|
2015-12-21 19:36:17 +01:00
|
|
|
|
|
|
|
target_kwargs = {}
|
2015-11-22 13:57:59 +01:00
|
|
|
fargs = getargspec(handler).args
|
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
'''
|
|
|
|
async handlers will receive all optional arguments, since we can't
|
|
|
|
their argument list.
|
|
|
|
'''
|
|
|
|
|
|
|
|
is_async = 'pargs' == getargspec(handler).varargs
|
2015-11-22 14:07:20 +01:00
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
if is_async or 'update_queue' in fargs:
|
|
|
|
target_kwargs['update_queue'] = self.update_queue
|
|
|
|
|
|
|
|
if is_async or 'args' in fargs:
|
2015-11-22 13:57:59 +01:00
|
|
|
if isinstance(update, Update):
|
|
|
|
args = update.message.text.split(' ')[1:]
|
|
|
|
elif isinstance(update, str):
|
|
|
|
args = update.split(' ')[1:]
|
|
|
|
else:
|
|
|
|
args = None
|
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
target_kwargs['args'] = args
|
|
|
|
|
|
|
|
if is_async or 'groups' in fargs:
|
|
|
|
target_kwargs['groups'] = kwargs.get('groups', None)
|
|
|
|
|
|
|
|
if is_async or 'groupdict' in fargs:
|
|
|
|
target_kwargs['groupdict'] = kwargs.get('groupdict', None)
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
handler(self.bot, update, **target_kwargs)
|