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:
Eldinnie 2017-04-23 23:22:05 +02:00 committed by Jannes Höke
parent 35132271af
commit b5b09884b1
3 changed files with 28 additions and 0 deletions

View file

@ -183,6 +183,7 @@ class ConversationHandler(Handler):
res = new_state.result(timeout=self.run_async_timeout) res = new_state.result(timeout=self.run_async_timeout)
except Exception as exc: except Exception as exc:
self.logger.exception("Promise function raised exception") self.logger.exception("Promise function raised exception")
self.logger.exception("{}".format(exc))
error = True error = True
if not error and new_state.done.is_set(): if not error and new_state.done.is_set():

View file

@ -17,6 +17,7 @@
# You should have received a copy of the GNU Lesser Public License # You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
""" This module contains the Filters for use with the MessageHandler class """ """ This module contains the Filters for use with the MessageHandler class """
from telegram import Chat
class BaseFilter(object): class BaseFilter(object):
@ -248,3 +249,17 @@ class Filters(object):
def filter(self, message): def filter(self, message):
return any([entity.type == self.entity_type for entity in message.entities]) 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()

View file

@ -178,6 +178,18 @@ class FiltersTest(BaseTest, unittest.TestCase):
self.message.entities = [self.e(MessageEntity.BOLD), self.e(MessageEntity.MENTION)] self.message.entities = [self.e(MessageEntity.BOLD), self.e(MessageEntity.MENTION)]
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message)) 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): def test_and_filters(self):
self.message.text = 'test' self.message.text = 'test'
self.message.forward_date = True self.message.forward_date = True