def join_book(self, msg, arguments): open_books = DBSession.query(BookAssignment) \ .filter(BookAssignment.chat_id == msg.chat.id) \ .filter(BookAssignment.current == True).all() user = User.create_or_get(msg.sender) current_books = [participation.book_assignment_id for participation in user.active_participation(msg.chat.id)] open_books = [book for book in open_books if book.id not in current_books] if len(open_books) == 0: self.bot.send_message(chat_id=msg.chat.id, text="No books are currently being read that you're not in!", reply_to_message_id=msg.message_id) elif len(open_books) == 1: self._join_book(assignment=open_books[0], user_id=msg.sender.id) self.bot.send_message(chat_id=msg.chat.id, text=f"@{msg.sender.username} joined book {open_books[0].book.friendly_name}!", reply_to_message_id=msg.message_id) else: reply = "Which book do you want to join?" keyboard_rows = [] for book_assign in open_books: keyboard_rows.append([botapi.InlineKeyboardButton(text=book_assign.book.friendly_name, callback_data=str(book_assign.book.id))]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) query = self.bot.send_message(chat_id=msg.chat.id, text=reply, reply_markup=keyboard, reply_to_message_id=msg.message_id).join().result self.update_loop.register_inline_reply(message=query, srcmsg=msg, function=self.join_book__select_book, permission=Permission.SameUser)
def get_book__info_type(self, msg, cbquery, data): book = DBSession.query(BookAssignment).filter(BookAssignment.id == data).first() reply = f"What info would you like for {book.book.friendly_name}?" buttons = [] if book.book.goodreads_id: buttons.append("Description") if book.ebook_message_id: buttons.append("eBook") if book.audiobook_message_id: buttons.append("Audiobook") if len(buttons) == 0: reply = f"Current book is {book.book.friendly_name}" keyboard = None else: keyboard_rows = [] for button in buttons: keyboard_rows.append([botapi.InlineKeyboardButton(text=button, callback_data=str(button))]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) if cbquery is not None: query = self.bot.edit_message_text(chat_id=msg.chat.id, text=reply, reply_markup=keyboard, message_id=cbquery.message.message_id).join().result else: query = self.bot.send_message(chat_id=msg.chat.id, text=reply, reply_markup=keyboard).join().result self.update_loop.register_inline_reply(message=query, srcmsg=msg, function=partial(self.get_book__select_info_type, data), permission=Permission.SameUser)
def start_book(self, msg, arguments): open_books = DBSession.query(BookAssignment).filter(BookAssignment.chat_id == msg.chat.id).filter(BookAssignment.done == False).filter( BookAssignment.current == False).all() current_book = DBSession.query(BookAssignment).filter(BookAssignment.chat_id == msg.chat.id).filter(BookAssignment.current == True).all() if len(open_books) == 0: self.bot.send_message(chat_id=msg.chat.id, text="There are no open books to start.", reply_to_message_id=msg.message_id) else: reply = "" if len(current_book) > 0: reply += f"There are currently {len(current_book)} active books. \n" reply += "Which book do you want to set as active?" # TODO: Support a "More" button. keyboard_rows = [] for book_assign in open_books: keyboard_rows.append([botapi.InlineKeyboardButton(text=book_assign.book.friendly_name, callback_data=str(book_assign.id))]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) query = self.bot.send_message(chat_id=msg.chat.id, text=reply, reply_markup=keyboard, reply_to_message_id=msg.message_id).join().result self.update_loop.register_inline_reply(message=query, srcmsg=msg, function=self.start_book__select_book, permission=Permission.SameUser)
def get_deadline(self, msg, arguments): current_books = DBSession.query(BookAssignment).filter(BookAssignment.chat_id == msg.chat.id).filter(BookAssignment.current == True).all() if len(current_books) == 1: self._send_deadline(current_books[0].id) else: reply = "Which book do you want deadline for?" keyboard_rows = [] for book_assign in current_books: keyboard_rows.append([botapi.InlineKeyboardButton(text=book_assign.book.friendly_name, callback_data=str(book_assign.id))]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) query = self.bot.send_message(chat_id=msg.chat.id, text=reply, reply_markup=keyboard, reply_to_message_id=msg.message_id).join().result self.update_loop.register_inline_reply(message=query, srcmsg=msg, function=self.get_deadline__select_book, permission=Permission.SameUser)
def set_progress(self, msg, arguments): try: progress = int(arguments) except (ValueError, TypeError): progress = None user = User.create_or_get(msg.sender) joined_books = user.active_participation(chat_id=msg.chat.id) if len(joined_books) == 0: self.bot.send_message(chat_id=msg.chat.id, text="You are not currently reading any books!", reply_to_message_id=msg.message_id) elif len(joined_books) == 1: book = joined_books[0].book if progress is not None: try: user = f"@{msg.sender.username}" or f"{msg.sender.first_name} {msg.sender.last_name}" self._set_progress(joined_books[0].id, progress) self.bot.send_message(chat_id=msg.chat.id, text=f"{user} progress set for {book.title} through {progress}!") except sqlalchemy.exc.DataError: self.bot.send_message(chat_id=msg.chat.id, text=f"Error setting progress set for {book.friendly_name}, number may be too large or invalid.", reply_to_message_id=msg.message_id) else: query = self.bot.send_message(chat_id=msg.chat.id, text="How far have you read?", reply_markup=botapi.ForceReply.create(selective=True), reply_to_message_id=msg.message_id).join().result self.update_loop.register_reply_watch(message=query, function=partial(self.set_progress__ask_progress, joined_books[0].id)) else: reply = "Which book do you want to set progress on?" keyboard_rows = [] for participation in joined_books: keyboard_rows.append([botapi.InlineKeyboardButton(text=participation.book.friendly_name, callback_data=str(participation.id))]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) query = self.bot.send_message(chat_id=msg.chat.id, text=reply, reply_markup=keyboard).join().result self.update_loop.register_inline_reply(message=query, srcmsg=msg, function=partial(self.set_progress__select_book, msg.message_id, progress), permission=Permission.SameUser)
def register_ebook(self, msg, arguments): open_books = DBSession.query(BookAssignment).filter(BookAssignment.chat_id == msg.chat.id).filter(BookAssignment.done == False).all() if len(open_books) == 0: self.bot.send_message(chat_id=msg.chat.id, text="There are no open books.", reply_to_message_id=msg.message_id) elif len(open_books) == 1: query = self.bot.send_message(chat_id=msg.chat.id, text="Gimmie that ebook.", reply_markup=botapi.ForceReply.create(selective=True), reply_to_message_id=msg.message_id).join().result self.update_loop.register_reply_watch(message=query, function=partial(self.register_ebook__file, open_books[0].id)) else: # TODO: Support a "More" button. keyboard_rows = [] for book_assign in open_books: keyboard_rows.append([botapi.InlineKeyboardButton(text=book_assign.book.friendly_name, callback_data=str(book_assign.book.id))]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) query = self.bot.send_message(chat_id=msg.chat.id, text="Which book do you want to register an ebook for?", reply_markup=keyboard, reply_to_message_id=msg.message_id).join().result self.update_loop.register_inline_reply(message=query, srcmsg=msg, function=partial(self.register_ebook__select_book, msg.message_id), permission=Permission.SameUser)
def quit_book(self, msg, arguments): user = User.create_or_get(msg.sender) if msg.chat.type == "private": books = user.active_participation() else: books = user.active_participation(msg.chat.id) if len(books) == 0: self.bot.send_message(chat_id=msg.chat.id, text="You are not reading any books.", reply_to_message_id=msg.message_id) else: reply = "Which book do you want to quit?" keyboard_rows = [] for participation in books: keyboard_rows.append([botapi.InlineKeyboardButton(text=participation.book.friendly_name, callback_data=str(participation.id))]) keyboard_rows.append([botapi.InlineKeyboardButton(text="Cancel", callback_data="CANCEL")]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) query = self.bot.send_message(chat_id=msg.chat.id, text=reply, reply_markup=keyboard, reply_to_message_id=msg.message_id).join().result self.update_loop.register_inline_reply(message=query, srcmsg=msg, function=self.quit_book__select_book, permission=Permission.SameUser)
def add_book__set_goodreads(self, original_msg, search): # Set typing action for goodreads search self.bot.send_chat_action(chat_id=original_msg.chat.id, action="typing") possible_books = self.goodreads.search_books(search) # TODO: Support a "More" button. keyboard_rows = [] for book in possible_books[:5]: title = book['best_book']['title'] author = book['best_book']['author']['name'] try: year = book['original_publication_year']['#text'] except KeyError: year = "Unk" keyboard_rows.append([botapi.InlineKeyboardButton(text=f"{title[:30]+(title[30:] and '..')} - {author} ({year})", callback_data=f"GID:{book['best_book']['id']['#text']}")]) keyboard_rows.append([botapi.InlineKeyboardButton(text=f"As Entered: {search}", callback_data=original_msg.text), botapi.InlineKeyboardButton(text=f"Cancel", callback_data="CANCEL")]) keyboard = botapi.InlineKeyboardMarkup(inline_keyboard=keyboard_rows) query = self.bot.send_message(chat_id=original_msg.chat.id, text="Please select book to add. (search results via GoodReads)", reply_markup=keyboard).join().result self.update_loop.register_inline_reply(message=query, srcmsg=original_msg, function=partial(self.add_book__select_goodreads, original_msg), permission=Permission.SameUser)