Music Finder Bot
What it does A Telegram bot that lets users search for a song or artist, choose a result from inline buttons, and receive the selected track as Telegram audio that plays in chat.
Key features
- /play searches a provider and shows 5–10 results with a clean, mobile-friendly inline keyboard.
- Tap a result to download and send audio with title and performer metadata.
- Status updates: Searching… Downloading… Sending audio…
- Long-term memory stored in MongoDB when MONGODB_URI is set, with in-memory fallback.
- Optional AI via CookMyBots AI gateway to normalize queries and produce friendly short messages (no direct OpenAI calls).
- Production-ready long polling on Render using @grammyjs/runner, with webhook clearing and 409 conflict backoff.
Architecture overview
- src/index.js boots, clears webhook, starts polling with runner.
- src/bot.js wires middleware, commands, and callback query handling.
- src/commands/*.js implement /start /help /play /reset.
- src/services/musicProvider.js is the pluggable provider layer.
- src/lib/memoryStore.js persists conversation turns to MongoDB (or in-memory fallback).
- src/lib/sessionStore.js persists pending search results for button selections.
- src/lib/ai.js wraps CookMyBots AI gateway.
Setup Prerequisites
- Node.js 18+
- A Telegram bot token from @BotFather
- Optional: MongoDB connection string
- Optional: CookMyBots AI gateway env vars (usually provided by CookMyBots)
Install
- npm run install:root
Configure Copy .env.sample to .env and fill at minimum:
- TELEGRAM_BOT_TOKEN
Optional but recommended:
- MONGODB_URI for long-term memory and restart-safe selection handling
- COOKMYBOTS_AI_ENDPOINT and COOKMYBOTS_AI_KEY for AI query normalization and friendly responses
Run locally
- npm run dev
Build and run
- npm run build
- npm start
Commands /start Shows a short welcome and how to use /play.
/help Shows examples and tips.
/play Searches tracks and shows selectable results.
/reset Clears your stored memory and any pending selection session.
Integrations Music provider Default provider is iTunes Search (public catalog). It returns 30s previews that are legal and playable. If your selected provider cannot return a downloadable audio file, the bot will fall back to sending a link.
AI gateway (optional) The bot calls CookMyBots AI gateway endpoints:
- POST {COOKMYBOTS_AI_ENDPOINT}/chat Used for query normalization and short friendly messages.
Database Collections
-
memory_messages Stores conversation turns: userId (string), platform (telegram), chatId (string), role (user|assistant), text (string), ts (date)
-
music_sessions Stores pending search results: sessionId, userId, chatId, tracks[], messageId, expiresAt, createdAt, updatedAt
Indexes The bot creates safe indexes for memory_messages and music_sessions (including a TTL on expiresAt).
Deployment on Render
- Use a Background Worker (recommended) or Web Service.
- Set TELEGRAM_BOT_TOKEN env var.
- Add MONGODB_URI if you want long-term memory and restart-safe sessions.
- Build command: npm run build
- Start command: npm start
Troubleshooting
-
Bot is not responding Check logs for TELEGRAM_BOT_TOKEN set: false or 409 Conflict. The bot auto-backs off and retries.
-
/play says provider not configured Set MUSIC_PROVIDER (defaults to itunes). If you changed providers, you may need MUSIC_API_BASE_URL and MUSIC_API_KEY.
-
Audio fails to send Large files can exceed Telegram limits. The bot tries sendAudio, then sendDocument, then falls back to a URL.
Extensibility To add a new provider, extend src/services/musicProvider.js with a new provider implementation that matches:
- search(query, limit)
- getDownload(track) Then set MUSIC_PROVIDER to your provider name.