|
| 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 | +""" This module contains the MessageHandler class """ |
| 20 | + |
| 21 | + |
| 22 | +class BaseFilter(object): |
| 23 | + """Base class for all Message Filters""" |
| 24 | + |
| 25 | + def __call__(self, message): |
| 26 | + raise NotImplementedError('Please implement a call method in your filter.') |
| 27 | + |
| 28 | + def __and__(self, other): |
| 29 | + return MergedFilter(self, and_filter=other) |
| 30 | + |
| 31 | + def __or__(self, other): |
| 32 | + return MergedFilter(self, or_filter=other) |
| 33 | + |
| 34 | + |
| 35 | +class MergedFilter(BaseFilter): |
| 36 | + """Represents a filter consisting of two other filters.""" |
| 37 | + |
| 38 | + def __init__(self, base_filter, and_filter=None, or_filter=None): |
| 39 | + self.base_filter = base_filter |
| 40 | + self.and_filter = and_filter |
| 41 | + self.or_filter = or_filter |
| 42 | + |
| 43 | + def __call__(self, message): |
| 44 | + if self.and_filter: |
| 45 | + return self.base_filter(message) and self.and_filter(message) |
| 46 | + elif self.or_filter: |
| 47 | + return self.base_filter(message) or self.or_filter(message) |
| 48 | + |
| 49 | + |
| 50 | +class Filters(object): |
| 51 | + """ |
| 52 | + Convenient namespace (class) & methods for the filter funcs of the |
| 53 | + MessageHandler class. |
| 54 | + """ |
| 55 | + |
| 56 | + class Text(BaseFilter): |
| 57 | + |
| 58 | + def __call__(self, message): |
| 59 | + return bool(message.text and not message.text.startswith('/')) |
| 60 | + |
| 61 | + text = Text() |
| 62 | + |
| 63 | + class Command(BaseFilter): |
| 64 | + |
| 65 | + def __call__(self, message): |
| 66 | + return bool(message.text and message.text.startswith('/')) |
| 67 | + |
| 68 | + command = Command() |
| 69 | + |
| 70 | + class Audio(BaseFilter): |
| 71 | + |
| 72 | + def __call__(self, message): |
| 73 | + return bool(message.audio) |
| 74 | + |
| 75 | + audio = Audio() |
| 76 | + |
| 77 | + class Document(BaseFilter): |
| 78 | + |
| 79 | + def __call__(self, message): |
| 80 | + return bool(message.document) |
| 81 | + |
| 82 | + document = Document() |
| 83 | + |
| 84 | + class Photo(BaseFilter): |
| 85 | + |
| 86 | + def __call__(self, message): |
| 87 | + return bool(message.photo) |
| 88 | + |
| 89 | + photo = Photo() |
| 90 | + |
| 91 | + class Sticker(BaseFilter): |
| 92 | + |
| 93 | + def __call__(self, message): |
| 94 | + return bool(message.sticker) |
| 95 | + |
| 96 | + sticker = Sticker() |
| 97 | + |
| 98 | + class Video(BaseFilter): |
| 99 | + |
| 100 | + def __call__(self, message): |
| 101 | + return bool(message.video) |
| 102 | + |
| 103 | + video = Video() |
| 104 | + |
| 105 | + class Voice(BaseFilter): |
| 106 | + |
| 107 | + def __call__(self, message): |
| 108 | + return bool(message.voice) |
| 109 | + |
| 110 | + voice = Voice() |
| 111 | + |
| 112 | + class Contact(BaseFilter): |
| 113 | + |
| 114 | + def __call__(self, message): |
| 115 | + return bool(message.contact) |
| 116 | + |
| 117 | + contact = Contact() |
| 118 | + |
| 119 | + class Location(BaseFilter): |
| 120 | + |
| 121 | + def __call__(self, message): |
| 122 | + return bool(message.location) |
| 123 | + |
| 124 | + location = Location() |
| 125 | + |
| 126 | + class Venue(BaseFilter): |
| 127 | + |
| 128 | + def __call__(self, message): |
| 129 | + return bool(message.venue) |
| 130 | + |
| 131 | + venue = Venue() |
| 132 | + |
| 133 | + class StatusUpdate(BaseFilter): |
| 134 | + |
| 135 | + def __call__(self, message): |
| 136 | + return bool(message.new_chat_member or message.left_chat_member |
| 137 | + or message.new_chat_title or message.new_chat_photo |
| 138 | + or message.delete_chat_photo or message.group_chat_created |
| 139 | + or message.supergroup_chat_created or message.channel_chat_created |
| 140 | + or message.migrate_to_chat_id or message.migrate_from_chat_id |
| 141 | + or message.pinned_message) |
| 142 | + |
| 143 | + status_update = StatusUpdate() |
| 144 | + |
| 145 | + class Forwarded(BaseFilter): |
| 146 | + |
| 147 | + def __call__(self, message): |
| 148 | + return bool(message.forward_date) |
| 149 | + |
| 150 | + forwarded = Forwarded() |
0 commit comments