From 4eacf5836af45720d5c8306a13d411ae28530070 Mon Sep 17 00:00:00 2001 From: himitsu Date: Thu, 24 Sep 2020 15:41:17 +0700 Subject: [PATCH] Feature : heroku add configvars @ /settings -> heroku config vars --- nana/assistant/__main__.py | 21 +++------ nana/assistant/heroku.py | 63 +++++++++++++++++++++++++++ nana/assistant/input_handler.py | 76 +++++++++++++++++++++++++++++++++ nana/assistant/settings.py | 69 +++--------------------------- nana/assistant/theme_modules.py | 50 +--------------------- 5 files changed, 153 insertions(+), 126 deletions(-) create mode 100644 nana/assistant/heroku.py create mode 100644 nana/assistant/input_handler.py diff --git a/nana/assistant/__main__.py b/nana/assistant/__main__.py index 837755a9..b0c8fe09 100644 --- a/nana/assistant/__main__.py +++ b/nana/assistant/__main__.py @@ -4,6 +4,7 @@ from pyrogram import Filters, InlineKeyboardMarkup, InlineKeyboardButton from nana import app, setbot, AdminSettings, DB_AVAILABLE, USERBOT_VERSION, ASSISTANT_VERSION, BotUsername, Owner, \ OwnerName, NANA_IMG +from nana.assistant.settings import get_text_settings, get_button_settings from nana.assistant.theme.theme_helper import get_theme if DB_AVAILABLE: @@ -136,17 +137,9 @@ async def report_some_errors(client, query): await client.answer_callback_query(query.id, "Report was sent!") -namevars = "" -valuevars = "" - - -@setbot.on_callback_query(dynamic_data_filter("add_vars")) -async def add_vars(_client, query): - global namevars - await query.message.edit_text("Send Name Variable :") - setbot.on_message() - - -async def name_vars(_client, message): - global namevars - namevars = message.text +@setbot.on_message(Filters.user(AdminSettings) & Filters.command(["settings"]) & Filters.private) +async def settings(_client, message): + text = await get_text_settings() + button = await get_button_settings() + img = await get_theme("settings") + await setbot.send_photo(Owner, img, caption=text, reply_markup=button) diff --git a/nana/assistant/heroku.py b/nana/assistant/heroku.py new file mode 100644 index 00000000..eae76542 --- /dev/null +++ b/nana/assistant/heroku.py @@ -0,0 +1,63 @@ +import heroku3 +from pyrogram import errors, InlineKeyboardButton, InlineKeyboardMarkup, Filters + +from nana import setbot, HEROKU_API +from nana.assistant.input_handler import temp_vars +from nana.assistant.repo_changer import dynamic_data_filter +from nana.assistant.settings import get_text_settings, get_button_settings + + +@setbot.on_callback_query(dynamic_data_filter("restart_heroku")) +async def reboot_heroku(client, query): + text = await get_text_settings() + button = await get_button_settings() + if HEROKU_API is not None: + text += "\nPlease wait..." + try: + await query.message.edit_text(text, reply_markup=button) + except errors.exceptions.bad_request_400.MessageNotModified: + pass + await client.answer_callback_query(query.id, "Please wait for Heroku App restarting...") + heroku = heroku3.from_key(HEROKU_API) + heroku_applications = heroku.apps() + if len(heroku_applications) >= 1: + heroku_app = heroku_applications[0] + heroku_app.restart() + else: + text += "No heroku application found, but a key given? 😕" + try: + await query.message.edit_text(text, reply_markup=button) + except errors.exceptions.bad_request_400.MessageNotModified: + pass + await client.answer_callback_query(query.id, "No heroku application found, but a key given?") + + +@setbot.on_callback_query(dynamic_data_filter("heroku_vars")) +async def vars_heroku(_client, query): + text = "**⚙️ Welcome to Heroku Vars Settings!**\n" \ + "`Setting your heroku config vars here!`\n" + list_button = [[InlineKeyboardButton("⬅ back️", callback_data="back"), + InlineKeyboardButton("➕ add️", callback_data="add_vars")]] + if HEROKU_API: + heroku = heroku3.from_key(HEROKU_API) + heroku_applications = heroku.apps() + if len(heroku_applications) >= 1: + app = heroku_applications[0] + config = app.config() + # if config["api_id"]: + # list_button.insert(0, [InlineKeyboardButton("api_id✅", callback_data="api_id")]) + # else: + # list_button.insert(0, [InlineKeyboardButton("api_id🚫", callback_data="api_id")]) + configdict = config.to_dict() + for x, _ in configdict.items(): + list_button.insert(0, [InlineKeyboardButton("{}✅".format(x), callback_data="tes")]) + button = InlineKeyboardMarkup(list_button) + await query.message.edit_text(text, reply_markup=button) + + +@setbot.on_callback_query(Filters.regex("^add_vars")) +async def addvars(client, query): + temp_vars.append(True) + await query.message.edit_text("Send Name Variable :") + + diff --git a/nana/assistant/input_handler.py b/nana/assistant/input_handler.py new file mode 100644 index 00000000..703c4c0d --- /dev/null +++ b/nana/assistant/input_handler.py @@ -0,0 +1,76 @@ +import heroku3 +from pyrogram import Filters, InlineKeyboardButton, InlineKeyboardMarkup + +from nana import AdminSettings, setbot, Owner, HEROKU_API +from nana.assistant.database.custom_theme_db import add_custom_theme + +temp_input = False +theme_format = [] +temp_query = {} +temp_vars = [] + + +@setbot.on_message(Filters.user(AdminSettings)) +async def theme_input_handlers(client, message): + global temp_input, theme_format, temp_query, temp_vars + if temp_input: + text = "**⚙️Add Theme **\n" + if len(theme_format) >= 1: + try: + cap = "Image has been set!" + await setbot.send_photo(Owner, message.text, caption=cap) + theme_format.append(message.text) + except: + text += "**Wrong URL image !** \n" + else: + theme_format.append(message.text) + + if len(theme_format) == 1: + text += f"Set URL image for welcome image \n" + elif len(theme_format) == 2: + text += f"Set URL image for start image \n" + elif len(theme_format) == 3: + text += f"Set URL image for settings image \n" + elif len(theme_format) == 4: + text += f"Set URL image for stats image \n" + elif len(theme_format) == 5: + text += f"Custom Theme has successfully added \n" + temp_input = False + await add_custom_theme(theme_format[0], theme_format[1], theme_format[2], theme_format[3], theme_format[4]) + theme_format = [] + list_button = [[InlineKeyboardButton("❌ Cancel", callback_data="cancel")]] + button = InlineKeyboardMarkup(list_button) + await client.send_message(Owner, text, reply_markup=button) + elif len(temp_vars) >= 1: + temp_vars.append(message.text) + text = "**⚙️Add heroku config vars **\n" + if len(temp_vars) == 2: + text += "`Send Value `" + list_button = [[InlineKeyboardButton("❌ Cancel", callback_data="cancel")]] + button = InlineKeyboardMarkup(list_button) + await client.send_message(Owner, text, reply_markup=button) + if len(temp_vars) == 3: + await config_vars(temp_vars[1], temp_vars[2]) + text += "`Successfully added config vars! `" + await client.send_message(Owner, text) + temp_vars = [] + + +@setbot.on_callback_query(Filters.regex("^cancel")) +async def cancel_input(client, query): + global temp_input, theme_format, temp_vars + temp_input = False + theme_format = [] + temp_vars = [] + await client.answer_callback_query(query.id, "Operation Canceled!") + await query.message.delete() + + +async def config_vars(name, value): + if HEROKU_API: + heroku = heroku3.from_key(HEROKU_API) + heroku_applications = heroku.apps() + if len(heroku_applications) >= 1: + app = heroku_applications[0] + config = app.config() + config[name] = value diff --git a/nana/assistant/settings.py b/nana/assistant/settings.py index 41455e9b..d32959a3 100644 --- a/nana/assistant/settings.py +++ b/nana/assistant/settings.py @@ -1,11 +1,9 @@ from platform import python_version -import heroku3 from pyrogram import Filters, InlineKeyboardButton, InlineKeyboardMarkup, errors -from .theme.theme_helper import get_theme, theme_list -from nana import AdminSettings, setbot, app, USERBOT_VERSION, ASSISTANT_VERSION, DB_AVAILABLE, HEROKU_API, Owner +from .theme.theme_helper import get_theme +from nana import setbot, app, USERBOT_VERSION, ASSISTANT_VERSION, DB_AVAILABLE, HEROKU_API, Owner from nana.__main__ import reload_userbot, restart_all -from nana.assistant.__main__ import dynamic_data_filter async def is_userbot_run(): @@ -44,15 +42,7 @@ async def get_button_settings(): return InlineKeyboardMarkup(list_button) -@setbot.on_message(Filters.user(AdminSettings) & Filters.command(["settings"]) & Filters.private) -async def settings(_client, message): - text = await get_text_settings() - button = await get_button_settings() - img = await get_theme("settings") - await setbot.send_photo(Owner, img, caption=text, reply_markup=button) - - -@setbot.on_callback_query(dynamic_data_filter("toggle_startbot")) +@setbot.on_callback_query(Filters.regex("^toggle_startbot")) async def start_stop_bot(client, query): try: await app.stop() @@ -77,7 +67,7 @@ async def start_stop_bot(client, query): await client.answer_callback_query(query.id, "Bot was stopped!") -@setbot.on_callback_query(dynamic_data_filter("restart_bot")) +@setbot.on_callback_query(Filters.regex("^restart_bot")) async def reboot_bot(client, query): await restart_all() text = await get_text_settings() @@ -89,57 +79,10 @@ async def reboot_bot(client, query): pass await client.answer_callback_query(query.id, "Please wait for bot restarting...") - -@setbot.on_callback_query(dynamic_data_filter("restart_heroku")) -async def reboot_heroku(client, query): - text = await get_text_settings() - button = await get_button_settings() - if HEROKU_API is not None: - text += "\nPlease wait..." - try: - await query.message.edit_text(text, reply_markup=button) - except errors.exceptions.bad_request_400.MessageNotModified: - pass - await client.answer_callback_query(query.id, "Please wait for Heroku App restarting...") - heroku = heroku3.from_key(HEROKU_API) - heroku_applications = heroku.apps() - if len(heroku_applications) >= 1: - heroku_app = heroku_applications[0] - heroku_app.restart() - else: - text += "No heroku application found, but a key given? 😕" - try: - await query.message.edit_text(text, reply_markup=button) - except errors.exceptions.bad_request_400.MessageNotModified: - pass - await client.answer_callback_query(query.id, "No heroku application found, but a key given?") - - -@setbot.on_callback_query(dynamic_data_filter("heroku_vars")) -async def vars_heroku(_client, query): - text = "**⚙️ Welcome to Heroku Vars Settings!**\n" \ - "`Setting your heroku config vars here!`\n" - list_button = [[InlineKeyboardButton("⬅ back️", callback_data="back"), - InlineKeyboardButton("➕ add️", callback_data="add_vars")]] - if HEROKU_API: - heroku = heroku3.from_key(HEROKU_API) - heroku_applications = heroku.apps() - if len(heroku_applications) >= 1: - app = heroku_applications[0] - config = app.config() - # if config["api_id"]: - # list_button.insert(0, [InlineKeyboardButton("api_id✅", callback_data="api_id")]) - # else: - # list_button.insert(0, [InlineKeyboardButton("api_id🚫", callback_data="api_id")]) - configdict = config.to_dict() - for x, _ in configdict.items(): - list_button.insert(0, [InlineKeyboardButton("{}✅".format(x), callback_data="tes")]) - button = InlineKeyboardMarkup(list_button) - await query.message.edit_text(text, reply_markup=button) - # Back button -@setbot.on_callback_query(dynamic_data_filter("back")) + +@setbot.on_callback_query(Filters.regex("^back")) async def back(_client, message): text = await get_text_settings() button = await get_button_settings() diff --git a/nana/assistant/theme_modules.py b/nana/assistant/theme_modules.py index 7aa38f8d..c204a924 100644 --- a/nana/assistant/theme_modules.py +++ b/nana/assistant/theme_modules.py @@ -4,10 +4,7 @@ from .database.theme_db import set_name_theme_set from .theme.theme_helper import theme_list, name_theme -temp_input = False -theme_format = [] -temp_query = {} -temp_client = "" + @setbot.on_callback_query(Filters.regex("^theme")) @@ -50,48 +47,3 @@ async def addtheme(client, query): list_button = [[InlineKeyboardButton("❌ Cancel", callback_data="cancel")]] button = InlineKeyboardMarkup(list_button) await client.send_message(Owner, text, reply_markup=button) - - -@setbot.on_callback_query(Filters.regex("^cancel")) -async def addtheme(client, query): - global temp_input, theme_format - if temp_input: - temp_input = False - theme_format = [] - await client.answer_callback_query(query.id, "Operation Canceled!") - await query.message.delete() - - -@setbot.on_message(Filters.user(AdminSettings)) -async def theme_input_handlers(client, message): - global temp_input, theme_format, temp_query, temp_client - if temp_input: - text = "**⚙️Add Theme **\n" - if len(theme_format) >= 1: - try: - cap = "Image has been set!" - await setbot.send_photo(Owner, message.text, caption=cap) - theme_format.append(message.text) - except: - text += "**Wrong URL image !** \n" - else: - theme_format.append(message.text) - - if len(theme_format) == 1: - text += f"Set URL image for welcome image \n" - elif len(theme_format) == 2: - text += f"Set URL image for start image \n" - elif len(theme_format) == 3: - text += f"Set URL image for settings image \n" - elif len(theme_format) == 4: - text += f"Set URL image for stats image \n" - elif len(theme_format) == 5: - text += f"Custom Theme has successfully added \n" - temp_input = False - await add_custom_theme(theme_format[0], theme_format[1], theme_format[2], theme_format[3], theme_format[4]) - theme_format = [] - list_button = [[InlineKeyboardButton("❌ Cancel", callback_data="cancel")]] - button = InlineKeyboardMarkup(list_button) - await client.send_message(Owner, text, reply_markup=button) - -