def detect(message): if not botname_checker(message): return inputtext = utils.textparser(message) if inputtext is None: logger.write_log(message, "none") return logger.write_log(message, inputtext) try: lang = interlayer.lang_list.get(interlayer.extract_lang(inputtext)) if locales.get_chat_lang(message.chat.id) != "en": translated_lang = " (" + interlayer.get_translate( lang, locales.get_chat_lang(message.chat.id)) + ")" else: translated_lang = "" utils.bot.reply_to( message, locales.get_text(message.chat.id, "langDetectedAs").format(lang, translated_lang)) except (interlayer.BadTrgLangException, interlayer.UnkTransException): utils.bot.reply_to(message, locales.get_text(message.chat.id, "langDetectErr"))
def translate(message): if botname_checker(message): inputtext = utils.textparser(message) if inputtext is None: logger.write_log(message, "none") return logger.write_log(message, inputtext) src_lang = None message.text = utils.lang_autocorr(message.text) if utils.extract_arg(message.text, 2) is not None: src_lang = utils.extract_arg(message.text, 1) lang = utils.extract_arg(message.text, 2) elif utils.extract_arg(message.text, 1) is not None: lang = utils.extract_arg(message.text, 1) else: utils.bot.reply_to( message, locales.get_text(message.chat.id, "specifyLang")) return try: utils.bot.reply_to( message, interlayer.get_translate(inputtext, lang, src_lang=src_lang) + add_ad(message.chat.id)) except interlayer.BadTrgLangException: utils.bot.reply_to( message, locales.get_text(message.chat.id, "badTrgLangException")) except interlayer.BadSrcLangException: utils.bot.reply_to( message, locales.get_text(message.chat.id, "badSrcLangException")) except interlayer.TooManyRequestException: utils.bot.reply_to( message, locales.get_text(message.chat.id, "tooManyRequestException")) except interlayer.TooLongMsg: utils.bot.reply_to(message, locales.get_text(message.chat.id, "tooLongMsg")) except interlayer.EqualLangsException: utils.bot.reply_to( message, locales.get_text(message.chat.id, "equalLangsException")) except interlayer.UnkTransException: utils.bot.reply_to( message, locales.get_text(message.chat.id, "unkTransException"))
def qwerty_main(message): text = utils.textparser(message) if text is None: logger.write_log(message, "none") return logger.write_log(message, text) message.text = utils.lang_autocorr(message.text) arg1, arg2 = utils.extract_arg(message.text, 1), utils.extract_arg(message.text, 2) if arg2 is None: tab1 = utils.layouts.get(interlayer.extract_lang(text)) tab2 = utils.layouts.get(arg1) else: tab1 = utils.layouts.get(arg1) tab2 = utils.layouts.get(arg2) if tab1 is None and arg2 is None: try: recognized_lang = interlayer.get_translate( interlayer.lang_list.get(interlayer.extract_lang(text)), "ru") except (interlayer.BadTrgLangException, interlayer.UnkTransException, interlayer.TooManyRequestException): recognized_lang = "Unknown" utils.bot.reply_to( message, locales.get_text(message.chat.id, "unknownSourceLang").format(recognized_lang)) return if tab1 is None or tab2 is None: utils.bot.reply_to(message, locales.get_text(message.chat.id, "unknownLayout")) return try: translated_text = text.translate(str.maketrans(tab1, tab2)) utils.bot.reply_to(message, translated_text + ad_module.add_ad(message.chat.id)) except Exception as e: logging.error(str(e) + "\n" + traceback.format_exc()) utils.bot.reply_to(message, locales.get_text(message.chat.id, "layoutError"))
def qwerty_main(message): text = utils.textparser(message) if text is None: logger.write_log("none", message) return logger.write_log(text, message) arg1, arg2 = utils.extract_arg(message.text, 1), utils.extract_arg(message.text, 2) if arg2 is None: tab1 = utils.layouts.get(utils.extract_lang(text)) tab2 = utils.layouts.get(arg1) else: tab1 = utils.layouts.get(arg1) tab2 = utils.layouts.get(arg2) if tab1 is None and arg2 is None: utils.bot.reply_to( message, "Исходный язык не распознан. Неправильный аргумент или неверно распознан " "язык? (" + LANGUAGES.get(utils.extract_lang(text)) + ")\n" "Попробуйте указать исходный язык вручную. Возможно, язык отсутствует в " "словаре символов") return if tab1 is None or tab2 is None: utils.bot.reply_to( message, "Неизвестная раскладка. Возможно, язык отсутствует в словаре символов" ) return try: translated_text = text.translate(str.maketrans(tab1, tab2)) utils.bot.reply_to(message, translated_text) except Exception as e: logger.write_log("ERR: " + str(e) + "\n" + traceback.format_exc()) utils.bot.reply_to( message, "Ошибка смены раскладки текста. Обратитесь к авторам бота\n" "Информация для отладки сохранена в логах бота.")
def module_rem_task(message): if utils.user_admin_checker(message) is False: return if not enable_ad: utils.bot.reply_to(message, locales.get_text(message.chat.id, "adDisabled")) return text = utils.textparser(message) if text is None: return try: sql_worker.rem_task(message.reply_to_message.id, message.chat.id) utils.bot.reply_to( message, locales.get_text(message.chat.id, "taskerRemSuccess")) except sql_worker.SQLWriteError: utils.bot.reply_to(message, locales.get_text(message.chat.id, "taskerRemError"))
def module_add_task(message): if utils.user_admin_checker(message) is False: return if not enable_ad: utils.bot.reply_to(message, locales.get_text(message.chat.id, "adDisabled")) return text = utils.textparser(message) if text is None: return if utils.extract_arg(message.text, 1) is None or utils.extract_arg( message.text, 2) is None: utils.bot.reply_to( message, locales.get_text(message.chat.id, "taskerArguments")) return try: expire_time = int( time.time()) + int(utils.extract_arg(message.text, 2)) * 86400 except (TypeError, ValueError): utils.bot.reply_to( message, locales.get_text(message.chat.id, "taskerArguments")) return lang_code = utils.extract_arg(message.text, 1) if sql_worker.write_task(message.reply_to_message.id, text, lang_code, expire_time, message.chat.id) is False: utils.bot.reply_to(message, locales.get_text(message.chat.id, "taskerFail")) else: utils.bot.reply_to( message, locales.get_text(message.chat.id, "taskerSuccess").format( lang_code, datetime.datetime.fromtimestamp(expire_time).strftime( "%d.%m.%Y %H:%M:%S")))
def translate_main(message): lang = utils.extract_arg(message.text, 1) if utils.extract_arg(message.text, 2) is not None: lang = lang + " " + utils.extract_arg(message.text, 2) inputtext = utils.textparser(message) if inputtext is None: logger.write_log("none", message) return logger.write_log(inputtext, message) if lang is None: utils.bot.reply_to(message, "Укажите код языка/название страны") return try: translated = utils.translator.translate(inputtext, lang).text if translated == inputtext and translate_verify is True: logger.write_log("ERR: GOOGLE_API_REJECT") utils.bot.reply_to( message, "Исходный и итоговый текст совпадают. Возможно, Google Api отклонил запрос. " "Если вы уверены, что так быть не должно, повторите попытку позже" ) return utils.bot.reply_to(message, translated) except Exception as e: if str(e) in "invalid destination language": utils.bot.reply_to(message, "Указан неверный код языка/название страны") else: logger.write_log("ERR: " + str(e) + "\n" + traceback.format_exc()) utils.bot.reply_to( message, "Ошибка перевода. Обратитесь к авторам бота\n" "Информация для отладки сохранена в логах бота.")
def distort_main(message): if max_inits == 0: utils.bot.reply_to( message, "Ошибка, хостер бота принудительно отключил функцию Distort") return inputshiz = utils.textparser(message) if inputshiz is None: logger.write_log("none", message) return logger.write_log(inputshiz, message) try: counter = int(utils.extract_arg(message.text, 1)) except (ValueError, TypeError): utils.bot.reply_to(message, "Ошибка, число не распознано") return if counter > max_inits or counter < 1: utils.bot.reply_to( message, "Ошибка, укажите значение от 1 до " + str(max_inits)) return randlangs = "" if utils.extract_arg(message.text, 2) is not None and utils.extract_arg( message.text, 3) is not None: endlang = utils.extract_arg(message.text, 2) + " " + utils.extract_arg( message.text, 3) elif utils.extract_arg(message.text, 2) is not None: endlang = utils.extract_arg(message.text, 2) else: endlang = utils.extract_lang(inputshiz) tmpmessage = utils.bot.reply_to( message, "Генерация начата, осталось " + str(counter * cooldown) + " секунд") idc = tmpmessage.chat.id idm = tmpmessage.message_id for i in range(counter): randlang = random.choice(list(LANGUAGES)) randlangs += randlang + "; " for iteration in range( attempts ): # three tries as default, if = 0 => without checking inputshizchecker = inputshiz try: inputshiz = utils.translator.translate(inputshiz, randlang).text if inputshizchecker != inputshiz: break except Exception as e: if str(e) in "invalid destination language": pass else: logger.write_log("ERR: " + str(e) + "\n" + traceback.format_exc()) utils.bot.edit_message_text( "Ошибка искажения текста. Обратитесь к авторам бота\n" "Информация для отладки сохранена в логах бота.", idc, idm) return if iteration == attempts - 1: logger.write_log("ERR GOOGLE_API_REJECT") utils.bot.edit_message_text( "Неизвестная ошибка перевода. Повторите попытку позже.\n" "Возможно, запрос был заблокирован Google Api", idc, idm) return time.sleep(cooldown) outstr = "Готова итерация " + str(i + 1) + "/" + str(counter) + "\n" \ "Осталось " + str((counter - i - 1) * cooldown) + " секунд" utils.bot.edit_message_text(outstr, idc, idm) try: inputshiz = utils.translator.translate(inputshiz, endlang).text except Exception as e: if str(e) in "invalid destination language": endlang = utils.extract_lang(utils.textparser(message)) inputshiz = utils.translator.translate(inputshiz, endlang).text utils.bot.edit_message_text( inputshiz + "\n\nИспользовались искажения: " + randlangs, idc, idm)
def distort_main(message): if max_inits == 0: utils.bot.reply_to( message, locales.get_text(message.chat.id, "distortDisabled")) return inputshiz = utils.textparser(message) if inputshiz is None: logger.write_log(message, "none") return logger.write_log(message, inputshiz) try: counter = int(utils.extract_arg(message.text, 1)) except (ValueError, TypeError): utils.bot.reply_to(message, locales.get_text(message.chat.id, "distortNaN")) return if counter > max_inits or counter < 1: utils.bot.reply_to( message, locales.get_text(message.chat.id, "distortWrongNumber").format(str(max_inits))) return randlangs_list = "" if utils.extract_arg(message.text, 2) is not None: endlang = utils.extract_arg(utils.lang_autocorr(message.text), 2) if interlayer.lang_list.get(endlang) is None: utils.bot.reply_to( message, locales.get_text(message.chat.id, "distortWrongLang")) return else: endlang = interlayer.extract_lang(inputshiz) tmpmessage = utils.bot.reply_to( message, locales.get_text(message.chat.id, "distortStarted")) idc = tmpmessage.chat.id idm = tmpmessage.message_id lastlang = interlayer.extract_lang(inputshiz) randlang = random.choice(list(interlayer.lang_list)) for i in range(counter): while randlang == lastlang: randlang = random.choice(list(interlayer.lang_list)) randlangs_list += randlang + "; " try: inputshiz = interlayer.get_translate(inputshiz, randlang, True) except interlayer.TooManyRequestException: utils.bot.edit_message_text( locales.get_text(message.chat.id, "tooManyRequestException"), idc, idm) return except interlayer.TooLongMsg: utils.bot.edit_message_text( locales.get_text(message.chat.id, "tooLongMsg"), idc, idm) return except interlayer.UnkTransException: utils.bot.edit_message_text( locales.get_text(message.chat.id, "distortUnkTransException"), idc, idm) return lastlang = randlang try: inputshiz = interlayer.get_translate(inputshiz, endlang) except (interlayer.UnkTransException, interlayer.TooLongMsg, interlayer.TooManyRequestException, interlayer.BadTrgLangException): utils.bot.reply_to( message, locales.get_text(message.chat.id, "distortEndingError")) utils.bot.edit_message_text( inputshiz + locales.get_text(message.chat.id, "usedDistortions") + randlangs_list + ad_module.add_ad(idc), idc, idm)