def favorite_query_handler(query: CallbackQuery): user_id = query.from_user.id user = users.get_user_by_telegram_id(user_id) def error(string_code): telegram_bot.answer_callback_query(query.id, strings.get_string(string_code), show_alert=True) if not user: error('catalog.add_favorite.not_allowed') return if not query.data.isnumeric(): error('catalog.add_favorite.error') return file_id = int(query.data) file = files.get_file_by_id(file_id) if not file: error('catalog.add_favorite.error') return if file in user.favorites_files.all(): user.favorites_files.remove(file) success_message = strings.get_string('catalog.add_favorite.removed') new_keyboard = keyboards.from_file_to_inline_keyboard_favorite(file) else: user.favorites_files.add(file) success_message = strings.get_string('catalog.add_favorite.added') new_keyboard = keyboards.from_file_to_inline_keyboard_favorite( file, remove=True) telegram_bot.answer_callback_query(query.id, success_message) telegram_bot.edit_message_reply_markup(user_id, query.message.message_id, query.inline_message_id, new_keyboard)
def file_handler(message: Message): user_id = message.from_user.id user = users.get_user_by_telegram_id(user_id) def error(): telegram_bot.send_message(user_id, strings.get_string('catalog.files.select')) telegram_bot.register_next_step_handler_by_chat_id( user_id, file_handler) if not message.text: error() return if Helpers.check_for_start_command(message): Navigation.to_main_menu(user_id) return if strings.get_string('back') in message.text: Navigation.to_search(user_id) else: file = files.get_file_by_name(message.text) if not file: error() return Helpers.send_file(user_id, file, user) telegram_bot.register_next_step_handler_by_chat_id( user_id, file_handler)
def file_handler(message: Message, *args, **kwargs): user_id = message.from_user.id current_category = kwargs.get('current_category') user = users.get_user_by_telegram_id(user_id) def error(): telegram_bot.send_message(user_id, strings.get_string('catalog.files.select')) telegram_bot.register_next_step_handler_by_chat_id( user_id, file_handler, current_category=current_category) if not message.text: error() return if strings.get_string('back') in message.text: if 'from_users' in kwargs: _go_back(user_id, current_category, from_users=True) else: _go_back(user_id, current_category) else: file = files.get_file_by_name(message.text) if not file: error() return Helpers.send_file(user_id, file, user) telegram_bot.register_next_step_handler_by_chat_id( user_id, file_handler, current_category=current_category)
def user_file_handler(message: Message): user_telegram_file = message.audio file_size_mb = user_telegram_file.file_size / 5120**2 if file_size_mb > 1: too_much_size_message = strings.get_string('user_files.too_much_size') telegram_bot.reply_to(message, too_much_size_message) else: try: wait_message = strings.get_string('user_files.wait') telegram_bot.reply_to(message, wait_message) telegram_file_info = telegram_bot.get_file( user_telegram_file.file_id) telegram_file_path = telegram_file_info.file_path file_caption = 'audio_' + secrets.token_hex(5) telegram_file = requests.get( 'https://api.telegram.org/file/bot{0}/{1}'.format( API_TOKEN, telegram_file_path)) file_storage = FileSystemStorage() filename = 'users/' + file_caption extension = os.path.splitext( os.path.basename(telegram_file_path))[1] if os.path.exists( os.path.join(file_storage.location, filename + extension)): filename += secrets.token_hex(5) filename += extension filepath = os.path.join(file_storage.location, filename) open(filepath, 'wb').write(telegram_file.content) user = users.get_user_by_telegram_id(message.from_user.id) new_file = File.objects.create( name=file_caption, file_path=file_storage.path(filename), file_url=file_storage.url(filename), is_user_file=True, confirmed=False, caption='@send_sound_bot', user=user) type_filename_message = strings.get_string( 'user_files.type_file_name') remove_keyboard = keyboards.get_keyboard('remove') telegram_bot.send_message(message.chat.id, type_filename_message, reply_markup=remove_keyboard) telegram_bot.register_next_step_handler(message, file_name_handler, file_id=new_file.id) except Exception as e: error_message = strings.get_string('user_files.error') Navigation.to_main_menu(message.chat.id, error_message) logging.error(e)
def favorites_handler(message: Message): user_id = message.from_user.id user = users.get_user_by_telegram_id(user_id) user_files = user.favorites_files.all() if not user_files: empty_message = strings.get_string('favorites.empty') telegram_bot.send_message(user_id, empty_message) return select_message = strings.get_string('catalog.files.select') files_keyboard = keyboards.from_files_list_to_keyboard(user_files) telegram_bot.send_message(user_id, select_message, reply_markup=files_keyboard) telegram_bot.register_next_step_handler_by_chat_id(user_id, file_handler)
def _auth(message: Message): user_id = message.from_user.id user = users.get_user_by_telegram_id(user_id) return user