def command_send_chapter(chat_message): ''' TODO: docstring ''' user_id = chat_message.chat.id connection, cursor = Database_control.create_db() book = Database_control.get_book(cursor, user_id) origin_language = Database_control.get_language(cursor, user_id) chapter = Database_control.get_chapter(cursor, user_id) bible_version = Database_control.get_bible_version(cursor, user_id) language = Database_control.get_language(cursor, user_id) verify_id = Database_control.verify_id(cursor, user_id) if verify_id: send_translated_message(user_id, "Sending chapter ...", language) message = " ".join([book, str(chapter)]) try: if origin_language in lang_ver_transl: n_message = message response = Bible.get_message(n_message, bible_version) else: n_message = Bible.translate_message(message, language="English", src=origin_language) response = Bible.get_message(n_message, bible_version) response = Bible.translate_message(response, origin_language) except (Exception) as exception: response = "Error - please retry" log.critical("Send chapter page:", exception) send_translated_message(user_id, n_message, language) response = [i for i in telebot.util.split_string(response, 3000)] for each_message in response: send_translated_message(user_id, each_message, language) next_chapter = Bible.get_next_chapter(message) next_book = next_chapter.split(" ")[0] next_chapter = next_chapter.split(" ")[1] Database_control.set_book(connection, cursor, next_book, user_id) Database_control.set_chapter(connection, cursor, next_chapter, user_id) Database_control.set_mod_default(connection, cursor, user_id) Database_control.close_db(connection) show_mainkeyboard(chat_message) else: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") Database_control.close_db(connection) command_start(chat_message)
def command_subscribe(chat_message): ''' TODO: docstring ''' user_id = chat_message.chat.id connection, cursor = Database_control.create_db() current_status = Database_control.get_status(cursor, user_id) verify_id = Database_control.verify_id(cursor, user_id) if verify_id: if current_status == 1: new_status = 0 else: new_status = 1 Database_control.set_status(connection, cursor, new_status, user_id) language = Database_control.get_language(cursor, user_id) Database_control.close_db(connection) if new_status == 1: status = '✅' else: status = '❌' text = "Subscription: \t" + status send_translated_message(user_id, text, language) # send the generated text else: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") Database_control.close_db(connection) command_start(chat_message)
def command_send_chapter_crontab(user_id): ''' TODO: docstring ''' connection, cursor = Database_control.create_db() book = Database_control.get_book(cursor, user_id) origin_language = Database_control.get_language(cursor, user_id) chapter = Database_control.get_chapter(cursor, user_id) bible_version = Database_control.get_bible_version(cursor, user_id) language = Database_control.get_language(cursor, user_id) send_translated_message(user_id, "Sending daily chapter", language) message = " ".join([book, str(chapter)]) try: if origin_language in lang_ver_transl: n_message = message response = Bible.get_message(n_message, bible_version) else: n_message = Bible.translate_message(message, language="English", src=origin_language) response = Bible.get_message(n_message, bible_version) response = Bible.translate_message(response, origin_language) except (Exception) as exception: response = "Error - please retry" log.critical("In send command_send_chapter_crontab - Exception: ", exception) response = [i for i in telebot.util.split_string(response, 3000)] send_translated_message(user_id, n_message, language) for each_message in response: send_translated_message(user_id, each_message, language) next_chapter = Bible.get_next_chapter(message) next_book = next_chapter.split(" ")[0] next_chapter = next_chapter.split(" ")[1] Database_control.set_book(connection, cursor, next_book, user_id) Database_control.set_chapter(connection, cursor, next_chapter, user_id) Database_control.set_mod_default(connection, cursor, user_id) Database_control.close_db(connection)
def command_language(chat_message): ''' TODO: docstring ''' command_hide(chat_message) user_id = chat_message.chat.id connection, cursor = Database_control.create_db() Database_control.set_mod_language(connection, cursor, user_id) language = Database_control.get_language(cursor, user_id) verify_id = Database_control.verify_id(cursor, user_id) Database_control.close_db(connection) if verify_id: send_translated_message(user_id, "Type your new language", language) else: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") command_start(chat_message)
def command_information(chat_message): ''' TODO: docstring ''' user_id = chat_message.chat.id connection, cursor = Database_control.create_db() status = Database_control.get_status(cursor, user_id) language = Database_control.get_language(cursor, user_id) book = Database_control.get_book(cursor, user_id) chapter = Database_control.get_chapter(cursor, user_id) bible_version = Database_control.get_bible_version(cursor, user_id) verify_id = Database_control.verify_id(cursor, user_id) Database_control.close_db(connection) if verify_id: # Full version name (no acronym) try: bible_version = dict_api_acr2ver[bible_version] except: bible_version = "akjv" if status == 1: status = '✅' else: status = '❌' info_text = "📒 Information \n📚 Subscribed: \t" + status + "\n🌎 Language: \t" + str( language) + "\n📖 Current Bible Book: \t" + str( book) + "\n📑 Current Chapter: \t" + str( chapter) + "\n📕 Current Bible Version: \t" + str( bible_version) send_translated_message(chat_message.chat.id, info_text, language) # Send info text show_mainkeyboard(chat_message) else: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") command_start(chat_message)
def command_bible_version(chat_message): ''' TODO: docstring ''' command_hide(chat_message) user_id = chat_message.chat.id connection, cursor = Database_control.create_db() Database_control.set_mod_bible_version(connection, cursor, user_id) language = Database_control.get_language(cursor, user_id) verify_id = Database_control.verify_id(cursor, user_id) if verify_id: if language in dict_api_version: versions = dict_api_version[language] + ( dict_api_version["English"]) else: versions = dict_api_version["English"] version_markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False) for each in range(0, len(versions)): version_markup.row(versions[each]) Database_control.set_mod_default(connection, cursor, user_id) Database_control.set_mod_bible_version(connection, cursor, user_id) Database_control.close_db(connection) bot.send_message(chat_message.from_user.id, "⌨️", reply_markup=version_markup) else: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") Database_control.close_db(connection) command_start(chat_message)
def command_help(chat_message): ''' TODO: docstring ''' user_id = chat_message.chat.id connection, cursor = Database_control.create_db() language = Database_control.get_language(cursor, user_id) verify_id = Database_control.verify_id(cursor, user_id) Database_control.close_db(connection) if verify_id: help_text = "Avaliable commands: \n\n" for key in commands: # generate help text out of the commands dictionary defined at the top help_text += "/" + key + ": " + commands[key] + "\n" # send the generated help page send_translated_message(user_id, help_text, language) show_mainkeyboard(chat_message) else: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") command_start(chat_message)
def command_verse(chat_message): ''' TODO: docstring ''' command_hide(chat_message) user_id = chat_message.chat.id connection, cursor = Database_control.create_db() Database_control.set_verse(connection, cursor, user_id) language = Database_control.get_language(cursor, user_id) verify_id = Database_control.verify_id(cursor, user_id) Database_control.close_db(connection) if verify_id: send_translated_message(user_id, "Type the desired passage", language) Example = "Examples: \nJohn 14:6 \nGenesis 2:1-4 \nLuke 3" send_translated_message(user_id, Example, language) else: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") command_start(chat_message)
def command_start(chat_message): ''' TODO: docstring ''' user_id = chat_message.chat.id name = chat_message.chat.first_name connection, cursor = Database_control.create_db() verify_id = Database_control.verify_id(cursor, user_id) if verify_id == False: Database_control.add_user(connection, cursor, user_id, name) language = Database_control.get_language(cursor, user_id) Database_control.close_db(connection) start_text = """☝ This bot is a free service for educational purposes \nThis bot 🤖 can: \n\t1. Send you daily 1 chapter of the Bible in a sequence ('/subscribe') \n\t2. If you are not subscribed or want to advance more, you can manually request the chapter ('/send_chapter'). \n\t3.❓ Also you can ask for any verse or passage ('/verse') \n\nGo to help ('/help') for more information""" send_translated_message(chat_message.chat.id, start_text, language) # Send start text show_mainkeyboard(chat_message)
def command_default(chat_message): ''' TODO: docstring TODO - put all in a tuple instead single queries - optimize db queries ''' user_id = chat_message.chat.id connection, cursor = Database_control.create_db() if Database_control.verify_id(cursor, user_id) == False: send_translated_message( chat_message.chat.id, "You are a new user. Check what I can do at /start") Database_control.close_db(connection) command_start(chat_message) else: lang_selection = Database_control.get_mod_language(cursor, user_id) bible_book_selection = Database_control.get_mod_book(cursor, user_id) chapter_selection = Database_control.get_mod_chapter(cursor, user_id) bible_version_selection = Database_control.get_mod_bible_version( cursor, user_id) verse = Database_control.get_verse(cursor, user_id) origin_language = Database_control.get_language(cursor, user_id) bible_version = Database_control.get_bible_version(cursor, user_id) if lang_selection == True: language = verify_language(chat_message.text) Database_control.set_language(connection, cursor, language, user_id) Database_control.set_mod_language(connection, cursor, user_id) if language in dict_api_version: Database_control.set_bible_version( connection, cursor, dict_api_ver2acr[dict_api_version[language][0]], user_id) else: Database_control.set_bible_version(connection, cursor, 'akjv', user_id) send_translated_message(chat_message.chat.id, "The selected language is " + language, origin_language) Database_control.set_mod_default(connection, cursor, user_id) Database_control.close_db(connection) show_mainkeyboard(chat_message) elif verse == True: send_translated_message(user_id, "Accepted request - Searching ...", origin_language) message = chat_message.text try: if origin_language in lang_ver_transl: if origin_language != 'English': n_message = Bible.translate_message( message, language="English", src=origin_language) else: n_message = message if n_message.split(":")[-1][0] == " ": n_message = "".join([ n_message.split(":")[:-1][0], ":", "".join(n_message.split(":")[-1][1:].split(" ")) ]) response = Bible.get_message(n_message, bible_version) else: n_message = Bible.translate_message(message, language="English", src=origin_language) if n_message.split(":")[-1][0] == " ": n_message = "".join([ n_message.split(":")[:-1][0], ":", "".join(n_message.split(":")[-1][1:].split(" ")) ]) response = Bible.get_message(n_message, bible_version) response = Bible.translate_message(response, origin_language) Database_control.set_verse(connection, cursor, user_id) except (Exception) as exception: log.critical("In message handler - Exception: ", exception) response = [i for i in telebot.util.split_string(response, 3000)] for each_message in response: send_translated_message(user_id, each_message, origin_language) Database_control.set_mod_default(connection, cursor, user_id) Database_control.close_db(connection) show_mainkeyboard(chat_message) elif bible_book_selection == True: message = chat_message.text command_hide(chat_message) if origin_language in list(lang_ver_transl) + list(lang_transl): if origin_language != 'English': n_message = Bible.translate_message(message, language="English", src=origin_language) else: n_message = message book = Bible.verify_book(n_message) else: send_translated_message( chat_message.chat.id, "As your language is not available, please write in English", origin_language) n_message = message book = Bible.verify_book(n_message) Database_control.set_book(connection, cursor, book, user_id) Database_control.set_mod_book(connection, cursor, user_id) Database_control.set_mod_default(connection, cursor, user_id) send_translated_message(chat_message.chat.id, "The selected Book is " + book, origin_language) Database_control.close_db(connection) show_mainkeyboard(chat_message) elif chapter_selection == True: message = chat_message.text book = Database_control.get_book(cursor, user_id) if message.isnumeric(): if (Bible.verify_book_chapter(book, message) == True) and (int(message) >= 1): Database_control.set_chapter(connection, cursor, message, user_id) send_translated_message( chat_message.chat.id, "The selected chapter is " + message, origin_language) else: send_translated_message( chat_message.chat.id, "This chapter doesn't exist for the book of " + book + ". \nChapter 1 selected.", origin_language) message = "1" Database_control.set_chapter(connection, cursor, "1", user_id) else: message = "1" send_translated_message( chat_message.chat.id, "This is not a number. \nChapter 1 selected.", origin_language) Database_control.set_chapter(connection, cursor, "1", user_id) Database_control.set_mod_book(connection, cursor, user_id) Database_control.set_mod_default(connection, cursor, user_id) Database_control.close_db(connection) show_mainkeyboard(chat_message) elif bible_version_selection == True: message = chat_message.text command_hide(chat_message) version = verify_version(message) acr = dict_api_ver2acr[version] Database_control.set_bible_version(connection, cursor, acr, user_id) Database_control.set_mod_default(connection, cursor, user_id) Database_control.close_db(connection) show_mainkeyboard(chat_message) else: send_translated_message( chat_message.chat.id, "I don't understand. \nTry the help page at '/help'", origin_language) Database_control.set_mod_default(connection, cursor, user_id) Database_control.close_db(connection) show_mainkeyboard(chat_message) Database_control.close_db(connection)