X Tutup
Skip to content

Commit 95fde0c

Browse files
committed
create missing handler types and minor fixes
1 parent 884bb41 commit 95fde0c

File tree

7 files changed

+257
-1
lines changed

7 files changed

+257
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2016
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
20+
""" This module contains the base class for handlers as used by the
21+
Dispatcher """
22+
23+
from .handler import Handler
24+
from telegram import Update
25+
26+
27+
class CallbackQueryHandler(Handler):
28+
29+
def __init__(self, callback, pass_update_queue=False):
30+
super(Handler).__init__(callback, pass_update_queue)
31+
32+
def checkUpdate(self, update):
33+
return isinstance(update, Update) and update.inline_query
34+
35+
def handleUpdate(self, update, dispatcher):
36+
optional_args = self.collectOptionalArgs(dispatcher)
37+
38+
self.callback(dispatcher.bot, update, **optional_args)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2016
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
20+
""" This module contains the base class for handlers as used by the
21+
Dispatcher """
22+
23+
from .handler import Handler
24+
from telegram import Update
25+
26+
27+
class ChosenInlineResultHandler(Handler):
28+
29+
def __init__(self, callback, pass_update_queue=False):
30+
super(Handler).__init__(callback, pass_update_queue)
31+
32+
def checkUpdate(self, update):
33+
return isinstance(update, Update) and update.chosen_inline_result
34+
35+
def handleUpdate(self, update, dispatcher):
36+
optional_args = self.collectOptionalArgs(dispatcher)
37+
38+
self.callback(dispatcher.bot, update, **optional_args)

telegram/ext/inlinequeryhandler.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2016
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
20+
""" This module contains the base class for handlers as used by the
21+
Dispatcher """
22+
23+
from .handler import Handler
24+
from telegram import Update
25+
26+
27+
class InlineQueryHandler(Handler):
28+
29+
def __init__(self, callback, pass_update_queue=False):
30+
super(Handler).__init__(callback, pass_update_queue)
31+
32+
def checkUpdate(self, update):
33+
return isinstance(update, Update) and update.inline_query
34+
35+
def handleUpdate(self, update, dispatcher):
36+
optional_args = self.collectOptionalArgs(dispatcher)
37+
38+
self.callback(dispatcher.bot, update, **optional_args)

telegram/ext/messagehandler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from .filters import *
2727

28+
2829
class MessageHandler(Handler):
2930

3031
def __init__(self, filters, callback, pass_update_queue=False):
@@ -35,7 +36,8 @@ def checkUpdate(self, update):
3536
filters = self.filters
3637
if isinstance(update, Update) and update.message:
3738
message = update.message
38-
return (TEXT in filters and message.text and
39+
return (not filters or # If filters is empty, accept all messages
40+
TEXT in filters and message.text and
3941
not message.text.startswith('/') or
4042
AUDIO in filters and message.audio or
4143
DOCUMENT in filters and message.document or
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2016
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
20+
""" This module contains the base class for handlers as used by the
21+
Dispatcher """
22+
23+
from .handler import Handler
24+
25+
26+
class StringCommandHandler(Handler):
27+
28+
def __init__(self, command, callback, pass_args=False,
29+
pass_update_queue=False):
30+
super(Handler).__init__(callback, pass_update_queue)
31+
self.command = command
32+
self.pass_args = pass_args
33+
34+
def checkUpdate(self, update):
35+
return (isinstance(update, str) and
36+
update.startswith('/') and
37+
update[1:].split(' ')[0] == self.command)
38+
39+
def handleUpdate(self, update, dispatcher):
40+
optional_args = self.collectOptionalArgs(dispatcher)
41+
42+
if self.pass_args:
43+
optional_args['args'] = update.split(' ')[1:]
44+
45+
self.callback(dispatcher.bot, update, **optional_args)

telegram/ext/stringregexhandler.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2016
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
20+
""" This module contains the base class for handlers as used by the
21+
Dispatcher """
22+
23+
import re
24+
25+
from .handler import Handler
26+
27+
28+
class RegexHandler(Handler):
29+
30+
def __init__(self, pattern, callback, pass_groups=False,
31+
pass_groupdict=False, pass_update_queue=False):
32+
super(Handler).__init__(callback, pass_update_queue)
33+
34+
if isinstance(pattern, str):
35+
pattern = re.compile(pattern)
36+
37+
self.pattern = pattern
38+
self.pass_groups = pass_groups
39+
self.pass_groupdict = pass_groupdict
40+
41+
def checkUpdate(self, update):
42+
if isinstance(update, str):
43+
match = re.match(self.pattern, update)
44+
return bool(match)
45+
else:
46+
return False
47+
48+
def handleUpdate(self, update, dispatcher):
49+
optional_args = self.collectOptionalArgs(dispatcher)
50+
match = re.match(self.pattern, update)
51+
52+
if self.pass_groups:
53+
optional_args['groups'] = match.groups()
54+
if self.pass_groupdict:
55+
optional_args['groupdict'] = match.groupdict()
56+
57+
self.callback(dispatcher.bot, update, **optional_args)

telegram/ext/typehandler.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2016
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
20+
""" This module contains the base class for handlers as used by the
21+
Dispatcher """
22+
23+
from .handler import Handler
24+
25+
26+
class TypeHandler(Handler):
27+
28+
def __init__(self, type, callback, pass_update_queue=False):
29+
super(Handler).__init__(callback, pass_update_queue)
30+
self.type = type
31+
32+
def checkUpdate(self, update):
33+
return isinstance(update, self.type)
34+
35+
def handleUpdate(self, update, dispatcher):
36+
optional_args = self.collectOptionalArgs(dispatcher)
37+
38+
self.callback(dispatcher.bot, update, **optional_args)

0 commit comments

Comments
 (0)
X Tutup