create missing handler types and minor fixes

This commit is contained in:
Jannes Höke 2016-04-16 15:21:19 +02:00
parent 884bb41d4a
commit 95fde0c6c4
7 changed files with 257 additions and 1 deletions

View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# 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/].
""" This module contains the base class for handlers as used by the
Dispatcher """
from .handler import Handler
from telegram import Update
class CallbackQueryHandler(Handler):
def __init__(self, callback, pass_update_queue=False):
super(Handler).__init__(callback, pass_update_queue)
def checkUpdate(self, update):
return isinstance(update, Update) and update.inline_query
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)

View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# 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/].
""" This module contains the base class for handlers as used by the
Dispatcher """
from .handler import Handler
from telegram import Update
class ChosenInlineResultHandler(Handler):
def __init__(self, callback, pass_update_queue=False):
super(Handler).__init__(callback, pass_update_queue)
def checkUpdate(self, update):
return isinstance(update, Update) and update.chosen_inline_result
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)

View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# 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/].
""" This module contains the base class for handlers as used by the
Dispatcher """
from .handler import Handler
from telegram import Update
class InlineQueryHandler(Handler):
def __init__(self, callback, pass_update_queue=False):
super(Handler).__init__(callback, pass_update_queue)
def checkUpdate(self, update):
return isinstance(update, Update) and update.inline_query
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)

View file

@ -25,6 +25,7 @@ from telegram import Update
from .filters import *
class MessageHandler(Handler):
def __init__(self, filters, callback, pass_update_queue=False):
@ -35,7 +36,8 @@ class MessageHandler(Handler):
filters = self.filters
if isinstance(update, Update) and update.message:
message = update.message
return (TEXT in filters and message.text and
return (not filters or # If filters is empty, accept all messages
TEXT in filters and message.text and
not message.text.startswith('/') or
AUDIO in filters and message.audio or
DOCUMENT in filters and message.document or

View file

@ -0,0 +1,45 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# 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/].
""" This module contains the base class for handlers as used by the
Dispatcher """
from .handler import Handler
class StringCommandHandler(Handler):
def __init__(self, command, callback, pass_args=False,
pass_update_queue=False):
super(Handler).__init__(callback, pass_update_queue)
self.command = command
self.pass_args = pass_args
def checkUpdate(self, update):
return (isinstance(update, str) and
update.startswith('/') and
update[1:].split(' ')[0] == self.command)
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
if self.pass_args:
optional_args['args'] = update.split(' ')[1:]
self.callback(dispatcher.bot, update, **optional_args)

View file

@ -0,0 +1,57 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# 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/].
""" This module contains the base class for handlers as used by the
Dispatcher """
import re
from .handler import Handler
class RegexHandler(Handler):
def __init__(self, pattern, callback, pass_groups=False,
pass_groupdict=False, pass_update_queue=False):
super(Handler).__init__(callback, pass_update_queue)
if isinstance(pattern, str):
pattern = re.compile(pattern)
self.pattern = pattern
self.pass_groups = pass_groups
self.pass_groupdict = pass_groupdict
def checkUpdate(self, update):
if isinstance(update, str):
match = re.match(self.pattern, update)
return bool(match)
else:
return False
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
match = re.match(self.pattern, update)
if self.pass_groups:
optional_args['groups'] = match.groups()
if self.pass_groupdict:
optional_args['groupdict'] = match.groupdict()
self.callback(dispatcher.bot, update, **optional_args)

View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# 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/].
""" This module contains the base class for handlers as used by the
Dispatcher """
from .handler import Handler
class TypeHandler(Handler):
def __init__(self, type, callback, pass_update_queue=False):
super(Handler).__init__(callback, pass_update_queue)
self.type = type
def checkUpdate(self, update):
return isinstance(update, self.type)
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)