async def miui(event): """Send a message when the command /recovery or /fastboot is sent.""" try: device = event.pattern_match.group(1).lower() except (IndexError, AttributeError): if event.message.message.endswith('/recovery') \ or event.message.message.endswith('/fastboot'): device = DATABASE.get_codename(event.chat_id) else: return if not device: return locale = DATABASE.get_locale(event.chat_id) if device not in list(PROVIDER.miui_codenames): await event.reply(await error_message(device, locale)) return method = "Recovery" if "recovery" in event.pattern_match.string else "Fastboot" message, buttons = await miui_message(device, method, PROVIDER.miui_updates, PROVIDER.codenames_names, locale) try: await event.reply(message, buttons=buttons, link_preview=False) except ChatWriteForbiddenError: pass raise events.StopPropagation
async def start(event): """Send a message when the command /start is sent.""" # sender_info = await get_user_info(event) # DATABASE.add_chat_to_db(sender_info) locale = DATABASE.get_locale(event.chat_id) if event.is_group and event.pattern_match.group(1): message, buttons = await welcome_in_pm_message(locale) try: await event.reply(message, buttons=buttons) except ChatWriteForbiddenError: pass return try: key = event.message.message.split('/start ')[1] except IndexError: key = None if event.message.message.endswith('help'): await show_help(event) elif key and key != 'start': try: decoded = b64decode(key).decode() if "/subscribe" in decoded: event.message.message = decoded await subscribe(event) except UnicodeDecodeError: pass elif event.is_private: message, buttons = await welcome_message(locale) try: await event.reply(message, buttons=buttons, link_preview=False) except UserIsBlockedError: pass raise events.StopPropagation # Other handlers won't have an event to work with
async def subscription_handler(event): """List your current subscriptions""" if not await subscription_allowed(event): return locale = DATABASE.get_locale(event.chat_id) subscriptions = DATABASE.get_chat_subscriptions(event.chat_id) await event.reply(await subscriptions_message(subscriptions, locale)) raise events.StopPropagation
async def set_lang_keyboard(event): """Send a message when the command /set_lang is sent""" locale = DATABASE.get_locale(event.chat_id) if not await subscription_allowed(event): return message, buttons = await set_locale_pm_message(locale) await event.reply(message, buttons=buttons) raise events.StopPropagation
async def set_lang_handler(event): """ Set the language based on the user selection""" lang = event.message.message.split(' ')[0] DATABASE.set_locale(event.chat_id, lang) locale = DATABASE.get_locale(event.chat_id) message = await set_locale_message(lang, locale) await event.reply(message, buttons=Button.clear()) raise events.StopPropagation
async def help_callback(event): """help buttons callback for back button""" locale = DATABASE.get_locale(event.chat_id) message, buttons = await help_main_message(locale) try: await event.edit(message, buttons=buttons) except MessageNotModifiedError: pass
async def codename(event): """Send a message when the command /codename is sent.""" device = event.pattern_match.group(1) locale = DATABASE.get_locale(event.chat_id) message = await codename_message(device, PROVIDER.names_codenames, locale) if message: await event.reply(message) raise events.StopPropagation
async def settings_callback(event): """settings buttons callback for back button""" locale = DATABASE.get_locale(event.chat_id) message, buttons = await settings_main_message(locale) try: await event.edit(message, buttons=buttons) except (MessageNotModifiedError, ChannelPrivateError, ChatWriteForbiddenError): pass
async def guides(event): """Send a message when the command /guides is sent.""" locale = DATABASE.get_locale(event.chat_id) message, buttons = await guides_message(locale) try: await event.reply(message, buttons=buttons, link_preview=False) except (ChannelPrivateError, ChatWriteForbiddenError, UserIsBlockedError): pass raise events.StopPropagation
async def subscriptions_help(event): """subscriptions settings callback handler""" locale = DATABASE.get_locale(event.chat_id) subscriptions = DATABASE.get_chat_subscriptions(event.chat_id) try: await event.edit(await subscriptions_message(subscriptions, locale), buttons=[ [Button.inline(LOCALIZE.get_text(locale, "Back"), data="settings")], ]) except (MessageNotModifiedError, ChannelPrivateError, ChatWriteForbiddenError): pass
async def whatis(event): """Send a message when the command /whatis is sent.""" device = event.pattern_match.group(1).lower() locale = DATABASE.get_locale(event.chat_id) if device not in list(PROVIDER.codenames_names.keys()): await event.reply(await error_message(device, locale)) return message = await whatis_message(device, PROVIDER.codenames_names, locale) await event.reply(message) raise events.StopPropagation
async def oss(event): """Send a message when the command /oss is sent.""" locale = DATABASE.get_locale(event.chat_id) device = event.pattern_match.group(1) message = await oss_message(device, locale) if message: await event.reply(message) else: await event.reply(await error_message(device, locale)) raise events.StopPropagation
async def lang_help(event): """language help callback handler""" locale = DATABASE.get_locale(event.chat_id) try: await event.edit(await lang_settings_message(locale), buttons=[ [Button.inline(LOCALIZE.get_text(locale, "change_language"), data="change_language")], [Button.inline(LOCALIZE.get_text(locale, "Back"), data="settings")] ]) except (MessageNotModifiedError, ChannelPrivateError, ChatWriteForbiddenError): pass
async def show_settings(event): """Send a message when the command /settings is sent.""" locale = DATABASE.get_locale(event.chat_id) if not await subscription_allowed(event): return message, buttons = await settings_main_message(locale) try: await event.respond(message, buttons=buttons) except (MessageNotModifiedError, ChannelPrivateError, ChatWriteForbiddenError): pass raise events.StopPropagation
async def set_codename_help(event): """preferred device settings callback handler""" locale = DATABASE.get_locale(event.chat_id) device = DATABASE.get_codename(event.chat_id) try: await event.edit(await preferred_device_message(device, PROVIDER.codenames_names, locale), buttons=[ [Button.inline(LOCALIZE.get_text(locale, "change_preferred_device"), data="preferred_device_help")], [Button.inline(LOCALIZE.get_text(locale, "Back"), data="settings")] ]) except (MessageNotModifiedError, ChannelPrivateError, ChatWriteForbiddenError): pass
async def specs(event): """Send a message when the command /specs is sent.""" device = event.pattern_match.group(1) locale = DATABASE.get_locale(event.chat_id) message = await specs_message(device, PROVIDER.specs_data, locale) if message: try: await event.reply(message) except (ChannelPrivateError, ChatWriteForbiddenError): pass else: await event.reply(await error_message(device, locale)) raise events.StopPropagation
async def eu_help(event): """eu help callback handler""" locale = DATABASE.get_locale(event.chat_id) try: await event.edit(await eu_help_message(locale), buttons=[ [ Button.inline(LOCALIZE.get_text( locale, "Back"), data="help") ], ]) except MessageNotModifiedError: pass
async def unsubscribe(event): """Unsubscribe to updates""" if not await subscription_allowed(event): return sub_type = event.pattern_match.group(1) device = event.pattern_match.group(2) locale = DATABASE.get_locale(event.chat_id) if not await is_device(sub_type, device): await event.reply(await wrong_codename_message(locale)) return DATABASE.remove_subscription(await get_user_info(event), sub_type, device) message = await unsubscribed_message(sub_type, device, locale) await event.reply(message) raise events.StopPropagation
async def set_codename_handler(event): """Send a message when the command /set_codename is sent""" locale = DATABASE.get_locale(event.chat_id) if not await subscription_allowed(event): return device = event.pattern_match.group(1) if device not in PROVIDER.miui_codenames: await event.reply(await wrong_codename_message(locale)) return if DATABASE.set_codename(event.chat_id, device): try: await event.reply(await set_codename_message(device, PROVIDER.codenames_names, locale)) except (MessageNotModifiedError, ChannelPrivateError, ChatWriteForbiddenError): pass raise events.StopPropagation
async def show_help(event): """Send a message when the command /help is sent.""" locale = DATABASE.get_locale(event.chat_id) if not event.is_private: message, buttons = await open_in_pm_message(locale) try: await event.reply(message, buttons=buttons) except ChatWriteForbiddenError: pass else: message, buttons = await help_main_message(locale) try: await event.reply(message, buttons=buttons) except ChatWriteForbiddenError: pass raise events.StopPropagation
async def post_firmware_updates(): """ Send firmware updates to subscribers every 65 minutes """ while True: new_updates = await diff_updates(PROVIDER.firmware_data, PROVIDER.bak_firmware_data) if not new_updates: await sleep(65 * 60) continue for codename, updates in new_updates.items(): subscriptions = DATABASE.get_subscriptions('firmware', codename) if subscriptions: for subscription in subscriptions: for update in updates: locale = DATABASE.get_locale(subscription[0]) message, buttons = await firmware_update_message(codename, update, locale) await post_update(subscription, message, buttons) await sleep(2) await sleep(65 * 60)
async def orangefox(event): """Send a message when the command /of is sent.""" try: device = event.pattern_match.group(1).lower() except (IndexError, AttributeError): if event.message.message.endswith('/of'): device = DATABASE.get_codename(event.chat_id) else: return if not device: return locale = DATABASE.get_locale(event.chat_id) try: message, buttons = await orangefox_message(device, locale) await event.reply(message, buttons=buttons, link_preview=False) except TypeError: await event.reply(await error_message(device, locale)) raise events.StopPropagation
async def firmware(event): """Send a message when the command /archive is sent.""" try: device = event.pattern_match.group(1).lower() except (IndexError, AttributeError): if event.message.message.endswith('/archive'): device = DATABASE.get_codename(event.chat_id) else: return if not device: return locale = DATABASE.get_locale(event.chat_id) if device not in PROVIDER.miui_codenames: await event.reply(await error_message(device, locale)) return message, buttons = await archive_message(device, PROVIDER.codenames_names, locale) await event.reply(message, buttons=buttons, link_preview=False) raise events.StopPropagation
async def latest(event): """Send a message when the command /latest is sent.""" try: device = event.pattern_match.group(1).lower() except (IndexError, AttributeError): if event.message.message.endswith('/latest'): device = DATABASE.get_codename(event.chat_id) else: return if not device: return locale = DATABASE.get_locale(event.chat_id) if device not in list(PROVIDER.miui_codenames): await event.reply(await error_message(device, locale)) return message = await latest_miui_message(device, PROVIDER.miui_updates, PROVIDER.codenames_names, locale) await event.reply(message) raise events.StopPropagation
async def post_miui_updates(): """ Send miui updates to subscribers every 65 minutes """ while True: new_updates = await diff_miui_updates(PROVIDER.miui_updates, PROVIDER.bak_miui_updates) if not new_updates: await sleep(65 * 60) continue for codename, updates in new_updates.items(): subscriptions = DATABASE.get_subscriptions('miui', codename) if subscriptions: for subscription in subscriptions: for update in updates: locale = DATABASE.get_locale(subscription[0]) message, buttons = await miui_update_message( update, PROVIDER.codenames_names, locale) await post_update(subscription, message, buttons) await sleep(2) await sleep(65 * 60)
async def subscribe(event): """Subscribe to updates""" if not await subscription_allowed(event): return try: sub_type = event.pattern_match.group(1) device = event.pattern_match.group(2) except IndexError: sub_type = event.message.message.split(' ')[1] device = event.message.message.split(' ')[2] locale = DATABASE.get_locale(event.chat_id) if not await is_device(sub_type, device): await event.reply(await wrong_codename_message(locale)) return if DATABASE.add_subscription(await get_user_info(event), sub_type, device): message = await subscribed_message(sub_type, device, locale) else: message = await already_subscribed_message(sub_type, device, locale) await event.reply(message) raise events.StopPropagation
async def twrp(event): """Send a message when the command /twrp is sent.""" try: device = event.pattern_match.group(1).lower() except (IndexError, AttributeError): if event.message.message.endswith('/twrp'): device = DATABASE.get_codename(event.chat_id) else: return if not device: return locale = DATABASE.get_locale(event.chat_id) if device not in list(PROVIDER.twrp_data.keys()): await event.reply(await error_message(device, locale)) return message, buttons = await twrp_message(device, PROVIDER.twrp_data, locale) try: await event.reply(message, buttons=buttons, link_preview=False) except (ChannelPrivateError, ChatWriteForbiddenError): pass raise events.StopPropagation
async def xiaomi_eu(event): """Send a message when the command /eu is sent.""" try: device = event.pattern_match.group(1).lower() except (IndexError, AttributeError): if event.message.message.endswith('/eu'): device = DATABASE.get_codename(event.chat_id) else: return if not device: return locale = DATABASE.get_locale(event.chat_id) if device not in list(PROVIDER.eu_codenames.keys()): await event.reply(await error_message(device, locale)) return try: message, buttons = await eu_message(device, PROVIDER.eu_data, PROVIDER.eu_codenames, locale) await event.reply(message, buttons=buttons, link_preview=False) except TypeError: pass raise events.StopPropagation
async def handler(event): """Handle inline queries""" # pylint: disable=too-many-branches, too-many-statements result = None query = None query_args = re.findall(r'\S+', event.text.lower().strip()) try: query = query_args[0] except IndexError: pass try: query_request = query_args[1] except IndexError: query_request = None locale = DATABASE.get_locale(event.chat_id) if query == 'twrp' and query_request is not None: if query_request in list(PROVIDER.twrp_data.keys()): result = await twrp_inline(event, query_request, PROVIDER.twrp_data, locale) if query == 'of' and query_request is not None: result = await orangefox_inline(event, query_request, locale) if query == 'pb' and query_request is not None: if query_request in str(PROVIDER.pitchblack_data): result = await pitchblack_inline(event, query_request, PROVIDER.pitchblack_data, locale) if query == 'firmware' and query_request is not None: if query_request in PROVIDER.firmware_codenames: result = await firmware_inline(event, query_request, PROVIDER.codenames_names, locale) if query == 'vendor' and query_request is not None: if query_request in PROVIDER.vendor_codenames: result = await vendor_inline(event, query_request, PROVIDER.codenames_names, locale) if query == 'models' and query_request is not None: if query_request in list(PROVIDER.models_data.keys()): result = await models_inline(event, query_request, PROVIDER.models_data, locale) if query == 'whatis' and query_request is not None: if query_request in list(PROVIDER.codenames_names.keys()): result = await whatis_inline(event, query_request, PROVIDER.codenames_names, locale) if query == 'codename': query_request = ' '.join(query_args[1:]) if await get_codename(query_request, PROVIDER.names_codenames): result = await codename_inline(event, query_request, PROVIDER.names_codenames, locale) if query == 'recovery' and query_request is not None: if query_request in list(PROVIDER.miui_codenames): result = await miui_inline(event, query_request, "Recovery", PROVIDER.miui_updates, PROVIDER.codenames_names, locale) if query == 'fastboot' and query_request is not None: if query_request in list(PROVIDER.miui_codenames): result = await miui_inline(event, query_request, "Fastboot", PROVIDER.miui_updates, PROVIDER.codenames_names, locale) if query == 'latest' and query_request is not None: if query_request in list(PROVIDER.miui_codenames): result = await latest_miui_inline(event, query_request, PROVIDER.miui_updates, PROVIDER.codenames_names, locale) if query == 'archive' and query_request is not None: if query_request in PROVIDER.miui_codenames: result = await archive_inline(event, query_request, PROVIDER.codenames_names, locale) if query == 'eu' and query_request is not None: if query_request in list(PROVIDER.eu_codenames.keys()): result = await eu_inline(event, query_request, PROVIDER.eu_data, PROVIDER.eu_codenames, locale) if query == 'specs' and query_request is not None: result = await specs_inline(event, query_request, PROVIDER.specs_data, locale) if query == 'oss' and query_request is not None: result = await oss_inline(event, query_request, locale) if query == 'unlockbl': result = await unlockbl_inline(event, locale) if query == 'tools': result = await tools_inline(event, locale) if query == 'guides': result = await guides_inline(event, locale) else: pass if result: await event.answer([result])