Beispiel #1
0
def command_handler(bot, update, user_data):
    global ANSWERS
    answer = ANSWERS[update.message.text][0]
    if update.message.text == '/save_my_book':
        genre_dict = get_genre_dict()
        genres = []
        for genre in user_data.get('genre'):
            genres.append(genre_dict[genre])
        user_data['genre'] = genres
        save_the_book(
            user_data.get('name'),
            User.get_id_by_telegram_name(update.message.chat.username),
            user_data.get('text_from_file'), user_data.get('description'),
            user_data.get('chapter_name'), user_data.get('genre'))
    elif update.message.text == '/choose_genres':
        genre_dict = get_genre_dict()
        for line in genre_dict.keys():
            answer += line
            answer += ', '
    elif update.message.text == '/by_author':
        authors_id_list = Author.get_authors_with_books()
        for author_id in authors_id_list:
            answer += ' ' + User.query.filter(
                User.id == author_id).first().user_name
    elif update.message.text == '/by_genre':
        for line in Genre.get_parents():
            answer += line.genre_name + ' '
    elif update.message.text == '/start':
        if User.get_id_by_telegram_name(update.message.chat.username) is None:
            answer = ANSWERS[update.message.text][2]
    update.message.reply_text(answer)
    return ANSWERS[update.message.text][1]
Beispiel #2
0
def start(bot, update):
    if User.get_id_by_telegram_name(update.message.chat.username) is not None:
        anser = '''
        Добрый день!
        Вы находитесь в библиотеке.
        Вы можете добавить новую книгу /add_a_book
        или выбрать книгу для чтения /read_a_book
        для возврата в главное меню /start

        '''
        mode = MAIN_MENU
    else:
        answer = '''
        Добрый день! Вы не зарегестрированны,
        вам доступен только просмотр бесплатных книг
        Для просмотра книг нажмите /read_a_book
        для возврата в главное меню /start

        '''
        mode = MAIN_MENU_UN_REGISTRED
    update.message.reply_text(answer)
    return mode
Beispiel #3
0
def get_book(bot, update, user_data):
    author_id = None
    if user_data.get('selected_autrhor'):
        author_id = user_data['selected_autrhor']
        book_name = update.message.text
    else:
        book_name, user_name = update.message.text.split('|', maxsplit=1)
        author_obj = User.query.filter(User.user_name == user_name).first()
        if author_obj:
            author_id = author_obj.id
    if author_id:
        my_book = Author.query.filter(Author.user_id == author_id).filter(
            Book.book_name == book_name).options(
                joinedload(Author.books_author)).first()
        if my_book:
            chat_id = update.message.chat.id
            user_id = User.get_id_by_telegram_name(
                update.message.chat.username)
            book_file = get_book_file(my_book.id, book_name, 'pdf', user_id)
            book_obj = BytesIO(book_file['file'])
            bot.send_document(chat_id=chat_id,
                              document=book_obj,
                              filename=book_file['file_name'])