import logging from bot.telegram_api import send_message from db import models as dbm log = logging.getLogger("onboarding") async def handle_start(chat_id, user_id, username=None): user = dbm.get_user(user_id) if user and dbm.user_has_valid_token(user_id): await send_message( chat_id, "**Welcome back!**\n\n" "Send any message to chat with the model.\n" "Use /help for commands.", ) return await send_message( chat_id, "**Welcome.**\n\n" "This bot requires an access token.\n\n" "Send `/token YOUR_TOKEN_HERE` to activate.\n" "If you don't have one, ask the admin.", ) async def handle_token_command(chat_id, user_id, token: str): if not token: await send_message(chat_id, "Usage: `/token YOUR_TOKEN_HERE`") return ok = dbm.bind_token(user_id, token) if ok: await send_message( chat_id, "**Token accepted.**\n\nYou can now send messages.", ) else: await send_message( chat_id, "**Invalid, revoked, or already-used token.**", ) async def handle_reset(chat_id, user_id): # 1. Wipe live context (SQLite) so the model starts fresh dbm.clear_history(user_id) # 2. Close the current session file. Next message opens a NEW file # at /data/chat_logs/{user_id}/{timestamp}.md โ€” nothing is deleted. dbm.archive_close_session(user_id) await send_message( chat_id, "๐Ÿงน **Conversation memory cleared.**\n" "_A fresh session will start on your next message._", ) async def handle_help(chat_id): await send_message( chat_id, "**Commands**\n\n" "/start โ€” welcome / onboarding\n" "/token VALUE โ€” activate or replace token\n" "/reset โ€” start a fresh session\n" "/stats โ€” your usage stats\n" "/help โ€” this message", ) async def handle_stats(chat_id, user_id): user = dbm.get_user(user_id) or {} sessions = dbm.archive_list(user_id) await send_message( chat_id, f"**Your stats**\n\n" f"Total turns: {user.get('total_turns', 0)}\n" f"Sessions: {len(sessions)}\n" f"Last seen: {user.get('last_seen', 'n/a')}", )