mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-25 16:22:19 +01:00
add documentation and minor stuff
This commit is contained in:
parent
95fde0c6c4
commit
0d0ad1334c
11 changed files with 183 additions and 4 deletions
|
@ -25,6 +25,17 @@ from telegram import Update
|
||||||
|
|
||||||
|
|
||||||
class CallbackQueryHandler(Handler):
|
class CallbackQueryHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle Telegram callback queries.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, callback, pass_update_queue=False):
|
def __init__(self, callback, pass_update_queue=False):
|
||||||
super(Handler).__init__(callback, pass_update_queue)
|
super(Handler).__init__(callback, pass_update_queue)
|
||||||
|
|
|
@ -25,6 +25,18 @@ from telegram import Update
|
||||||
|
|
||||||
|
|
||||||
class ChosenInlineResultHandler(Handler):
|
class ChosenInlineResultHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle Telegram updates that contain a chosen inline
|
||||||
|
result.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, callback, pass_update_queue=False):
|
def __init__(self, callback, pass_update_queue=False):
|
||||||
super(Handler).__init__(callback, pass_update_queue)
|
super(Handler).__init__(callback, pass_update_queue)
|
||||||
|
|
|
@ -17,14 +17,31 @@
|
||||||
# You should have received a copy of the GNU Lesser Public License
|
# You should have received a copy of the GNU Lesser Public License
|
||||||
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
||||||
|
|
||||||
""" This module contains the base class for handlers as used by the
|
""" This module contains the CommandHandler class """
|
||||||
Dispatcher """
|
|
||||||
|
|
||||||
from .handler import Handler
|
from .handler import Handler
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
|
|
||||||
|
|
||||||
class CommandHandler(Handler):
|
class CommandHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle Telegram commands. Commands are Telegram messages
|
||||||
|
that start with ``/``, optionally followed by an @ and the bot's
|
||||||
|
name and/or some additional text.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
command (str): The name of the command this handler should listen for.
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_args (optional[bool]): If the handler should be passed the
|
||||||
|
arguments passed to the command as a keyword argument called `
|
||||||
|
`args``. It will contain a list of strings, which is the text
|
||||||
|
following the command split on spaces. Default is ``False``
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, command, callback, pass_args=False,
|
def __init__(self, command, callback, pass_args=False,
|
||||||
pass_update_queue=False):
|
pass_update_queue=False):
|
||||||
|
|
|
@ -1,2 +1,23 @@
|
||||||
|
#!/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 filters used by the MessageHandler class """
|
||||||
|
|
||||||
TEXT, AUDIO, DOCUMENT, PHOTO, STICKER, VIDEO, VOICE, CONTACT, LOCATION, \
|
TEXT, AUDIO, DOCUMENT, PHOTO, STICKER, VIDEO, VOICE, CONTACT, LOCATION, \
|
||||||
VENUE, STATUS_UPDATE = range(11)
|
VENUE, STATUS_UPDATE = range(11)
|
||||||
|
|
|
@ -22,6 +22,18 @@ Dispatcher """
|
||||||
|
|
||||||
|
|
||||||
class Handler(object):
|
class Handler(object):
|
||||||
|
"""
|
||||||
|
The base class for all update handlers. You can create your own handlers
|
||||||
|
by inheriting from this class.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_update_queue (optional[bool]): If the callback should be passed
|
||||||
|
the update queue as a keyword argument called ``update_queue``. It
|
||||||
|
can be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, callback, pass_update_queue=False):
|
def __init__(self, callback, pass_update_queue=False):
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
|
|
|
@ -25,6 +25,17 @@ from telegram import Update
|
||||||
|
|
||||||
|
|
||||||
class InlineQueryHandler(Handler):
|
class InlineQueryHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle Telegram inline queries.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, callback, pass_update_queue=False):
|
def __init__(self, callback, pass_update_queue=False):
|
||||||
super(Handler).__init__(callback, pass_update_queue)
|
super(Handler).__init__(callback, pass_update_queue)
|
||||||
|
|
|
@ -27,6 +27,23 @@ from .filters import *
|
||||||
|
|
||||||
|
|
||||||
class MessageHandler(Handler):
|
class MessageHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle telegram messages. Messages are Telegram Updates
|
||||||
|
that do not contain a command. They might contain text, media or status
|
||||||
|
updates.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filters (list): A list of filters defined in ``telegram.ext.filters``.
|
||||||
|
All messages that match at least one of those filters will be
|
||||||
|
accepted. If ``bool(filters)`` evaluates to ``False``, messages are
|
||||||
|
not filtered.
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, filters, callback, pass_update_queue=False):
|
def __init__(self, filters, callback, pass_update_queue=False):
|
||||||
super(Handler).__init__(callback, pass_update_queue)
|
super(Handler).__init__(callback, pass_update_queue)
|
||||||
|
|
|
@ -27,6 +27,27 @@ from telegram import Update
|
||||||
|
|
||||||
|
|
||||||
class RegexHandler(Handler):
|
class RegexHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle Telegram updates based on a regex. It uses a
|
||||||
|
regular expression to check text messages. Read the documentation of the
|
||||||
|
``re`` module for more information. The ``re.match`` function is used to
|
||||||
|
determine if an update should be handled by this handler.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
pattern (str or Pattern): The regex pattern.
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_groups (optional[bool]): If the callback should be passed the
|
||||||
|
result of ``re.match(pattern, text).groups()`` as a keyword
|
||||||
|
argument called ``groups``. Default is ``False``
|
||||||
|
pass_groupdict (optional[bool]): If the callback should be passed the
|
||||||
|
result of ``re.match(pattern, text).groupdict()`` as a keyword
|
||||||
|
argument called ``groupdict``. Default is ``False``
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, pattern, callback, pass_groups=False,
|
def __init__(self, pattern, callback, pass_groups=False,
|
||||||
pass_groupdict=False, pass_update_queue=False):
|
pass_groupdict=False, pass_update_queue=False):
|
||||||
|
|
|
@ -24,6 +24,23 @@ from .handler import Handler
|
||||||
|
|
||||||
|
|
||||||
class StringCommandHandler(Handler):
|
class StringCommandHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle string commands. Commands are string updates
|
||||||
|
that start with ``/``.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
command (str): The name of the command this handler should listen for.
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_args (optional[bool]): If the handler should be passed the
|
||||||
|
arguments passed to the command as a keyword argument called `
|
||||||
|
`args``. It will contain a list of strings, which is the text
|
||||||
|
following the command split on spaces. Default is ``False``
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, command, callback, pass_args=False,
|
def __init__(self, command, callback, pass_args=False,
|
||||||
pass_update_queue=False):
|
pass_update_queue=False):
|
||||||
|
|
|
@ -26,6 +26,27 @@ from .handler import Handler
|
||||||
|
|
||||||
|
|
||||||
class RegexHandler(Handler):
|
class RegexHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle string updates based on a regex. It uses a
|
||||||
|
regular expression to check update content. Read the documentation of the
|
||||||
|
``re`` module for more information. The ``re.match`` function is used to
|
||||||
|
determine if an update should be handled by this handler.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
pattern (str or Pattern): The regex pattern.
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
pass_groups (optional[bool]): If the callback should be passed the
|
||||||
|
result of ``re.match(pattern, update).groups()`` as a keyword
|
||||||
|
argument called ``groups``. Default is ``False``
|
||||||
|
pass_groupdict (optional[bool]): If the callback should be passed the
|
||||||
|
result of ``re.match(pattern, update).groupdict()`` as a keyword
|
||||||
|
argument called ``groupdict``. Default is ``False``
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, pattern, callback, pass_groups=False,
|
def __init__(self, pattern, callback, pass_groups=False,
|
||||||
pass_groupdict=False, pass_update_queue=False):
|
pass_groupdict=False, pass_update_queue=False):
|
||||||
|
|
|
@ -24,13 +24,32 @@ from .handler import Handler
|
||||||
|
|
||||||
|
|
||||||
class TypeHandler(Handler):
|
class TypeHandler(Handler):
|
||||||
|
"""
|
||||||
|
Handler class to handle updates of custom types.
|
||||||
|
|
||||||
def __init__(self, type, callback, pass_update_queue=False):
|
Args:
|
||||||
|
type (type): The ``type`` of updates this handler should process, as
|
||||||
|
determined by ``isinstance``
|
||||||
|
callback (function): A function that takes ``bot, update`` as
|
||||||
|
positional arguments. It will be called when the ``checkUpdate``
|
||||||
|
has determined that an update should be processed by this handler.
|
||||||
|
strict (optional[bool]): Use ``type`` instead of ``isinstance``.
|
||||||
|
Default is ``False``
|
||||||
|
pass_update_queue (optional[bool]): If the handler should be passed the
|
||||||
|
update queue as a keyword argument called ``update_queue``. It can
|
||||||
|
be used to insert updates. Default is ``False``
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, type, callback, strict=False, pass_update_queue=False):
|
||||||
super(Handler).__init__(callback, pass_update_queue)
|
super(Handler).__init__(callback, pass_update_queue)
|
||||||
self.type = type
|
self.type = type
|
||||||
|
self.strict = strict
|
||||||
|
|
||||||
def checkUpdate(self, update):
|
def checkUpdate(self, update):
|
||||||
return isinstance(update, self.type)
|
if not self.strict:
|
||||||
|
return isinstance(update, self.type)
|
||||||
|
else:
|
||||||
|
return type(update) is self.type
|
||||||
|
|
||||||
def handleUpdate(self, update, dispatcher):
|
def handleUpdate(self, update, dispatcher):
|
||||||
optional_args = self.collectOptionalArgs(dispatcher)
|
optional_args = self.collectOptionalArgs(dispatcher)
|
||||||
|
|
Loading…
Add table
Reference in a new issue