|
| 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