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_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 process_remove_wat(message): """Removes a WAT from database.""" 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 bot name') nekowat.register_next_step_handler(msg, lambda m: process_remove_wat(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_remove_wat(m)) return # Remove result = nekowat.remove_wat(wat.doc_id) if result: nekowat.send_message(chat_id, 'Removed WAT %s' % name, reply_markup=hide_markup) return nekowat.send_message(chat_id, 'Failed to remove WAT WAT %s' % name, reply_markup=hide_markup) return
def process_set_expressions(message, name): """Sets the expressions a wat.""" chat_id = message.chat.id if message.content_type != 'text': msg = nekowat.send_message( chat_id, 'You need to send a comma separated list of expressions') nekowat.register_next_step_handler( msg, lambda m: process_set_expressions(m, name)) return if message.text == '/cancel': nekowat.send_message(chat_id, 'Operation cancelled') return # Update record expressions = [e.lower().strip() for e in message.text.split(',')] nekowat.set_wat_expressions(name, expressions) nekowat.send_message(chat_id, 'Expressions updated')
def process_add_image(message, name): """Adds an image to the WAT.""" chat_id = message.chat.id if message.content_type == 'text' and message.text == '/cancel': nekowat.send_message(chat_id, 'Operation cancelled') return if message.content_type != 'photo': 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)) return # Get file IDs file_ids = [p.file_id for p in message.photo] # Create record nekowat.create_wat(name, file_ids) nekowat.send_message(chat_id, 'Added correctly!')
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 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))