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 on_add_sticker_response(bot, update, user_data): user_id = update.effective_user.id entry, entry_type, name = process_entry_from_response(bot, update) if not entry: bot.send_message(chat_id=user_id, text="type not supported", reply_markup=add_sticker_reply_markup) return limits_reached = not limits.check_insertion(user_id) with storage.session_scope() as session: add_result = storage.add_entry(session, user_id, user_data['pack'], entry_type, entry, only_remove=limits_reached) if limits_reached and add_result: bot.send_message(chat_id=user_id, text="Entry limits exceeded", reply_markup=limits.limits_report_markup) elif add_result: bot.send_message(chat_id=user_id, text='%s added' % name, reply_markup=add_sticker_reply_markup) else: bot.send_message(chat_id=user_id, text='%s removed' % name, reply_markup=add_sticker_reply_markup)
def on_stickerpack_removed(bot, user_id, stickerpack_name): """Called whenever a stickerpack of a user gets removed""" bot.send_message( user_id, "The sticker pack %s has been removed, sorry for the inconvenience" % stickerpack_name) with storage.session_scope() as session: storage.remove_every_pack_mention(session, user_id, stickerpack_name)
def get_pack_entries(bot, user_id, pack_name, offset, limit=1000000, similar=False): assert limit > 0 with storage.session_scope() as session: entries = storage.get_entries(session, user_id, pack_name, similar) discard_remaining = offset remaining = limit result = [] more = False for entry_type, entry in entries: logging.debug( "e: %s, discard_remaining: %s, remaining: %s, result: %s", (entry_type, entry), discard_remaining, remaining, len(result)) if remaining <= 0: more = True break entry_type = EntryType(entry_type) if entry_type == EntryType.PACK: try: pack = bot.get_sticker_set(entry) except BadRequest: on_stickerpack_removed(bot, user_id, entry) continue stickers = pack.stickers if len(stickers) <= discard_remaining: discard_remaining -= len(stickers) continue elif discard_remaining > 0: stickers = stickers[discard_remaining:] discard_remaining = 0 if len(stickers) > remaining: stickers = stickers[:remaining] more = True remaining -= len(stickers) result += ((EntryType.STICKER, sticker.file_id) for sticker in stickers) # Temporarily remove local sticker pack support '''elif entry_type == EntryType.LOCAL_PACK: stickers, pack_more = get_pack_entries(bot, user_id, entry, discard_remaining, limit - len(result)) remaining -= len(stickers) if remaining == 0: more = pack_more result += stickers''' elif discard_remaining: discard_remaining -= 1 else: remaining -= 1 result.append((entry_type, entry)) logging.debug("DiscardRem: %s, res: %s", discard_remaining, len(result)) assert discard_remaining == 0 return result, more
def import_json(self): if not limits.check_insertion(self.user_id, inserted_count=self.new_media_count): return False with storage.session_scope() as session: for entry in self.imports: entry.import_pack(session) return True
def initialize_from_json(self, raw): packs = raw['packs'] self.imports = [] with storage.session_scope() as session: for raw_pack in packs: import_entry = ImportEntry(self.user_id, raw_pack) self.imports.append(import_entry) import_entry.initialize(session)
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 limits_report(bot, update): user_id = update.effective_user.id with storage.session_scope() as session: current_entries = storage.count_total_entries(session, user_id) max_entries = get_max_entries(user_id) text = "Current entries:\n" \ "%d/%d (%d%%)\n" \ "Remaining: %d entries" % (current_entries, max_entries, (current_entries * 100) // max_entries, max_entries - current_entries) # TODO: enable real plans or whatever bot.send_message(chat_id=user_id, text=text, reply_markup=change_plan_markup)
def on_add_pack_response(bot, update, user_data): user_id = update.effective_user.id if update.message.sticker: entry = update.message.sticker.set_name entry_type = EntryType.PACK name = entry # Temporarily remove local sticker pack support '''elif update.message.text: entry = update.message.text if not storage.has_pack(update.effective_user.id, entry): bot.send_message(chat_id=chat_id, text="Pack '%s' not found" % entry) return entry_type = EntryType.LOCAL_PACK name = entry''' else: logging.warning("Unsupported message received '%s'", update.message) bot.send_message(chat_id=user_id, text="type not supported", reply_markup=add_sticker_pack_reply_markup) return limits_reached = not limits.check_insertion(user_id) with storage.session_scope() as session: add_result = storage.add_entry(session, user_id, user_data['pack'], entry_type, entry, only_remove=limits_reached) if limits_reached and add_result: bot.send_message(chat_id=user_id, text="Entry limits exceeded", reply_markup=limits.limits_report_markup) elif add_result: bot.send_message(chat_id=user_id, text='%s appended' % name, reply_markup=add_sticker_pack_reply_markup) else: bot.send_message(chat_id=user_id, text='%s removed' % name, reply_markup=add_sticker_pack_reply_markup)
def create_select_sticker_menu(user_id, callback_char, send_add_button=True, send_back_button=True): with storage.session_scope() as session: sticker_packs = storage.get_packs(session, user_id) buttons = [ InlineKeyboardButton(pack, callback_data=callback_char + str(pack)) for pack in sticker_packs ] footer_buttons = [] if send_add_button: footer_buttons.append( InlineKeyboardButton("Add entry", callback_data=CREATE_PACK_CHAR)) if send_back_button: footer_buttons.append(back_button) menu = build_menu(buttons, 1, None, footer_buttons) return InlineKeyboardMarkup(menu)
def check_insertion(user_id, inserted_count=1): with storage.session_scope() as session: current_entries = storage.count_total_entries(session, user_id) return current_entries + inserted_count <= MAX_ENTRIES