@@ -887,7 +887,8 @@ def filter(self, message):
887
887
"""Messages sent in a group chat."""
888
888
889
889
class user (BaseFilter ):
890
- """Filters messages to allow only those which are from specified user ID.
890
+ """Filters messages to allow only those which are from specified user ID(s) or
891
+ username(s).
891
892
892
893
Examples:
893
894
``MessageHandler(Filters.user(1234), callback_method)``
@@ -919,7 +920,6 @@ class user(BaseFilter):
919
920
RuntimeError: If user_id and username are both present.
920
921
921
922
"""
922
-
923
923
def __init__ (self , user_id = None , username = None , allow_empty = False ):
924
924
self .allow_empty = allow_empty
925
925
self .__lock = Lock ()
@@ -1053,8 +1053,171 @@ def filter(self, message):
1053
1053
return self .allow_empty
1054
1054
return False
1055
1055
1056
+ class via_bot (BaseFilter ):
1057
+ """Filters messages to allow only those which are from specified via_bot ID(s) or
1058
+ username(s).
1059
+
1060
+ Examples:
1061
+ ``MessageHandler(Filters.via_bot(1234), callback_method)``
1062
+
1063
+ Warning:
1064
+ :attr:`bot_ids` will give a *copy* of the saved bot ids as :class:`frozenset`. This
8000
td>1065
+ is to ensure thread safety. To add/remove a bot, you should use :meth:`add_usernames`,
1066
+ :meth:`add_bot_ids`, :meth:`remove_usernames` and :meth:`remove_bot_ids`. Only update
1067
+ the entire set by ``filter.bot_ids/usernames = new_set``, if you are entirely sure
1068
+ that it is not causing race conditions, as this will complete replace the current set
1069
+ of allowed bots.
1070
+
1071
+ Attributes:
1072
+ bot_ids(set(:obj:`int`), optional): Which bot ID(s) to allow through.
1073
+ usernames(set(:obj:`str`), optional): Which username(s) (without leading '@') to allow
1074
+ through.
1075
+ allow_empty(:obj:`bool`, optional): Whether updates should be processed, if no bot
1076
+ is specified in :attr:`bot_ids` and :attr:`usernames`.
1077
+
1078
+ Args:
1079
+ bot_id(:obj:`int` | List[:obj:`int`], optional): Which bot ID(s) to allow
1080
+ through.
1081
+ username(:obj:`str` | List[:obj:`str`], optional): Which username(s) to allow
1082
+ through. Leading '@'s in usernames will be discarded.
1083
+ allow_empty(:obj:`bool`, optional): Whether updates should be processed, if no user
1084
+ is specified in :attr:`bot_ids` and :attr:`usernames`. Defaults to :obj:`False`
1085
+
1086
+ Raises:
1087
+ RuntimeError: If bot_id and username are both present.
1088
+ """
1089
+
1090
+ def __init__ (self , bot_id = None , username = None , allow_empty = False ):
1091
+ self .allow_empty = allow_empty
1092
+ self .__lock = Lock ()
1093
+
1094
+ self ._bot_ids = set ()
1095
+ self ._usernames = set ()
1096
+
1097
+ self ._set_bot_ids (bot_id )
1098
+ self ._set_usernames (username )
1099
+
1100
+ @staticmethod
1101
+ def _parse_bot_id (bot_id ):
1102
+ if bot_id is None :
1103
+ return set ()
1104
+ if isinstance (bot_id , int ):
1105
+ return {bot_id }
1106
+ return set (bot_id )
1107
+
1108
+ @staticmethod
1109
+ def _parse_username (username ):
1110
+ if username is None :
1111
+ return set ()
1112
+ if isinstance (username , str ):
1113
+ return {username [1 :] if username .startswith ('@' ) else username }
1114
+ return {bot [1 :] if bot .startswith ('@' ) else bot for bot in username }
1115
+
1116
+ def _set_bot_ids (self , bot_id ):
1117
+ with self .__lock :
1118
+ if bot_id and self ._usernames :
1119
+ raise RuntimeError ("Can't set bot_id in conjunction with (already set) "
1120
+ "usernames." )
1121
+ self ._bot_ids = self ._parse_bot_id (bot_id )
1122
+
1123
+ def _set_usernames (self , username ):
1124
+ with self .__lock :
1125
+ if username and self ._bot_ids :
1126
+ raise RuntimeError ("Can't set username in conjunction with (already set) "
1127
+ "bot_ids." )
1128
+ self ._usernames = self ._parse_username (username )
1129
+
1130
+ @property
1131
+ def bot_ids (self ):
1132
+ with self .__lock :
1133
+ return frozenset (self ._bot_ids )
1134
+
1135
+ @bot_ids .setter
1136
+ def bot_ids (self , bot_id ):
1137
+ self ._set_bot_ids (bot_id )
1138
+
1139
+ @property
1140
+ def usernames (self ):
1141
+ with self .__lock :
1142
+ return frozenset (self ._usernames )
1143
+
1144
+ @usernames .setter
1145
+ def usernames (self , username ):
1146
+ self ._set_usernames (username )
1147
+
1148
+ def add_usernames (self , username ):
1149
+ """
1150
+ Add one or more users to the allowed usernames.
1151
+ Args:
1152
+ username(:obj:`str` | List[:obj:`str`], optional): Which username(s) to allow
1153
+ through. Leading '@'s in usernames will be discarded.
1154
+ """
1155
+ with self .__lock :
1156
+ if self ._bot_ids :
1157
+ raise RuntimeError ("Can't set username in conjunction with (already set) "
1158
+ "bot_ids." )
1159
+
1160
+ username = self ._parse_username (username )
1161
+ self ._usernames |= username
1162
+
1163
+ def add_bot_ids (self , bot_id ):
1164
+ """
1165
+ Add one or more users to the allowed user ids.
1166
+ Args:
1167
+ bot_id(:obj:`int` | List[:obj:`int`], optional): Which bot ID(s) to allow
1168
+ through.
1169
+ """
1170
+ with self .__lock :
1171
+ if self ._usernames :
1172
+ raise RuntimeError ("Can't set bot_id in conjunction with (already set) "
1173
+ "usernames." )
1174
+
1175
+ bot_id = self ._parse_bot_id (bot_id )
1176
+
1177
+ self ._bot_ids |= bot_id
1178
+
1179
+ def remove_usernames (self , username ):
1180
+ """
1181
+ Remove one or more users from allowed usernames.
1182
+ Args:
1183
+ username(:obj:`str` | List[:obj:`str`], optional): Which username(s) to disallow
1184
+ through. Leading '@'s in usernames will be discarded.
1185
+ """
1186
+ with self .__lock :
1187
+ if self ._bot_ids :
1188
+ raise RuntimeError ("Can't set username in conjunction with (already set) "
1189
+ "bot_ids." )
1190
+
1191
+ username = self ._parse_username (username )
1192
+ self ._usernames -= username
1193
+
1194
+ def remove_bot_ids (self , bot_id ):
1195
+ """
1196
+ Remove one or more users from allowed user ids.
1197
+ Args:
1198
+ bot_id(:obj:`int` | List[:obj:`int`], optional): Which bot ID(s) to disallow
1199
+ through.
1200
+ """
1201
+ with self .__lock :
1202
+ if self ._usernames :
1203
+ raise RuntimeError ("Can't set bot_id in conjunction with (already set) "
1204
+ "usernames." )
1205
+ bot_id = self ._parse_bot_id (bot_id )
1206
+ self ._bot_ids -= bot_id
1207
+
1208
+ def filter (self , message ):
1209
+ """""" # remove method from docs
1210
+ if message .via_bot :
1211
+ if self .bot_ids :
1212
+ return message .via_bot .id in self .bot_ids
1213
+ if self .usernames :
1214
+ return (message .via_bot .username
1215
+ and message .via_bot .username in self .usernames )
1216
+ return self .allow_empty
1217
+ return False
1218
+
1056
1219
class chat (BaseFilter ):
1057
- """Filters messages to allow only those which are from specified chat ID.
1220
+ """Filters messages to allow only those which are from a specified chat ID or username .
1058
1221
1059
1222
Examples:
1060
1223
``MessageHandler(Filters.chat(-1234), callback_method)``
0 commit comments