Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 470ee86

Browse files
authored
Merge pull request python-telegram-bot#677 from evgfilim1/new-filters
New filters for handling messages from specific chat/user id
2 parents 45d4ea0 + ecfcc69 commit 470ee86

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

telegram/ext/filters.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,79 @@ def filter(self, message):
359359

360360
group = _Group()
361361

362+
class user(BaseFilter):
363+
"""Filters messages to allow only those which are from specified user ID.
364+
365+
Notes:
366+
Only one of chat_id or username must be used here.
367+
368+
Args:
369+
user_id(Optional[int|list]): which user ID(s) to allow through.
370+
username(Optional[str|list]): which username(s) to allow through. If username starts
371+
with '@' symbol, it will be ignored.
372+
373+
Raises:
374+
ValueError
375+
"""
376+
377+
def __init__(self, user_id=None, username=None):
378+
if not (bool(user_id) ^ bool(username)):
379+
raise ValueError('One and only one of user_id or username must be used')
380+
if user_id is not None and isinstance(user_id, int):
381+
self.user_ids = [user_id]
382+
else:
383+
self.user_ids = user_id
384+
if username is None:
385+
self.usernames = username
386+
elif isinstance(username, str_type):
387+
self.usernames = [username.replace('@', '')]
388+
else:
389+
self.usernames = [user.replace('@', '') for user in username]
390+
391+
def filter(self, message):
392+
if self.user_ids is not None:
393+
return bool(message.from_user and message.from_user.id in self.user_ids)
394+
else:
395+
# self.usernames is not None
396+
return bool(message.from_user and message.from_user.username and
397+
message.from_user.username in self.usernames)
398+
399+
class chat(BaseFilter):
400+
"""Filters messages to allow only those which are from specified chat ID.
401+
402+
Notes:
403+
Only one of chat_id or username must be used here.
404+
405+
Args:
406+
chat_id(Optional[int|list]): which chat ID(s) to allow through.
407+
username(Optional[str|list]): which username(s) to allow through. If username starts
408+
with '@' symbol, it will be ignored.
409+
410+
Raises:
411+
ValueError
412+
"""
413+
414+
def __init__(self, chat_id=None, username=None):
415+
if not (bool(chat_id) ^ bool(username)):
416+
raise ValueError('One and only one of chat_id or username must be used')
417+
if chat_id is not None and isinstance(chat_id, int):
418+
self.chat_ids = [chat_id]
419+
else:
420+
self.chat_ids = chat_id
421+
if username is None:
422+
self.usernames = username
423+
elif isinstance(username, str_type):
424+
self.usernames = [username.replace('@', '')]
425+
else:
426+
self.usernames = [chat.replace('@', '') for chat in username]
427+
428+
def filter(self, message):
429+
if self.chat_ids is not None:
430+
return bool(message.chat_id in self.chat_ids)
431+
else:
432+
# self.usernames is not None
433+
return bool(message.chat.username and message.chat.username in self.usernames)
434+
362435
class _Invoice(BaseFilter):
363436
name = 'Filters.invoice'
364437

tests/test_filters.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,51 @@ def test_group_fileter(self):
213213
self.message.chat.type = "supergroup"
214214
self.assertTrue(Filters.group(self.message))
215215

216+
def test_filters_chat(self):
217+
with self.assertRaisesRegexp(ValueError, 'chat_id or username'):
218+
Filters.chat(chat_id=-1, username='chat')
219+
with self.assertRaisesRegexp(ValueError, 'chat_id or username'):
220+
Filters.chat()
221+
222+
def test_filters_chat_id(self):
223+
self.assertFalse(Filters.chat(chat_id=-1)(self.message))
224+
self.message.chat.id = -1
225+
self.assertTrue(Filters.chat(chat_id=-1)(self.message))
226+
self.message.chat.id = -2
227+
self.assertTrue(Filters.chat(chat_id=[-1, -2])(self.message))
228+
self.assertFalse(Filters.chat(chat_id=-1)(self.message))
229+
230+
def test_filters_chat_username(self):
231+
self.assertFalse(Filters.chat(username='chat')(self.message))
232+
self.message.chat.username = 'chat'
233+
self.assertTrue(Filters.chat(username='@chat')(self.message))
234+
self.assertTrue(Filters.chat(username='chat')(self.message))
235+
self.assertTrue(Filters.chat(username=['chat1', 'chat', 'chat2'])(self.message))
236+
self.assertFalse(Filters.chat(username=['@chat1', 'chat_2'])(self.message))
237+
238+
def test_filters_user(self):
239+
with self.assertRaisesRegexp(ValueError, 'user_id or username'):
240+
Filters.user(user_id=1, username='user')
241+
with self.assertRaisesRegexp(ValueError, 'user_id or username'):
242+
Filters.user()
243+
244+
def test_filters_user_id(self):
245+
self.assertFalse(Filters.user(user_id=1)(self.message))
246+
self.message.from_user.id = 1
247+
self.assertTrue(Filters.user(user_id=1)(self.message))
248+
self.message.from_user.id = 2
249+
self.assertTrue(Filters.user(user_id=[1, 2])(self.message))
250+
self.assertFalse(Filters.user(user_id=1)(self.message))
251+
252+
def test_filters_username(self):
253+
self.assertFalse(Filters.user(username='user')(self.message))
254+
self.assertFalse(Filters.user(username='Testuser')(self.message))
255+
self.message.from_user.username = 'user'
256+
self.assertTrue(Filters.user(username='@user')(self.message))
257+
self.assertTrue(Filters.user(username='user')(self.message))
258+
self.assertTrue(Filters.user(username=['user1', 'user', 'user2'])(self.message))
259+
self.assertFalse(Filters.user(username=['@username', '@user_2'])(self.message))
260+
216261
def test_and_filters(self):
217262
self.message.text = 'test'
218263
self.message.forward_date = True

0 commit comments

Comments
 (0)