ops: installer asks for the subscription and bot token; first /start claims the bot

- install.sh prompts (hidden) for the VLESS subscription URL and the Telegram token on
  the first run, and picks Whisper base on VMs under 3.5 GB RAM.
- The owner chat no longer has to be looked up: whoever sends /start first becomes the
  owner (saved in the settings table); OWNER_CHAT_ID still overrides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-10 02:55:02 +03:00
parent 80990bbbcd
commit 14c1f740d9
4 changed files with 59 additions and 18 deletions
+27 -8
View File
@@ -49,14 +49,32 @@ def locked(fn, *a, **kw):
# MARK: - Telegram
def owner() -> int:
"""OWNER_CHAT_ID, or whoever sent /start first (saved in the database)."""
if CFG["owner"]:
return CFG["owner"]
with lock:
row = ops.con.execute("SELECT value FROM settings WHERE key = 'owner'").fetchone()
return int(row["value"]) if row else 0
def claim_owner(chat_id: int) -> bool:
with lock:
if CFG["owner"] or ops.con.execute("SELECT 1 FROM settings WHERE key = 'owner'").fetchone():
return False
ops.con.execute("INSERT INTO settings (key, value) VALUES ('owner', ?)", (str(chat_id),))
log.info("owner claimed: %s", chat_id)
return True
def owner_only(handler):
async def wrapped(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
if not chat or chat.id != CFG["owner"]:
if chat and update.message and update.message.text == "/start" and claim_owner(chat.id):
await update.message.reply_text("Готово: этот чат — владелец бота. Чужим он отвечать не будет.")
if not chat or chat.id != owner():
if update.message and update.message.text == "/start":
await update.message.reply_text(
f"Это личный бот. Твой chat id: {chat.id if chat else '?'} — "
"впиши его в OWNER_CHAT_ID на сервере и перезапусти службу.")
await update.message.reply_text("Это личный бот.")
return
await handler(update, ctx)
return wrapped
@@ -151,7 +169,7 @@ def reminder_buttons(it: dict) -> InlineKeyboardMarkup:
async def on_button(update: Update, ctx):
q = update.callback_query
if not q or q.message.chat.id != CFG["owner"]:
if not q or q.message.chat.id != owner():
return
action, kind, ref = q.data.split(":")
ref = int(ref)
@@ -183,7 +201,8 @@ async def reminder_loop(bot):
while True:
try:
now = datetime.now(ops.tz)
if CFG["owner"] and CFG["digest_hour"] <= now.hour < CFG["quiet_from"]:
chat = owner()
if chat and CFG["digest_hour"] <= now.hour < CFG["quiet_from"]:
with lock:
digest_due = not ops.con.execute(
"SELECT 1 FROM reminders_sent WHERE kind = 'digest' AND day = ?",
@@ -191,12 +210,12 @@ async def reminder_loop(bot):
agenda = ops.agenda() if digest_due else None
items = ops.due_reminders()
if digest_due:
await bot.send_message(CFG["owner"], "☀️ Доброе утро! " + fmt.agenda(agenda),
await bot.send_message(chat, "☀️ Доброе утро! " + fmt.agenda(agenda),
parse_mode=ParseMode.HTML)
locked(ops.mark_reminded, "digest", 0)
today = ops.today()
for it in items:
await bot.send_message(CFG["owner"], "Напоминание: " + fmt.item_line(it, today),
await bot.send_message(chat, "Напоминание: " + fmt.item_line(it, today),
parse_mode=ParseMode.HTML, reply_markup=reminder_buttons(it))
locked(ops.mark_reminded, it["kind"], it["id"])
except Exception:
+4
View File
@@ -52,6 +52,10 @@ CREATE TABLE IF NOT EXISTS routine_log (
done_on TEXT NOT NULL,
note TEXT NOT NULL DEFAULT ''
);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS reminders_sent (
kind TEXT NOT NULL, -- task | sub | routine | digest
ref INTEGER NOT NULL,