mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 15:17:00 +01:00
Group filters (#575)
* stripping token of whitespaces before starting bot * Line feed * show exception that's caused (fixes flake8 failing) * Add private/group filters Add filters: - Filters.private (for messages in private chats) - Filters.group (for messages in group chats) * use constants
This commit is contained in:
parent
35132271af
commit
b5b09884b1
3 changed files with 28 additions and 0 deletions
|
@ -183,6 +183,7 @@ class ConversationHandler(Handler):
|
|||
res = new_state.result(timeout=self.run_async_timeout)
|
||||
except Exception as exc:
|
||||
self.logger.exception("Promise function raised exception")
|
||||
self.logger.exception("{}".format(exc))
|
||||
error = True
|
||||
|
||||
if not error and new_state.done.is_set():
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
# 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 for use with the MessageHandler class """
|
||||
from telegram import Chat
|
||||
|
||||
|
||||
class BaseFilter(object):
|
||||
|
@ -248,3 +249,17 @@ class Filters(object):
|
|||
|
||||
def filter(self, message):
|
||||
return any([entity.type == self.entity_type for entity in message.entities])
|
||||
|
||||
class _Private(BaseFilter):
|
||||
|
||||
def filter(self, message):
|
||||
return message.chat.type == Chat.PRIVATE
|
||||
|
||||
private = _Private()
|
||||
|
||||
class _Group(BaseFilter):
|
||||
|
||||
def filter(self, message):
|
||||
return message.chat.type in [Chat.GROUP, Chat.SUPERGROUP]
|
||||
|
||||
group = _Group()
|
||||
|
|
|
@ -178,6 +178,18 @@ class FiltersTest(BaseTest, unittest.TestCase):
|
|||
self.message.entities = [self.e(MessageEntity.BOLD), self.e(MessageEntity.MENTION)]
|
||||
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
|
||||
|
||||
def test_private_filter(self):
|
||||
self.assertTrue(Filters.private(self.message))
|
||||
self.message.chat.type = "group"
|
||||
self.assertFalse(Filters.private(self.message))
|
||||
|
||||
def test_group_fileter(self):
|
||||
self.assertFalse(Filters.group(self.message))
|
||||
self.message.chat.type = "group"
|
||||
self.assertTrue(Filters.group(self.message))
|
||||
self.message.chat.type = "supergroup"
|
||||
self.assertTrue(Filters.group(self.message))
|
||||
|
||||
def test_and_filters(self):
|
||||
self.message.text = 'test'
|
||||
self.message.forward_date = True
|
||||
|
|
Loading…
Reference in a new issue