@@ -359,6 +359,79 @@ def filter(self, message):
359
359
360
360
group = _Group ()
361
361
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
+
362
435
class _Invoice (BaseFilter ):
363
436
name = 'Filters.invoice'
364
437
0 commit comments