def takeout_search_result_confirm(bot, update, agent, chat, botuser): search_results_response = update.message.text if search_results_response == msg("takeout_search_results_notfound"): return msg("takeout_ask_search"), Chat.TAKEOUT_SEARCH_RESULTS selected_book = quick_search(search_results_response, Book).first() reply = msg("takeout_confirm_takeout").format(book=selected_book.name) return ( ( [reply], { "reply_markup": ReplyKeyboardMarkup( [[ msg("takeout_confirm_takeout_Yes"), msg("takeout_confirm_takeout_No"), ]], one_time_keyboard=True, ) }, ), Chat.TAKEOUT_CONFIRM_TAKEOUT, { "selected_book_id": selected_book.id }, )
def borrow_confirm_return(bot, update, agent, chat, botuser): text = update.message.text book_id = chat.get_meta()["selected_book_id"] book = Book.objects.filter(id=book_id).first() if text == msg("borrow_confirm_book_return_Yes"): book.status = Book.AVAILABLE book.current_user = None book.save() return msg("borrow_confirm_book_return_success"), ConversationHandler.END return msg("borrow_confirm_book_return_userexit"), ConversationHandler.END
def takeout_confirm_takeout(bot, update, agent, chat, botuser): text = update.message.text book_id = chat.get_meta()["selected_book_id"] book = Book.objects.filter(id=book_id).first() if book is None: return msg("error_general"), ConversationHandler.END if text == msg("takeout_confirm_takeout_Yes"): book.status = Book.TAKEN_OUT book.current_user = botuser book.save() return msg("takeout_confirm_takeout_success"), ConversationHandler.END return msg("takeout_confirm_takeout_userexit"), ConversationHandler.END
def borrow_confirm_borrow(bot, update, agent, chat, botuser): text = update.message.text book_id = chat.get_meta()["selected_book_id"] book = Book.objects.filter(id=book_id).first() if book is None: return msg("error_general"), ConversationHandler.END if text == msg("borrow_confirm_book_available_Yes"): book.status = Book.IN_USE book.current_user = botuser book.save() host_url = msg("mention_user").format(telegram=book.host.telegram) reply = msg("borrow_confirm_borrow_success").format(host_url=host_url) return reply, ConversationHandler.END return msg("borrow_confirm_borrow_userexit"), ConversationHandler.END
def regbook_end(bot, update, agent, chat, botuser): book_meta = chat.get_meta() isbn = update.message.text book_meta.update({"book_isbn": isbn}) reply = msg("regbook_summary").format(**book_meta) author = quick_search(book_meta["book_author"], BookAuthor).first() if author is None: author = BookAuthor.objects.create(name=book_meta["book_author"]) language = quick_search(book_meta["book_language"], BookLanguage).first() if language is None: language = BookLanguage.objects.create(name=book_meta["book_language"]) if "me" in book_meta["book_hostname"]: host = botuser else: host = quick_search(book_meta["book_hostname"], BotUser, "email").first() if host is None: host = botuser Book.objects.create( name=book_meta["book_name"], author=author, language=language, host=host, image=ImageUpload.objects.get(id=book_meta["book_photo_id"]), isbn=book_meta["book_isbn"], ) return reply, Chat.MAINMENU
def takeout_search_results(bot, update, agent, chat, botuser): search_qry = update.message.text results = Book.objects.filter(name__icontains=search_qry, host=botuser) reply = msg("takeout_search_results") return ( ( [reply], # args { "reply_markup": ReplyKeyboardMarkup( [[str(item)] for item in results] + [[msg("takeout_search_results_notfound")]], one_time_keyboard=True, ) }, # kwargs ), Chat.TAKEOUT_SEARCH_RESULT_CONFIRM, )
def borrow_search_result_confirm(bot, update, agent, chat, botuser): search_results_response = update.message.text if search_results_response == msg("borrow_search_results_notfound"): return msg("borrow_ask_search"), Chat.BORROW_SEARCH_RESULTS selected_book = quick_search(search_results_response, Book).first() if selected_book.status == Book.AVAILABLE: reply = msg("borrow_confirm_book_available").format(book=selected_book.name) return ( ( [reply], { "reply_markup": ReplyKeyboardMarkup( [ [ msg("borrow_confirm_book_available_Yes"), msg("borrow_confirm_book_available_No"), ] ], one_time_keyboard=True, ) }, ), Chat.BORROW_CONFIRM_BORROW, {"selected_book_id": selected_book.id}, ) if selected_book.current_user == botuser: reply = msg("borrow_confirm_book_return").format(book=selected_book.name) return ( ( [reply], { "reply_markup": ReplyKeyboardMarkup( [ [ msg("borrow_confirm_book_return_Yes"), msg("borrow_confirm_book_return_No"), ] ], one_time_keyboard=True, ) }, ), Chat.BORROW_CONFIRM_RETURN, {"selected_book_id": selected_book.id}, ) reply = msg("borrow_confirm_book_inuse").format( book=selected_book.name, current_user_tg_id=selected_book.current_user.telegram ) return reply, ConversationHandler.END
def borrow_search_results(bot, update, agent, chat, botuser): search_qry = update.message.text results = Book.objects.filter( name__icontains=search_qry, status__in=[Book.AVAILABLE, Book.IN_USE] ) reply = msg("borrow_search_results") return ( ( [reply], # args { "reply_markup": ReplyKeyboardMarkup( [[str(item)] for item in results] + [[msg("borrow_search_results_notfound")]], one_time_keyboard=True, ) }, # kwargs ), Chat.BORROW_SEARCH_RESULT_CONFIRM, )
def reguser_end(bot, update, agent, chat, botuser): text = update.message.text if botuser is None: botuser = BotUser.objects.create(telegram=agent, email=text) else: botuser.email = text botuser.save() reply = msg("reguser_end_success").format( agent=agent, email=text, user_id=botuser.pk ) return reply, Chat.MAINMENU
def crossroad(bot, update): logger.debug("/crossroad") agent, chat, botuser = get_update_context(update, state=Chat.MAINMENU) text = update.message.text logger.debug(config().get_handlers()) for roadkey, (road_state, road) in config().get_handlers().items(): if roadkey == text: chat.state = road_state chat.save() return road(bot, update) update.message.reply_text(msg("unrecognized_option_crossroad")) return ConversationHandler.END
def start(bot, update): logger.debug("/start @{}".format(update.message.from_user.username)) agent = update.message.from_user.id # removing all previous chats with this agent Chat.objects.filter(agent=agent).delete() # starting chat chat = Chat.objects.create(agent=agent) chat.state = Chat.MAINMENU chat.save() botuser = BotUser.objects.filter(telegram=agent).first() if botuser is None: botuser = BotUser.objects.create(telegram=agent) update.message.reply_text( msg("mainmenu"), reply_markup=ReplyKeyboardMarkup(route_config().get_keyboard(), one_time_keyboard=True), ) return Chat.MAINMENU
text = update.message.text book_id = chat.get_meta()["selected_book_id"] book = Book.objects.filter(id=book_id).first() if book is None: return msg("error_general"), ConversationHandler.END if text == msg("takeout_confirm_takeout_Yes"): book.status = Book.TAKEN_OUT book.current_user = botuser book.save() return msg("takeout_confirm_takeout_success"), ConversationHandler.END return msg("takeout_confirm_takeout_userexit"), ConversationHandler.END state_map = { "__meta__": { "caption": msg("mainmenu_takeout_book_caption"), "entry_state": Chat.TAKEOUT_ASK_SEARCH, "entry_point": takeout_ask_search, }, "states": { Chat.TAKEOUT_ASK_SEARCH: [MessageHandler(Filters.text, takeout_ask_search)], Chat.TAKEOUT_SEARCH_RESULTS: [MessageHandler(Filters.text, takeout_search_results)], Chat.TAKEOUT_SEARCH_RESULT_CONFIRM: [MessageHandler(Filters.text, takeout_search_result_confirm)], Chat.TAKEOUT_CONFIRM_TAKEOUT: [MessageHandler(Filters.text, takeout_confirm_takeout)], }, }
def regbook_ask_book_language(bot, update, agent, chat, botuser): reply = msg("regbook_ask_book_language") return reply, Chat.REGBOOK_ASK_HOSTNAME, { "book_author": update.message.text }
def takeout_ask_search(bot, update, agent, chat, botuser): reply = msg("takeout_ask_search") return reply, Chat.TAKEOUT_SEARCH_RESULTS
def borrow_ask_search(bot, update, agent, chat, botuser): reply = msg("borrow_ask_search") return reply, Chat.BORROW_SEARCH_RESULTS
host = botuser Book.objects.create( name=book_meta["book_name"], author=author, language=language, host=host, image=ImageUpload.objects.get(id=book_meta["book_photo_id"]), isbn=book_meta["book_isbn"], ) return reply, Chat.MAINMENU state_map = { "__meta__": { "caption": msg("mainmenu_regbook_caption"), "entry_state": Chat.REGBOOK_ASK_INFO, "entry_point": regbook_ask_book_name, }, "states": { Chat.REGBOOK_ASK_INFO: [MessageHandler(Filters.text, regbook_ask_book_name)], Chat.REGBOOK_ASK_AUTHOR: [MessageHandler(Filters.text, regbook_ask_book_author)], Chat.REGBOOK_ASK_LANGUAGE: [MessageHandler(Filters.text, regbook_ask_book_language)], Chat.REGBOOK_ASK_HOSTNAME: [MessageHandler(Filters.text, regbook_ask_book_hostname)], Chat.REGBOOK_ASK_PHOTO: [MessageHandler(Filters.text, regbook_ask_book_photo)], Chat.REGBOOK_ASK_ISBN:
@tg_handler(state=Chat.BORROW_CONFIRM_RETURN) def borrow_confirm_return(bot, update, agent, chat, botuser): text = update.message.text book_id = chat.get_meta()["selected_book_id"] book = Book.objects.filter(id=book_id).first() if text == msg("borrow_confirm_book_return_Yes"): book.status = Book.AVAILABLE book.current_user = None book.save() return msg("borrow_confirm_book_return_success"), ConversationHandler.END return msg("borrow_confirm_book_return_userexit"), ConversationHandler.END state_map = { "__meta__": { "caption": msg("mainmenu_borrow_book_caption"), "entry_state": Chat.BORROW_ASK_SEARCH, "entry_point": borrow_ask_search, }, "states": { Chat.BORROW_ASK_SEARCH: [MessageHandler(Filters.text, borrow_ask_search)], Chat.BORROW_SEARCH_RESULTS: [ MessageHandler(Filters.text, borrow_search_results) ], Chat.BORROW_SEARCH_RESULT_CONFIRM: [ MessageHandler(Filters.text, borrow_search_result_confirm) ], Chat.BORROW_CONFIRM_BORROW: [ MessageHandler(Filters.text, borrow_confirm_borrow) ], Chat.BORROW_CONFIRM_RETURN: [
def regbook_ask_book_name(bot, update, agent, chat, botuser): reply = msg("regbook_ask_book_name") # enter book name return reply, Chat.REGBOOK_ASK_AUTHOR
def regbook_ask_book_isbn(bot, update, agent, chat, botuser): photo = bot.get_file(update.message.photo[-1].file_id) photo_id = _save_photo(photo) reply = msg("regbook_ask_book_isbn") return reply, Chat.REGBOOK_END, {"book_photo_id": photo_id}
def regbook_ask_book_photo(bot, update, agent, chat, botuser): reply = msg("regbook_ask_book_photo") return reply, Chat.REGBOOK_ASK_ISBN, {"book_hostname": update.message.text}
def reguser_ask_email(bot, update, agent, chat, botuser): reply = msg("reguser_ask_email") return reply, Chat.REGUSER_END
def regbook_ask_book_author(bot, update, agent, chat, botuser): reply = msg("regbook_ask_book_author") return reply, Chat.REGBOOK_ASK_LANGUAGE, {"book_name": update.message.text}
def regbook_ask_book_hostname(bot, update, agent, chat, botuser): reply = msg("regbook_ask_book_hostname") return reply, Chat.REGBOOK_ASK_PHOTO, { "book_language": update.message.text }
reply = msg("reguser_ask_email") return reply, Chat.REGUSER_END @tg_handler(state=Chat.REGUSER_END) def reguser_end(bot, update, agent, chat, botuser): text = update.message.text if botuser is None: botuser = BotUser.objects.create(telegram=agent, email=text) else: botuser.email = text botuser.save() reply = msg("reguser_end_success").format( agent=agent, email=text, user_id=botuser.pk ) return reply, Chat.MAINMENU state_map = { "__meta__": { "caption": msg("mainmenu_reguser_caption"), "entry_state": Chat.REGUSER_ASK_EMAIL, "entry_point": reguser_ask_email, }, "states": { Chat.REGUSER_ASK_EMAIL: [MessageHandler(Filters.text, reguser_ask_email)], Chat.REGUSER_END: [MessageHandler(Filters.text, reguser_end)], }, }