soxogvv commited on
Commit
6f44894
Β·
verified Β·
1 Parent(s): e314f9f

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +47 -5
app.py CHANGED
@@ -959,6 +959,45 @@ def sync_ig_cookies_to_db() -> None:
959
  logger.warning(f"[IG] could not sync cookies to DB: {e}")
960
 
961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
962
  # ───────────────────────────── SEND EDITED VIDEO β†’ INSTAGRAM TELEGRAM BOT
963
  # Instagram blocks this (HF) server's IP, so the actual Reel upload is done by the
964
  # companion Telegram bot in koyeb/. This app just DELIVERS the finished video to
@@ -971,10 +1010,12 @@ def sync_ig_cookies_to_db() -> None:
971
  # IG_BOT_TARGET env overrides it.
972
 
973
  def get_bot_target():
974
- """Resolve the Telegram bot the edited video is sent to (MongoDB, env fallback)."""
 
 
975
  override = os.getenv("IG_BOT_TARGET", "").strip()
976
  if override:
977
- return override
978
  try:
979
  doc = config.find_one({"_id": "telegram"}) or {}
980
  except Exception as e:
@@ -1208,9 +1249,10 @@ def auto_loop():
1208
  "makes YouTube use the higher-quality VP9 codec to minimise it."
1209
  )
1210
 
1211
- # Instagram is uploaded by the companion Telegram bot (koyeb/). Mirror the
1212
- # HF env cookies into MongoDB so the bot always has the latest, and log the
1213
- # bot we deliver videos to.
 
1214
  sync_ig_cookies_to_db()
1215
  logger.info(f"[ℹ️] Instagram via Telegram bot: {get_bot_target() or '(not configured)'}")
1216
 
 
959
  logger.warning(f"[IG] could not sync cookies to DB: {e}")
960
 
961
 
962
+ def _parse_id_csv(raw: str) -> list:
963
+ """Parse a CSV of Telegram user ids into a list of ints (ignores junk)."""
964
+ ids = []
965
+ for tok in (raw or "").replace(" ", "").split(","):
966
+ if not tok:
967
+ continue
968
+ try:
969
+ ids.append(int(tok))
970
+ except ValueError:
971
+ logger.warning(f"[bot] ignoring non-numeric authorized id '{tok}'")
972
+ return ids
973
+
974
+
975
+ def sync_telegram_config_to_db() -> None:
976
+ """Store the HF env Telegram settings into MongoDB (config._id='telegram'):
977
+ β€’ BOT_TOKEN β†’ bot_token (the bot's login token)
978
+ β€’ USER_ID β†’ authorized_user_ids (only this user may send videos to the bot)
979
+ The Koyeb bot logs in with the token and writes back its own bot_id/bot_username,
980
+ so HF learns where to send. Only provided fields are written (no clobbering)."""
981
+ token = os.getenv("BOT_TOKEN", "").strip()
982
+ # USER_ID is the new HF variable; AUTHORIZED_USER_IDS kept as an alias.
983
+ ids = _parse_id_csv(os.getenv("USER_ID", "") or os.getenv("AUTHORIZED_USER_IDS", ""))
984
+
985
+ fields = {}
986
+ if token:
987
+ fields["bot_token"] = token
988
+ if ids:
989
+ fields["authorized_user_ids"] = ids
990
+
991
+ if not fields:
992
+ logger.info("[bot] no BOT_TOKEN/USER_ID on HF β€” leaving MongoDB telegram config as-is")
993
+ return
994
+ try:
995
+ config.update_one({"_id": "telegram"}, {"$set": fields}, upsert=True)
996
+ logger.info(f"[bot] βœ… stored Telegram config β†’ MongoDB ({', '.join(fields.keys())})")
997
+ except Exception as e:
998
+ logger.warning(f"[bot] could not store Telegram config in DB: {e}")
999
+
1000
+
1001
  # ───────────────────────────── SEND EDITED VIDEO β†’ INSTAGRAM TELEGRAM BOT
1002
  # Instagram blocks this (HF) server's IP, so the actual Reel upload is done by the
1003
  # companion Telegram bot in koyeb/. This app just DELIVERS the finished video to
 
1010
  # IG_BOT_TARGET env overrides it.
1011
 
1012
  def get_bot_target():
1013
+ """Resolve the Telegram bot the edited video is sent to. The Koyeb bot writes
1014
+ its own bot_username/bot_id into MongoDB after it logs in, so HF reads it from
1015
+ there. IG_BOT_TARGET env is a manual override (e.g. before Koyeb's first run)."""
1016
  override = os.getenv("IG_BOT_TARGET", "").strip()
1017
  if override:
1018
+ return override.lstrip("@")
1019
  try:
1020
  doc = config.find_one({"_id": "telegram"}) or {}
1021
  except Exception as e:
 
1249
  "makes YouTube use the higher-quality VP9 codec to minimise it."
1250
  )
1251
 
1252
+ # Instagram is uploaded by the companion Telegram bot (koyeb/). Mirror ALL
1253
+ # HF env config into MongoDB (cookies + bot token/username/authorized ids) so
1254
+ # the bot is set up entirely from Hugging Face β€” Koyeb only needs MONGO_URI.
1255
+ sync_telegram_config_to_db()
1256
  sync_ig_cookies_to_db()
1257
  logger.info(f"[ℹ️] Instagram via Telegram bot: {get_bot_target() or '(not configured)'}")
1258