Skip to content

Commit

Permalink
Feature : heroku add configvars @ /settings -> heroku config vars
Browse files Browse the repository at this point in the history
  • Loading branch information
himitsu committed Sep 24, 2020
1 parent 695e733 commit 4eacf58
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 126 deletions.
21 changes: 7 additions & 14 deletions nana/assistant/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
63 changes: 63 additions & 0 deletions nana/assistant/heroku.py
Original file line number Diff line number Diff line change
@@ -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 :")


76 changes: 76 additions & 0 deletions nana/assistant/input_handler.py
Original file line number Diff line number Diff line change
@@ -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
69 changes: 6 additions & 63 deletions nana/assistant/settings.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
50 changes: 1 addition & 49 deletions nana/assistant/theme_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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)


0 comments on commit 4eacf58

Please sign in to comment.