def handle_add_whitelist(message): """Add a user to the whitelist. Expects a message with the format: /addwhitelist <name> <id> """ if not nekowat.is_owner(message.chat.id): nekowat.reply_to(message, 'You do not have permission to do that') return args = telebot.util.extract_arguments(message.text).split(' ') if len(args) != 2: nekowat.reply_to(message, '/addwhitelist <name> <id>') return try: name = args[0] user_id = int(args[1]) except: nekowat.reply_to(message, '/addwhitelist <name> <id>') return if nekowat.add_whitelist(name, user_id): nekowat.reply_to(message, 'User added to whitelist!') return nekowat.reply_to(message, 'Failed to add user to whitelist')
def handle_set_expressions(message): """Sets expressions for a WAT. This shows all wats by name and then displays the expressions of the WAT that was chosen. """ chat_id = message.chat.id if not nekowat.is_owner(chat_id): nekowat.reply_to(message, 'You do not have permission to do that') return markup = telebot.types.ReplyKeyboardMarkup(row_width=2) for w in nekowat.get_all_wats(): markup.add(telebot.types.KeyboardButton(w['name'])) # Add cancel markup.add(telebot.types.KeyboardButton('/cancel')) msg = nekowat.send_message(chat_id, 'Choose a WAT to modify', reply_markup=markup) nekowat.register_next_step_handler(msg, lambda m: process_get_expressions(m))
def handle_show_whitelist(message): """Show current whitelist.""" if not nekowat.is_owner(message.chat.id): nekowat.reply_to(message, 'You do not have permission to do that') return msg = 'Whitelisted users:\n\n' for name, uid in nekowat.whitelist.items(): msg += '- %s (%d)\n' % (name, uid) nekowat.reply_to(message, msg)
def handle_start(message): """Initialize the bot.""" response = ('nekowatbot - "What the!?"\n\n' '/add <name> : Add a new WAT\n' '/remove : Remove a WAT\n' '/wat <expression> : Get a random WAT\n' '/setexpressions : Set the expressions of a WAT\n' '/addwhitelist <name> <id> : Add user ID to whitelist\n' '/rmwhitelist <name> : Remove user from whitelist\n' '/whitelist : Show current whitelist\n' '/togglewhitelist : Toggle use of whitelist') nekowat.reply_to(message, response)
def handle_toggle_whitelist(message): """Toggle use of whitelist.""" if not nekowat.is_owner(message.chat.id): nekowat.reply_to(message, 'You do not have permission to do that') return nekowat.toggle_whitelist() if nekowat.use_whitelist: status = 'ON' else: status = 'OFF' nekowat.reply_to(message, 'Whitelist is %s' % status)
def process_get_expressions(message): """Shows the expressions of the selected WAT and asks for new ones.""" chat_id = message.chat.id hide_markup = telebot.types.ReplyKeyboardRemove(selective=False) if message.content_type != 'text': msg = nekowat.send_message(chat_id, 'You need to send a WAT name') nekowat.register_next_step_handler( msg, lambda m: process_get_expressions(m)) return if message.text == '/cancel': nekowat.send_message(chat_id, 'Operation cancelled', reply_markup=hide_markup) return # Fetch wat name = message.text wat = nekowat.get_wat(name) if not wat: msg = nekowat.send_message(chat_id, 'No WAT found with that name') nekowat.register_next_step_handler( msg, lambda m: process_get_expressions(m)) return # Show expressions expressions = ','.join(wat['expressions']) msg = nekowat.send_message(chat_id, 'Expressions of %s' % name, reply_markup=hide_markup) nekowat.reply_to(msg, expressions or '[No expressions defined]') msg = nekowat.send_message(chat_id, 'Send a comma separated list of expressions') nekowat.register_next_step_handler( msg, lambda m: process_set_expressions(m, name))
def handle_add(message): """Add a new WAT to the bot. Expects a message with the format: /add <name> """ chat_id = message.chat.id if not nekowat.is_owner(chat_id): nekowat.reply_to(message, 'You do not have permission to do that') return name = telebot.util.extract_arguments(message.text) if not name: nekowat.reply_to(message, '/add <name>') return if nekowat.wat_exists(name): nekowat.reply_to(message, 'There is already a WAT with that name') return msg = nekowat.send_message(chat_id, 'Please send the image for this WAT') nekowat.register_next_step_handler(msg, lambda m: process_add_image(m, name))
def handle_wat(message): """Get a random WAT based on the received expression. Expects a message with the format: /wat <expression> If the expression is empty, gets a random WAT. """ if not nekowat.is_allowed(message.from_user.id): return # Normalize expression expression = telebot.util.extract_arguments(message.text).lower() if not expression: # Get all images wats = nekowat.get_all_wats() else: # Get by expression wats = nekowat.get_wats_by_expression(expression) if not wats: # Default to all WATs wats = nekowat.get_all_wats() if not wats: # Happens when database is empty nekowat.reply_to(message, 'Sorry, I have no WATs that match that') return # Choose random WAT and send file wat = random.choice(wats) nekowat.send_photo( message.chat.id, wat['file_ids'][-1], # Get biggest image reply_to_message_id=message.message_id)
def handle_remove(message): """Handle removing a WAT. This shows all wats by name. """ chat_id = message.chat.id if not nekowat.is_owner(chat_id): nekowat.reply_to(message, 'You do not have permission to do that') return markup = telebot.types.ReplyKeyboardMarkup(row_width=2) for w in nekowat.get_all_wats(): markup.add(telebot.types.KeyboardButton(w['name'])) # Add cancel markup.add(telebot.types.KeyboardButton('/cancel')) msg = nekowat.send_message(chat_id, 'Choose a WAT to delete', reply_markup=markup) nekowat.register_next_step_handler(msg, lambda m: process_remove_wat(m))
def handle_rm_whitelist(message): """Remove a user from the whitelist. Expects a message with the format: /rm-whitelist <name> """ if not nekowat.is_owner(message.chat.id): nekowat.reply_to(message, 'You do not have permission to do that') return name = telebot.util.extract_arguments(message.text) if nekowat.rm_whitelist(name): nekowat.reply_to(message, 'User removed from whitelist!') return nekowat.reply_to(message, 'Failed to remove user from whitelist')
def me(message): """Get user ID.""" nekowat.reply_to(message, message.chat.id)