def __get_users(bot, update): """Command handler function for `users` command. Sends a list of all currently registered users. Args: bot (telegram.Bot): The bot object. update (telegram.Update): The sent update. """ message = str() config = UserManager().config if not config: message = "No users registered" bot.sendMessage(chat_id=update.message.chat_id, text=message) for group, data in config.items(): message += "*{0}*\n".format(group) if data.get("users"): message += " verified users:\n" for user in data.get("users"): message += " - {0} with id {1}\n" \ .format(user.get("username"), user.get("id")) if data.get("unverified"): message += " unverified users:\n" for user in data.get("unverified"): message += " - {0}\n".format(user) bot.sendMessage(chat_id=update.message.chat_id, text=message, parse_mode=ParseMode.MARKDOWN)
def call(*args, **kwargs): offset = 0 # Set offset to 1 if first argument is not type of # telegram.Bot. if not isinstance(args[0], Bot): offset = 1 bot = args[0 + offset] update = args[1 + offset] user = User( update.message.from_user.name, user_id=update.message.from_user.id, group=group, ) if UserManager().group_is_empty(group): user.save() message = "Hello {0}! "\ "You have been added to the '{1}' group."\ .format( update.message.from_user.first_name, group ) bot.sendMessage(chat_id=update.message.chat_id, text=message) result = func(*args, **kwargs) return result
def __rm_user(bot, update, args): """Command handler function for `rmuser` command. Removes a telegram user from a usergroup. Args: bot (telegram.Bot): The bot object. update (telegram.Update): The sent update. args (list): The command's arguments. """ if len(args) != 2: message = "Usage: rmuser <user> <group>" bot.sendMessage(chat_id=update.message.chat_id, text=message) return username = args[0] group = args[1] if UserManager().rm_user(username, group): message = "Removed user '{0}' from the group '{1}'.".format( username, group) else: message = "The user '{0}' could not be found in the group '{1}'!"\ .format(username, group) bot.sendMessage(chat_id=update.message.chat_id, text=message)
def __add_user(bot, update, args): """Command handler function for `adduser` command. Adds a telegram user to a usergroup. Args: bot (telegram.Bot): The bot object. update (telegram.Update): The sent update. args (list): The command's arguments. """ if len(args) != 2: message = "Usage: adduser <user> <group>" bot.sendMessage(chat_id=update.message.chat_id, text=message) return username = args[0] group = args[1] if not UserManager().add_user(username, group): message = "The user '{0}' is already in the group '{1}'!" \ .format(username, group) else: message = "Added user '{0}' to the group '{1}'." \ .format(username, group) bot.sendMessage(chat_id=update.message.chat_id, text=message)
def test_init(self): """ Test the init function of the usermanager """ with patch("os.path.exists", return_value=False),\ patch("os.mkdir") as mkdir_mock: mkdir_mock.return_value = True UserManager() self.assertTrue(mkdir_mock.called)
class User(object): """Represents a telegram user. Args: name (str): The user's unique telegram username. user_id (str): The user's unique telegram id. group (Optional[str]): The user's group. """ def __init__(self, name, user_id, group=None): self.__name = name self.__id = user_id self.__group = group self.__usermanager = UserManager() def save(self): """Saves the user's data. Saves the users's data to the configuration file. Returns: bool: True if the user was saved, otherwise False """ if not self.__group: return False self.__usermanager.add_user(self.__name, self.__group, user_id=self.__id) return True def has_access(self, group): """Checks if the user is in given group. Returns True if given user has access rights to the given group. Args: group (str): The group's name. Returns: bool: True if user is in the given group, otherwise False. """ is_in_group = self.__usermanager.user_is_in_group(group, user_id=self.__id) is_admin = self.__usermanager.user_is_in_group("admin", user_id=self.__id) if is_in_group or is_admin: self.save() return True is_admin = self.__usermanager.verify_user(self.__id, self.__name, "admin") is_in_group = self.__usermanager.verify_user(self.__id, self.__name, group) return is_admin or is_in_group
def __init__(self, name, user_id, group=None): self.__name = name self.__id = user_id self.__group = group self.__usermanager = UserManager()
def __get_dummy_object(): """Returns a dummy usermanager object.""" with patch("os.mkdir"): return UserManager()
def __init__(self, dispatcher): self.__usermanager = UserManager() self.__dispatcher = dispatcher self.__register_handlers()