Auto-update existing Telegram bot during upgrade

This commit is contained in:
Виталий Литвинов
2026-04-26 10:51:30 +03:00
parent ffea9f5d77
commit 7bb88f2c24
2 changed files with 44 additions and 0 deletions

View File

@@ -963,6 +963,31 @@ bot_service_status() {
fi
}
auto_update_bot_if_possible() {
[ -d "$SCRIPT_DIR/gotelegram-bot" ] || return 0
[ "$(bot_service_status)" = "not_installed" ] && return 0
[ -f "$BOT_DIR/.env" ] || return 0
local needs_update=0
[ -f "$SCRIPT_DIR/gotelegram-bot/bot.py" ] && \
! cmp -s "$SCRIPT_DIR/gotelegram-bot/bot.py" "$BOT_DIR/bot.py" && needs_update=1
[ -f "$SCRIPT_DIR/gotelegram-bot/i18n.py" ] && \
! cmp -s "$SCRIPT_DIR/gotelegram-bot/i18n.py" "$BOT_DIR/i18n.py" && needs_update=1
[ -f "$SCRIPT_DIR/gotelegram-bot/requirements.txt" ] && \
! cmp -s "$SCRIPT_DIR/gotelegram-bot/requirements.txt" "$BOT_DIR/requirements.txt" && needs_update=1
local lang_file lang_name
for lang_file in "$SCRIPT_DIR"/gotelegram-bot/lang/*.json; do
[ -e "$lang_file" ] || continue
lang_name=$(basename "$lang_file")
! cmp -s "$lang_file" "$BOT_DIR/lang/$lang_name" && needs_update=1
done
[ "$needs_update" = "1" ] || return 0
bot_install >/dev/null 2>&1 || \
log_warning "Telegram bot auto-update failed; run menu 12 → Telegram-bot → Install/update"
}
menu_bot() {
local st
st=$(bot_service_status)
@@ -1788,6 +1813,7 @@ main() {
fi
auto_migrate_legacy_state || true
auto_update_bot_if_possible || true
auto_install_admin_web_if_possible || true
# First-run language picker (before banner so banner appears in chosen lang)

View File

@@ -7,6 +7,7 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
BOT_PATH = ROOT / "gotelegram-bot" / "bot.py"
CATALOG_PATH = ROOT / "templates_catalog.json"
INSTALL_PATH = ROOT / "install.sh"
class BotFeatureTests(unittest.TestCase):
@@ -79,6 +80,23 @@ class BotFeatureTests(unittest.TestCase):
self.assertIn("await get_telemt_version()", body)
self.assertNotIn('sh("telemt", "--version")', body)
def test_installer_auto_updates_existing_bot_files(self):
source = INSTALL_PATH.read_text(encoding="utf-8")
auto_body = re.search(
r"auto_update_bot_if_possible\(\).*?(?=\n\n[A-Za-z0-9_]+\(\) |\n\n#)",
source,
flags=re.S,
)
self.assertIsNotNone(auto_body)
auto_text = auto_body.group(0)
self.assertIn('bot_service_status', auto_text)
self.assertIn('bot_install', auto_text)
self.assertIn('cmp -s "$SCRIPT_DIR/gotelegram-bot/bot.py" "$BOT_DIR/bot.py"', auto_text)
self.assertIn('cmp -s "$SCRIPT_DIR/gotelegram-bot/i18n.py" "$BOT_DIR/i18n.py"', auto_text)
self.assertIn('cmp -s "$SCRIPT_DIR/gotelegram-bot/requirements.txt" "$BOT_DIR/requirements.txt"', auto_text)
self.assertIn('auto_migrate_legacy_state || true\n auto_update_bot_if_possible || true\n auto_install_admin_web_if_possible || true', source)
if __name__ == "__main__":
unittest.main()