def __list_all_modules(): import glob from os.path import basename, dirname, isfile # This generates a list of modules in this folder for the * in __main__ to # work. mod_paths = glob.glob(dirname(__file__) + "/*.py") all_modules = [ basename(f)[:-3] for f in mod_paths if isfile(f) and f.endswith(".py") and not f.endswith("__init__.py") ] if LOAD or NO_LOAD: to_load = LOAD if to_load: if not all( any(mod == module_name for module_name in all_modules) for mod in to_load): LOGGER.error("Invalid loadorder names. Quitting.") sys.exit(1) else: to_load = all_modules if NO_LOAD: LOGGER.info("Not loading: {}".format(NO_LOAD)) return [item for item in to_load if item not in NO_LOAD] return to_load return all_modules
def main(): # test_handler = CommandHandler("test", test) start_handler = CommandHandler("start", start, pass_args=True, run_async=True) help_handler = CommandHandler("help", get_help) help_callback_handler = CallbackQueryHandler(help_button, pattern=r"help_", run_async=True) help_staff_handler = CommandHandler( "staffhelp", staff_help, filters=CustomFilters.support_filter, run_async=True, ) settings_handler = CommandHandler("settings", get_settings, run_async=True) settings_callback_handler = CallbackQueryHandler(settings_button, pattern=r"stngs_", run_async=True) migrate_handler = MessageHandler(Filters.status_update.migrate, migrate_chats) is_chat_allowed_handler = MessageHandler(Filters.chat_type.groups, is_chat_allowed) # dispatcher.add_handler(test_handler) dispatcher.add_handler(start_handler) dispatcher.add_handler(help_handler) dispatcher.add_handler(settings_handler) dispatcher.add_handler(help_callback_handler) dispatcher.add_handler(help_staff_handler) dispatcher.add_handler(settings_callback_handler) dispatcher.add_handler(migrate_handler) dispatcher.add_handler(is_chat_allowed_handler) dispatcher.add_error_handler(error_handler) if WEBHOOK: LOGGER.info("Using webhooks.") updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) if CERT_PATH: updater.bot.set_webhook(url=URL + TOKEN, certificate=open(CERT_PATH, "rb")) else: updater.bot.set_webhook(url=URL + TOKEN) client.run_until_disconnected() else: LOGGER.info("Using long polling.") updater.start_polling(timeout=15, read_latency=5) if MESSAGE_DUMP: updater.bot.send_message(chat_id=MESSAGE_DUMP, text="System Started...") client.run_until_disconnected() updater.idle()
def main(): # test_handler = CommandHandler("test", test) start_handler = CommandHandler("start", start, pass_args=True) help_handler = CommandHandler("help", get_help) help_callback_handler = CallbackQueryHandler(help_button, pattern=r"help_") start_callback_handler = CallbackQueryHandler( send_start, pattern=r"bot_start") dispatcher.add_handler(start_callback_handler) startstop_callback_handler = CallbackQueryHandler( start_stop, pattern=r"close_menu") dispatcher.add_handler(startstop_callback_handler) settings_handler = CommandHandler("settings", get_settings) settings_callback_handler = CallbackQueryHandler( settings_button, pattern=r"stngs_") migrate_handler = MessageHandler( Filters.status_update.migrate, migrate_chats) is_chat_allowed_handler = MessageHandler(Filters.group, is_chat_allowed) # dispatcher.add_handler(test_handler) dispatcher.add_handler(start_handler) dispatcher.add_handler(help_handler) dispatcher.add_handler(settings_handler) dispatcher.add_handler(help_callback_handler) # dispatcher.add_handler(help_staff_handler) dispatcher.add_handler(settings_callback_handler) dispatcher.add_handler(migrate_handler) dispatcher.add_handler(is_chat_allowed_handler) dispatcher.add_error_handler(error_handler) if WEBHOOK: LOGGER.info("Using webhooks.") updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) if CERT_PATH: updater.bot.set_webhook( url=URL + TOKEN, certificate=open( CERT_PATH, "rb")) else: updater.bot.set_webhook(url=URL + TOKEN) client.run_until_disconnected() else: LOGGER.info("Using long polling.") updater.start_polling(timeout=15, read_latency=4) updater.bot.send_message( chat_id=MESSAGE_DUMP, text="Who do you think you are, Forrest Gump?") client.run_until_disconnected() updater.idle()
def send(msg, bot, update): if len(str(msg)) > 2000: with io.BytesIO(str.encode(msg)) as out_file: out_file.name = "output.txt" bot.send_document(chat_id=update.effective_chat.id, document=out_file) else: LOGGER.info(f"OUT: '{msg}'") bot.send_message(chat_id=update.effective_chat.id, text=f"`{msg}`", parse_mode=ParseMode.MARKDOWN)
def check_cas(user_id): cas_url = "https://api.cas.chat/check?user_id={}".format(user_id) try: r = get(cas_url, timeout=3) data = r.json() except BaseException: LOGGER.info(f"CAS check failed for {user_id}") return False if data and data["ok"]: return "https://cas.chat/query?u={}".format(user_id) else: return False
def migrate_chats(update, context): msg = update.effective_message # type: Optional[Message] if msg.migrate_to_chat_id: old_chat = update.effective_chat.id new_chat = msg.migrate_to_chat_id elif msg.migrate_from_chat_id: old_chat = msg.migrate_from_chat_id new_chat = update.effective_chat.id else: return LOGGER.info("Migrating from %s, to %s", str(old_chat), str(new_chat)) for mod in MIGRATEABLE: mod.__migrate__(old_chat, new_chat) LOGGER.info("Successfully migrated!") raise DispatcherHandlerStop
dispatcher.add_handler(settings_callback_handler) dispatcher.add_handler(migrate_handler) dispatcher.add_handler(is_chat_allowed_handler) dispatcher.add_error_handler(error_handler) if WEBHOOK: LOGGER.info("Using webhooks.") updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) if CERT_PATH: updater.bot.set_webhook(url=URL + TOKEN, certificate=open(CERT_PATH, "rb")) else: updater.bot.set_webhook(url=URL + TOKEN) client.run_until_disconnected() else: LOGGER.info("Using long polling.") updater.start_polling(timeout=15, read_latency=4) updater.bot.send_message(chat_id=MESSAGE_DUMP, text="Elizabeth Started...") client.run_until_disconnected() updater.idle() if __name__ == "__main__": LOGGER.info("Successfully loaded modules: " + str(ALL_MODULES)) client.start(bot_token=TOKEN) main()
mod_paths = glob.glob(dirname(__file__) + "/*.py") all_modules = [ basename(f)[:-3] for f in mod_paths if isfile(f) and f.endswith(".py") and not f.endswith("__init__.py") ] if LOAD or NO_LOAD: to_load = LOAD if to_load: if not all( any(mod == module_name for module_name in all_modules) for mod in to_load): LOGGER.error("Invalid loadorder names. Quitting.") sys.exit(1) else: to_load = all_modules if NO_LOAD: LOGGER.info("Not loading: {}".format(NO_LOAD)) return [item for item in to_load if item not in NO_LOAD] return to_load return all_modules ALL_MODULES = sorted(__list_all_modules()) LOGGER.info("Modules to load: %s", str(ALL_MODULES)) __all__ = ALL_MODULES + ["ALL_MODULES"]
else: send_settings(chat.id, user.id, True) def migrate_chats(update, context): msg = update.effective_message # type: Optional[Message] if msg.migrate_to_chat_id: old_chat = update.effective_chat.id new_chat = msg.migrate_to_chat_id elif msg.migrate_from_chat_id: old_chat = msg.migrate_from_chat_id new_chat = update.effective_chat.id else: return LOGGER.info("Migrating from %s, to %s", str(old_chat), str(new_chat)) for mod in MIGRATEABLE: mod.__migrate__(old_chat, new_chat) LOGGER.info("Successfully migrated!") raise DispatcherHandlerStop def is_chat_allowed(update, context): if len(WHITELIST_CHATS) != 0: chat_id = update.effective_message.chat_id if chat_id not in WHITELIST_CHATS: context.bot.send_message( chat_id=update.message.chat_id, text="Unallowed chat! Leaving...") try:
def log_input(update): user = update.effective_user.id chat = update.effective_chat.id LOGGER.info( f"IN: {update.effective_message.text} (user={user}, chat={chat})")