def export_pack(bot: Bot, update, pack): user_id = update.effective_user.id if not pack: markup = create_select_sticker_menu(user_id, EXPORT_CHAR, send_add_button=False) edit_or_send(bot, update, "Select the pack to export", reply_markup=markup) return with storage.session_scope() as session: pack_entries = storage.get_entries(session, user_id, pack, False) if not pack: bot.send_message(user_id, "Pack not found") return packs = [(pack, pack_entries)] out_buf = export_json(packs) filename = pack.replace(" ", "_") + ".json" bot.send_document(chat_id=user_id, document=out_buf, filename=filename)
def add_sticker_pack(bot, update, pack, user_data): chat_id = update.effective_chat.id if not pack: pack = user_data.get('pack') if not pack: edit_or_send( bot, update, "Select the pack to expand with the sticker pack", create_select_sticker_menu(update.effective_user.id, ADD_SUBPACK_CHAR)) return with storage.session_scope() as session: has_pack = storage.has_pack(session, update.effective_user.id, pack) if not has_pack: bot.send_message(chat_id=chat_id, text="Pack not found") return user_data['pack'] = pack edit_or_send(bot, update, text="Send me the sticker packs to append", reply_markup=add_sticker_pack_reply_markup) # Set state to ADD_PACK so that we receive the next user's messages return ADD_SUBPACK_CHAR
def add_stickers(bot, update, pack, user_data): chat_id = update.effective_chat.id if not pack: pack = user_data.get('pack') if not pack: edit_or_send( bot, update, "Select the pack to expand", create_select_sticker_menu(update.effective_user.id, ADD_MEDIA_CHAR)) return ADD_SUBPACK_CHAR with storage.session_scope() as session: has_pack = storage.has_pack(session, update.effective_user.id, pack) if not has_pack: bot.send_message(chat_id=chat_id, text="Pack not found") return user_data['pack'] = pack edit_or_send(bot, update, text="Send me the media to add", reply_markup=add_sticker_reply_markup) return ADD_MEDIA_CHAR
def create_pack(bot, update, name, user_data): if not name: edit_or_send(bot, update, "Write the new pack name:", reply_markup=create_markup) return CREATE_PACK_CHAR name = name.lower() # Case insensitive as always name_error = check_name(name) if name_error: bot.send_message(chat_id=update.message.chat_id, text=name_error, reply_markup=create_markup) return user_data['pack'] = name bot.send_message(chat_id=update.effective_chat.id, text="Now send me all of your stickers :3", reply_markup=add_sticker_reply_markup) return ADD_MEDIA_CHAR
def remove_pack(bot, update, name): if not name: reply_markup = create_select_sticker_menu(update.effective_user.id, REMOVE_PACK_CHAR, send_add_button=False) edit_or_send(bot, update, "What pack do you want to remove?", reply_markup) return with storage.session_scope() as session: remove_succeded = storage.remove_pack(session, update.effective_user.id, name.lower()) if remove_succeded: edit_or_send(bot, update, 'Pack removed successfully!', reply_markup=start_menu_markup) else: edit_or_send(bot, update, 'Pack not found')
def view_pack(bot, update, arg: str = None, user_data=None): if not arg: edit_or_send( bot, update, "Select the pack to view", create_select_sticker_menu(update.effective_user.id, VIEW_PACK_CHAR)) return chat_id = update.effective_chat.id # Split sticker and offset # In theory the divider should not be part of the name so there's no # confusion when splitting the command # As a matter of fact the character selected is filtered out of the possible names args = arg.rsplit(OFFSET_DIVIDER, 1) name = args[0].lower() if len(args) < 2: offset = 0 else: try: offset = int(args[1]) except ValueError: offset = 0 entries, more = get_pack_entries(bot, update.effective_user.id, name, offset * MAX_VIEW_RESULTS, MAX_VIEW_RESULTS) logging.debug("Entries for %s: %s", name, entries) if entries is None: edit_or_send(bot, update, "Cannot find pack") return buttons = [] navigate_buttons = [] if offset != 0: navigate_buttons.append( InlineKeyboardButton('Back', callback_data=VIEW_PACK_CHAR + name + OFFSET_DIVIDER + str(offset - 1))) if more: navigate_buttons.append( InlineKeyboardButton('Next', callback_data=VIEW_PACK_CHAR + name + OFFSET_DIVIDER + str(offset + 1))) if navigate_buttons: buttons.append(navigate_buttons) buttons += [ [InlineKeyboardButton('Add', callback_data=ADD_MEDIA_CHAR + name)], [ InlineKeyboardButton('Append Pack', callback_data=ADD_SUBPACK_CHAR + name) ], [ InlineKeyboardButton('Remove Pack', callback_data=REMOVE_PACK_CHAR + name) ], [InlineKeyboardButton('Menu', callback_data=VIEW_MENU_CHAR)], ] more_markup = InlineKeyboardMarkup(buttons) if 'last_messages' in user_data: message_ids = user_data.pop('last_messages') for id in message_ids: bot.delete_message(chat_id=chat_id, message_id=id) if entries: message_ids = [] for entry, has_more in lookahead(entries): entry_type, content = entry try: if entry_type == EntryType.STICKER: message = bot.send_sticker( chat_id=chat_id, sticker=content, timeout=3000, reply_markup=None if has_more else more_markup) elif entry_type == EntryType.GIF: message = bot.send_document( chat_id=chat_id, document=content, reply_markup=None if has_more else more_markup) else: logging.warning('Unsupported type: %s', entry_type) continue except BadRequest as req: if req.message != "Document_invalid": raise # Thanks to telegram api every file_id is unique from bot to bot bot.send_message( update.effective_user.id, "Error with file_id, please contact the /author") logging.exception("Error during view operation") return message_ids.append(message.message_id) user_data['last_messages'] = message_ids else: edit_or_send(bot, update, text="No sticker found", reply_markup=more_markup)
def view_menu(bot, update): edit_or_send(bot, update, "Select operation", reply_markup=start_menu_markup)