mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-16 12:25:45 +01:00
create missing handler types and minor fixes
This commit is contained in:
parent
884bb41d4a
commit
95fde0c6c4
7 changed files with 257 additions and 1 deletions
38
telegram/ext/callbackqueryhandler.py
Normal file
38
telegram/ext/callbackqueryhandler.py
Normal 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)
|
38
telegram/ext/choseninlineresulthandler.py
Normal file
38
telegram/ext/choseninlineresulthandler.py
Normal 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)
|
38
telegram/ext/inlinequeryhandler.py
Normal file
38
telegram/ext/inlinequeryhandler.py
Normal 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)
|
|
@ -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
|
||||
|
|
45
telegram/ext/stringcommandhandler.py
Normal file
45
telegram/ext/stringcommandhandler.py
Normal 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)
|
57
telegram/ext/stringregexhandler.py
Normal file
57
telegram/ext/stringregexhandler.py
Normal 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)
|
38
telegram/ext/typehandler.py
Normal file
38
telegram/ext/typehandler.py
Normal 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)
|
Loading…
Add table
Reference in a new issue