def on_first_sticker_receive(bot, update, user_data): logger.info('%d: first sticker of the pack received', update.effective_user.id) title, name = user_data['pack'].get('title', None), user_data['pack'].get( 'name', None) if not title or not name: logger.error('pack title or name missing (title: %s, name: %s)', title, name) update.message.reply_text( s.PACK_CREATION_FIRST_STICKER_PACK_DATA_MISSING) user_data.pop('pack', None) # remove temp info user_data['status'] = '' # reset user status return full_name = name + '_by_' + bot.username sticker = StickerFile(update.message.sticker or update.message.document, caption=update.message.caption) sticker.download(prepare_png=True) try: logger.debug('executing API request...') bot.create_new_sticker_set(user_id=update.effective_user.id, title=title, name=full_name, emojis=sticker.emoji, png_sticker=sticker.png_bytes_object) except (BadRequest, TelegramError) as e: logger.error('Telegram error while creating stickers pack: %s', e.message) error_code = u.get_exception_code(e.message) if error_code == 10: # there's already a pack with that link update.message.reply_html( s.PACK_CREATION_ERROR_DUPLICATE_NAME.format( u.name2link(full_name))) user_data['pack'].pop('name', None) # remove pack name user_data['status'] = 'waiting_pack_name' elif error_code == 13: update.message.reply_text(s.PACK_CREATION_ERROR_INVALID_NAME) user_data['pack'].pop('name', None) # remove pack name user_data['status'] = 'waiting_pack_name' else: update.message.reply_html( s.PACK_CREATION_ERROR_GENERIC.format(e.message)) return # do not continue db.save_pack(update.effective_user.id, full_name, title) pack_link = u.name2link(full_name) update.message.reply_html(s.PACK_CREATION_PACK_CREATED.format(pack_link)) sticker.delete() # remove sticker files user_data['status'] = 'adding_stickers' # wait for other stickers user_data['pack']['name'] = full_name
def on_addpack_name_receive(bot, update, user_data): logger.info('%d: received possible addpack name', update.effective_user.id) addpack_name = update.message.text if "https://t.me/addstickers/" in addpack_name: addpack_name = addpack_name.split("https://t.me/addstickers/", 1)[1] if "_by_" + bot.username not in addpack_name: update.message.reply_text(s.PACK_NAME_NOT_OURS_ERROR) user_data['status'] = 'waiting_addpack_name' return elif addpack_name is not None: addpack_name = addpack_name + '_by_' + bot.username else: update.message.reply_text(s.PACK_NAME_NOT_OURS_ERROR) user_data['status'] = 'waiting_addpack_name' return if len(addpack_name) > 64: logger.info('Not a valid pack name too long: %s', addpack_name) update.message.reply_text(s.PACK_TITLE_TOO_LONG) # do not change the user status and let him send another title return if '\n' in addpack_name: logger.info('pack title contains newline character') update.message.reply_text(s.PACK_TITLE_CONTAINS_NEWLINES) # do not change the user status and let him send another title return logger.info('pack title is valid') # get sticker set try: addpack_name_stickerset = bot.getStickerSet(addpack_name) except (BadRequest, TelegramError) as e: logger.error('Telegram error while trying to get the sticker set: %s', e.message) error_code = u.get_exception_code(e.message) if error_code == 13: update.message.reply_text(s.PACK_CREATION_ERROR_INVALID_NAME) user_data['status'] = 'waiting_addpack_name' else: update.message.reply_html( s.PACK_CREATION_ERROR_GENERIC.format(e.message)) return # do not continue db.save_pack(update.effective_user.id, addpack_name_stickerset.name, addpack_name_stickerset.title) pack_link = u.name2link(addpack_name_stickerset.name) update.message.reply_html(s.PACK_ADDED.format(pack_link)) user_data['status'] = ''
def remove_from_set(self, bot): logger.debug('removing sticker from set %s', self._file.set_name) try: bot.delete_sticker_from_set(self._file.file_id) return 0 except (BadRequest, TelegramError) as e: logger.error( 'Telegram exception while trying to remove a sticker from %s: %s', self._file.set_name, e.message) error_code = u.get_exception_code(e) if error_code == 0: # unknown error return e.message return error_code
def add_to_set(self, bot, user_id, pack_name): logger.debug('adding sticker to set %s', pack_name) try: bot.add_sticker_to_set(user_id=user_id, name=pack_name, emojis=self._emoji, png_sticker=self.png_bytes_object, mask_position=None) return 0 except (BadRequest, TelegramError) as e: logger.error( 'Telegram exception while trying to add a sticker to %s: %s', pack_name, e.message) error_code = u.get_exception_code(e) if error_code == 0: # unknown error return e.message return error_code