X Tutup
Skip to content

Commit 1e19084

Browse files
committed
initial commit for dispatcher rework. deleted updatequeue.py as it is not needed. added handler base class, messagehandler, commandhandler, regexhandler. adjusted dispatcher for new system
1 parent 8b95f9c commit 1e19084

File tree

9 files changed

+331
-575
lines changed

9 files changed

+331
-575
lines changed

telegram/ext/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@
2222
from .dispatcher import Dispatcher
2323
from .jobqueue import JobQueue
2424
from .updater import Updater
25+
from .handler import Handler
26+
from .commandhandler import CommandHandler
27+
from .messagehandler import MessageHandler
28+
from .regexhandler import RegexHandler
2529

26-
__all__ = ('Dispatcher', 'JobQueue', 'Updater')
30+
__all__ = ('Dispatcher', 'JobQueue', 'Updater', 'Handler', 'CommandHandler',
31+
'MessageHandler', 'RegexHandler')

telegram/ext/commandhandler.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 CommandHandler(Handler):
28+
29+
def __init__(self, command, callback, pass_args=False,
30+
pass_update_queue=False):
31+
super(Handler).__init__(callback, pass_update_queue)
32+
self.command = command
33+
self.pass_args = pass_args
34+
35+
def checkUpdate(self, update):
36+
return (isinstance(update, Update) and
37+
update.message and
38+
update.message.text and
39+
update.message.text.startswith('/') and
40+
update.message.text[1:].split(' ')[0].split('@')[0] ==
41+
self.command)
42+
43+
def handleUpdate(self, update, dispatcher):
44+
optional_args = self.collectOptionalArgs(dispatcher)
45+
46+
if self.pass_args:
47+
optional_args['args'] = update.message.text.split(' ')[1:]
48+
49+
self.callback(dispatcher.bot, update, **optional_args)

0 commit comments

Comments
 (0)
X Tutup