def locale_button(bot, update): chat = update.effective_chat user = update.effective_user # type: Optional[User] query = update.callback_query lang_match = re.findall(r"en|ru|ua|es|tr|id", query.data) if lang_match: if lang_match[0]: switch_to_locale(chat.id, lang_match[0]) query.answer(text="Language changed!") else: query.answer(text="Error!", show_alert=True) try: LANGUAGE = prev_locale(chat.id) locale = LANGUAGE.locale_name curr_lang = list_locales[locale] except: curr_lang = "English" if curr_lang == "Indonesian": text = "*Select language* \n" text += "User language : `Sinhala`" else: text = "*Select language* \n" text += "User language : `{}`".format(curr_lang) conn = connected(bot, update, chat, user.id, need_admin=False) if not conn == False: try: chatlng = prev_locale(conn).locale_name chatlng = list_locales[chatlng] if chatlng == "Indonesian": text += "\nConnected chat language : `Sinhala`" else: text += "\nConnected chat language : `{}`".format(chatlng) except: chatlng = "English" text += "*\n\nSelect new user language:*" query.message.reply_text( text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton("English 🇺🇸", callback_data="set_lang_en") ]] + [[ InlineKeyboardButton("Russian 🇷🇺", callback_data="set_lang_ru"), InlineKeyboardButton("Ukrainian 🇺🇦", callback_data="set_lang_ua") ]] + [[ InlineKeyboardButton("Spanish 🇪🇸", callback_data="set_lang_es"), InlineKeyboardButton("Turkish 🇹🇷", callback_data="set_lang_tr") ]] + [[ InlineKeyboardButton("Sinhala 🇱🇰", callback_data="set_lang_id") ]] + [[InlineKeyboardButton("⬅️ Back", callback_data="bot_start")]])) print(lang_match) query.message.delete() bot.answer_callback_query(query.id)
def locale_button(bot, update): chat = update.effective_chat user = update.effective_user # type: Optional[User] query = update.callback_query lang_match = re.findall(r"en|id", query.data) if lang_match: if lang_match[0]: switch_to_locale(chat.id, lang_match[0]) query.answer(text=tld(chat.id, 'language_switch_success').format( list_locales[lang_match[0]])) else: query.answer(text="Error!", show_alert=True) try: LANGUAGE = prev_locale(chat.id) locale = LANGUAGE.locale_name curr_lang = list_locales[locale] except: curr_lang = "English" text = tld(chat.id, "language_select_language") text += tld(chat.id, "language_user_language").format(curr_lang) conn = connected(bot, update, chat, user.id, need_admin=False) if conn: try: chatlng = prev_locale(conn).locale_name chatlng = list_locales[chatlng] text += tld(chat.id, "language_chat_language").format(chatlng) except: chatlng = "English" text += tld(chat.id, "language_sel_user_lang") query.message.reply_text( text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton("English 🇺🇸", callback_data="set_lang_en") ]] + [[ InlineKeyboardButton("Indonesian 🇮🇩", callback_data="set_lang_id") ]] + [[ InlineKeyboardButton(f"⬅️ {tld(chat.id, 'btn_go_back')}", callback_data="bot_start") ]])) print(lang_match) query.message.delete() bot.answer_callback_query(query.id)
def tld(chat_id, t, show_none=True): LANGUAGE = prev_locale(chat_id) if LANGUAGE: LOCALE = LANGUAGE.locale_name if LOCALE in ('en') and t in strings['en']: result = decode( encode(strings['en'][t], 'latin-1', 'backslashreplace'), 'unicode-escape') return result elif LOCALE in ('id') and t in strings['id']: result = decode( encode(strings['id'][t], 'latin-1', 'backslashreplace'), 'unicode-escape') return result if t in strings['en']: result = decode( encode(strings['en'][t], 'latin-1', 'backslashreplace'), 'unicode-escape') return result err = f"No string found for {t}.\nReport it in @HarukaAyaGroup." LOGGER.warning(err) return err
def locale(bot, update, args): chat = update.effective_chat if len(args) > 0: locale = args[0].lower() if locale in list_locales: if locale in ('en', 'ru', 'ua', 'es', 'tr', 'id', 'it'): switch_to_locale(chat.id, locale) update.message.reply_text( tld(chat.id, 'Switched to {} successfully!').format( list_locales[locale])) else: update.message.reply_text("{} is not supported yet!".format( list_locales[locale])) else: update.message.reply_text( "Is that even a valid language code? Use an internationally accepted ISO code!" ) else: LANGUAGE = prev_locale(chat.id) if LANGUAGE: locale = LANGUAGE.locale_name native_lang = list_locales[locale] update.message.reply_text( "Current locale for this chat is: *{}*".format(native_lang), parse_mode=ParseMode.MARKDOWN) else: update.message.reply_text( "Current locale for this chat is: *English*", parse_mode=ParseMode.MARKDOWN)
def tld_help(chat_id, t): LANGUAGE = prev_locale(chat_id) print("tld_help ", chat_id, t) if LANGUAGE: LOCALE = LANGUAGE.locale_name t = t + "_help" print("Test2", t) if LOCALE in ('ru') and t in RussianStrings: return RussianStrings[t] elif LOCALE in ('ua') and t in UkrainianStrings: return UkrainianStrings[t] elif LOCALE in ('es') and t in SpanishStrings: return SpanishStrings[t] elif LOCALE in ('tr') and t in TurkishStrings: return TurkishStrings[t] elif LOCALE in ('id') and t in IndonesianStrings: return IndonesianStrings[t] elif LOCALE in ('pt') and t in PortugueseStrings: return PortugueseStrings[t] else: return False else: return False
def tld(chat_id, t, show_none=True): LANGUAGE = prev_locale(chat_id) print(chat_id, t) if LANGUAGE: LOCALE = LANGUAGE.locale_name if LOCALE in ('ru') and t in RussianStrings: return RussianStrings[t] elif LOCALE in ('ua') and t in UkrainianStrings: return UkrainianStrings[t] elif LOCALE in ('es') and t in SpanishStrings: return SpanishStrings[t] elif LOCALE in ('tr') and t in TurkishStrings: return TurkishStrings[t] elif LOCALE in ('id') and t in IndonesianStrings: return IndonesianStrings[t] elif LOCALE in ('pt') and t in PortugueseStrings: return PortugueseStrings[t] else: if t in EnglishStrings: return EnglishStrings[t] else: return t elif show_none: if t in EnglishStrings: return EnglishStrings[t] else: return t
def locale(bot, update, args): chat = update.effective_chat if len(args) > 0: locale = args[0].lower() if locale in list_locales: if locale in ('en', 'id'): switch_to_locale(chat.id, locale) update.message.reply_text( tld(chat.id, 'language_switch_success').format( list_locales[locale])) else: update.message.reply_text( tld(chat.id, "language_not_supported").format(list_locales[locale])) else: update.message.reply_text(tld(chat.id, "language_code_not_valid")) else: LANGUAGE = prev_locale(chat.id) if LANGUAGE: locale = LANGUAGE.locale_name native_lang = list_locales[locale] update.message.reply_text(tld( chat.id, "language_current_locale").format(native_lang), parse_mode=ParseMode.MARKDOWN) else: update.message.reply_text(tld( chat.id, "language_current_locale").format("English"), parse_mode=ParseMode.MARKDOWN)
def tld_list(chat_id, t): LANGUAGE = prev_locale(chat_id) if LANGUAGE: LOCALE = LANGUAGE.locale_name if LOCALE in ('en') and t in strings['en']: return strings['en'][t] if t in strings['en']: return strings['en'][t]
def tld_list(chat_id, t): LANGUAGE = prev_locale(chat_id) if LANGUAGE: LOCALE = LANGUAGE.locale_name if LOCALE in ('en') and t in strings['en']: return strings['en'][t] if t in strings['en']: return strings['en'][t] LOGGER.warning(f"#NOSTR No string found for {t}.") return f"No string found for {t}.\nReport it in @HarukaAyaGroup."
def tld(chat_id, t, show_none=True): LANGUAGE = prev_locale(chat_id) if LANGUAGE: LOCALE = LANGUAGE.locale_name if LOCALE in ('en') and t in strings['en']: result = decode( encode(strings['en'][t], 'latin-1', 'backslashreplace'), 'unicode-escape') return result if t in strings['en']: result = decode( encode(strings['en'][t], 'latin-1', 'backslashreplace'), 'unicode-escape') return result
def weather(bot, update, args): chat = update.effective_chat # type: Optional[Chat] if len(args) == 0: update.effective_message.reply_text( tld(chat.id, "Write a location to check the weather.")) return location = " ".join(args) if location.lower() == bot.first_name.lower(): update.effective_message.reply_text( tld(chat.id, "I will keep an eye on both happy and sad times!")) bot.send_sticker(chat.id, BAN_STICKER) return try: LANGUAGE = prev_locale(chat.id) try: LANGUAGE = LANGUAGE.locale_name except: LANGUAGE = "en" print(LANGUAGE) owm = pyowm.OWM(API_WEATHER, language=LANGUAGE) observation = owm.weather_at_place(location) getloc = observation.get_location() thelocation = getloc.get_name() if thelocation == None: thelocation = "Unknown" theweather = observation.get_weather() temperature = theweather.get_temperature(unit='celsius').get('temp') if temperature == None: temperature = "Unknown" # Weather symbols status = "" status_now = theweather.get_weather_code() print(status_now) if status_now == 232: # Rain storm status += "⛈️ " elif status_now == 321: # Drizzle status += "🌧️ " elif status_now == 504: # Light rain status += "🌦️ " elif status_now == 531: # Cloudy rain status += "⛈️ " elif status_now == 622: # Snow status += "🌨️ " elif status_now == 781: # Atmosphere status += "🌪️ " elif status_now == 800: # Bright status += "🌤️ " elif status_now == 801: # A little cloudy status += "⛅️ " elif status_now == 804: # Cloudy status += "☁️ " print(getloc, observation) print(status) #print(status.formatted_address) status = status + theweather._detailed_status update.message.reply_text( tld(chat.id, "Today in {} is being {}, around {}°C.\n").format( thelocation, status, temperature)) except: update.effective_message.reply_text( tld(chat.id, "Sorry, location not found."))