Consider changing this condition
|
if not (bool(user_id) ^ bool(username)): |
to something like
if (user_id is None) == (username is None):
To avoid throwing the ValueError "One and only one of user_id or username must be used" when an empty list of user_ids or usernames is issued.
Similarly for Filters.chat.
PS This should also be 3 to 4 times faster.