mirror of https://github.com/coddrago/Heroku
Properly save Heroku session
parent
925b1caedd
commit
ed6d34cb82
|
@ -23,15 +23,10 @@ def publish(
|
|||
"""Push to heroku"""
|
||||
logging.debug("Configuring heroku...")
|
||||
|
||||
data = json.dumps(
|
||||
{
|
||||
getattr(client, "phone", ""): StringSession.save(client.session)
|
||||
for client in clients
|
||||
}
|
||||
)
|
||||
app, config = get_app(data, key, api_token, create_new, full_match)
|
||||
app, config = get_app(key, api_token, create_new)
|
||||
|
||||
config["authorization_strings"] = data
|
||||
# Will be configured later in app
|
||||
config["authorization_strings"] = None
|
||||
config["heroku_api_token"] = key
|
||||
|
||||
if api_token is not None:
|
||||
|
@ -43,6 +38,9 @@ def publish(
|
|||
"https://github.com/heroku/heroku-buildpack-python",
|
||||
"https://github.com/hikariatama/heroku-buildpack",
|
||||
"https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest",
|
||||
"https://github.com/heroku/heroku-buildpack-apt",
|
||||
"https://github.com/DuckyTeam/heroku-buildpack-imagemagick",
|
||||
"https://github.com/jontewks/puppeteer-heroku-buildpack",
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -61,11 +59,9 @@ def publish(
|
|||
|
||||
|
||||
def get_app(
|
||||
authorization_strings,
|
||||
key,
|
||||
api_token=None,
|
||||
create_new=True,
|
||||
full_match=False,
|
||||
):
|
||||
heroku = heroku3.from_key(key)
|
||||
app = None
|
||||
|
@ -73,15 +69,9 @@ def get_app(
|
|||
for poss_app in heroku.apps():
|
||||
config = poss_app.config()
|
||||
|
||||
if "authorization_strings" not in config:
|
||||
continue
|
||||
|
||||
if api_token is None or (
|
||||
config["api_id"] == api_token.ID and config["api_hash"] == api_token.HASH
|
||||
):
|
||||
if full_match and config["authorization_strings"] != authorization_strings:
|
||||
continue
|
||||
|
||||
app = poss_app
|
||||
break
|
||||
|
||||
|
@ -90,7 +80,12 @@ def get_app(
|
|||
logging.error("%r", {app: repr(app.config) for app in heroku.apps()})
|
||||
raise RuntimeError("Could not identify app!")
|
||||
|
||||
app = heroku.create_app(stack_id_or_name="heroku-20", region_id_or_name="eu")
|
||||
app = heroku.create_app(
|
||||
name=f"hikka-{utils.rand(8)}",
|
||||
stack_id_or_name="heroku-20",
|
||||
region_id_or_name="eu",
|
||||
)
|
||||
|
||||
config = app.config()
|
||||
|
||||
return app, config
|
||||
|
|
|
@ -290,6 +290,24 @@ class Hikka:
|
|||
self._get_api_token()
|
||||
self._get_proxy()
|
||||
|
||||
if "DYNO" in os.environ and "HIKKA_SESSION" in os.environ:
|
||||
try:
|
||||
client = TelegramClient(
|
||||
StringSession(os.environ.get("HIKKA_SESSION")),
|
||||
self.api_token.ID,
|
||||
self.api_token.HASH,
|
||||
connection=self.conn,
|
||||
proxy=self.proxy,
|
||||
connection_retries=None,
|
||||
device_model="Hikka",
|
||||
)
|
||||
|
||||
client.start()
|
||||
install_entity_caching(client)
|
||||
self.clients += [client]
|
||||
except Exception:
|
||||
logging.exception("Failed to load session")
|
||||
|
||||
def _get_proxy(self):
|
||||
"""
|
||||
Get proxy tuple from --proxy-host, --proxy-port and --proxy-secret
|
||||
|
@ -341,6 +359,12 @@ class Hikka:
|
|||
)
|
||||
)
|
||||
|
||||
if "DYNO" in os.environ:
|
||||
try:
|
||||
phones.update(json.loads(os.environ.get("HIKKA_PHONES")))
|
||||
except (KeyError, json.JSONDecodeError):
|
||||
pass
|
||||
|
||||
self.phones = phones
|
||||
|
||||
def _get_api_token(self):
|
||||
|
@ -405,12 +429,15 @@ class Hikka:
|
|||
id_ = (await client.get_me()).id
|
||||
client._tg_id = id_
|
||||
|
||||
session = SQLiteSession(
|
||||
os.path.join(
|
||||
self.arguments.data_root or BASE_DIR,
|
||||
f"hikka-{id_}",
|
||||
if self.arguments.heroku:
|
||||
session = StringSession()
|
||||
else:
|
||||
session = SQLiteSession(
|
||||
os.path.join(
|
||||
self.arguments.data_root or BASE_DIR,
|
||||
f"hikka-{id_}",
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
session.set_dc(
|
||||
client.session.dc_id,
|
||||
|
@ -418,7 +445,12 @@ class Hikka:
|
|||
client.session.port,
|
||||
)
|
||||
session.auth_key = client.session.auth_key
|
||||
session.save()
|
||||
|
||||
if "DYNO" not in os.environ:
|
||||
session.save()
|
||||
else:
|
||||
os.environ["HIKKA_SESSION"] = session.save()
|
||||
|
||||
client.session = session
|
||||
# Set db attribute to this client in order to save
|
||||
# custom bot nickname from web
|
||||
|
@ -689,7 +721,7 @@ class Hikka:
|
|||
or not self._init_clients() # Attempt to read sessions from env
|
||||
) and not self._initial_setup(): # Otherwise attempt to run setup
|
||||
return
|
||||
|
||||
|
||||
if self.arguments.heroku:
|
||||
if isinstance(self.arguments.heroku, str):
|
||||
key = self.arguments.heroku
|
||||
|
@ -699,10 +731,7 @@ class Hikka:
|
|||
).strip()
|
||||
|
||||
app = heroku.publish(self.clients, key, self.api_token)
|
||||
print(
|
||||
"Installed to heroku successfully!\n"
|
||||
"🎉 {}".format(app.web_url)
|
||||
)
|
||||
print("Installed to heroku successfully!\n" "🎉 {}".format(app.web_url))
|
||||
|
||||
if self.web:
|
||||
self.web.redirect_url = app.web_url
|
||||
|
|
Loading…
Reference in New Issue