Welcome Contributors!
Thank you for your interest in improving the Price Tracker Bot! This guide will help you get started with contributing.Code Structure
Understanding the codebase architecture will help you make effective contributions.File Organization
The bot is a single-file application:Code Sections
The code is organized into logical sections:Config (Lines 10-17)
Config (Lines 10-17)
Purpose: Environment setup and bot initializationKey constants:
TELEGRAM_TOKEN- Bot authenticationbot- Main Telegram bot instance
Persistence (Lines 19-50)
Persistence (Lines 19-50)
Purpose: Data storage and retrievalKey components:
DATA_FILE = "prices.json"- Storage filepriceData- Object storing products by URLchats- Set of registered chat IDsloadData()- Reads from disk on startupsaveData()- Writes to disk after changes
Utils (Lines 52-69)
Utils (Lines 52-69)
Purpose: Helper functionsFunctions:
sanitizeAmazonURL()- Removes query params from URLsescapeMD()- Escapes Markdown special charactersHISTORY_LIMIT = 120- Max history entries per product
Scraper (Lines 71-136)
Scraper (Lines 71-136)
Purpose: Web scraping logicMain function:
scrapeProduct(page, url)Process:- Navigate to product page (60s timeout)
- Wait 800ms for lazy-loaded content
- Try multiple CSS selectors for:
- Title (6 selectors)
- Price (6 selectors)
- Image (4 selectors)
- Parse price string to number
- Return product data or error object
Price Check Logic (Lines 138-250)
Price Check Logic (Lines 138-250)
Purpose: Automated price monitoringMain function:
checkPrices()Process:- Launch browser (shared page for efficiency)
- Iterate through all products
- Scrape current price
- Compare with stored price
- Detect drops:
if (scraped.price < originalPrice) - Update product data
- Record history (limit to 120 entries)
- Close browser
- Save data
- Send notifications to all chats
- Logs errors but continues checking other products
- Updates
lastCheckedeven on failure - Collects errors for summary report
Chart Generation (Lines 252-333)
Chart Generation (Lines 252-333)
Purpose: Visual price historyFunctions:
generateChartBuffer()- Creates chart image using Playwright + Chart.jssendPriceChart()- Sends chart to user
- Generate HTML with Chart.js
- Load in headless browser
- Wait for render (1s)
- Screenshot canvas element
- Return as buffer
- Send to Telegram as photo
Commands (Lines 335-623)
Commands (Lines 335-623)
Purpose: User interaction handlersCommand handlers:
/start(line 359) - Welcome message/help(line 384) - Command reference/add(line 398) - Add product to tracking/check(line 470) - Force price check/list(line 489) - Show all products/stats(line 492) - Show statistics/remove(line 520) - Delete product/edit(line 545) - Update product URL/chart(line 613) - Generate price graph
bot.onText() with regexCallback Queries (Lines 625-702)
Callback Queries (Lines 625-702)
Purpose: Handle inline button clicksHandled callbacks:
select_product:[url]- Show product detailsdelete_product:[url]- Remove productedit_product:[url]- Start edit workflowdelete_all- Clear all productslist- Show product listcheck_prices- Trigger checkchart:[url]- Generate chart
bot.on("callback_query", ...)Cron Jobs (Lines 718-740)
Cron Jobs (Lines 718-740)
Purpose: Scheduled automationJobs:
- Daily summary at 20:00 (line 720)
- Price checks every 2 hours (line 733)
node-cron libraryDevelopment Setup
Prerequisites
- Node.js 16+
- npm or yarn
- Git
- A Telegram account
Local Development
-
Fork and clone:
-
Install dependencies:
-
Install Playwright browsers:
-
Create test bot:
- Message @BotFather on Telegram
- Create a new bot for testing
- Copy the token
-
Configure environment:
-
Start development:
Testing Your Changes
Manual testing checklist:- Send
/start- Verify welcome message - Add a product - Check scraping works
- Run
/list- Verify product appears - Run
/check- Force a price update - Wait or modify prices.json - Test notifications
- Try
/chart- Verify chart generation - Test
/edit- Update a product URL - Run
/remove- Delete a product - Check console logs for errors
How to Add Features
Adding New Commands
Example: Add a/pause command to temporarily stop notifications
-
Add data structure (after line 22):
-
Update persistence (in
loadData()andsaveData()): -
Add command handler (after line 623):
-
Update notification logic (in
checkPrices(), line 223): -
Update help text (line 378):
Adding New Scraper Selectors
If Amazon changes their HTML or you want to support more page types:- Inspect the product page (browser DevTools)
- Find the CSS selector for the element
- Add to selector array (lines 81-125)
Selectors are tried in order. Place most reliable/common selectors first for better performance.
Adding Export Functionality
Example: Export price data to CSV-
Add command (after line 623):
-
Update help text (line 378):
Adding Multi-User Support
To isolate products per user:-
Change data structure (line 21):
-
Update all product access throughout:
-
Update price check (line 139):
GitHub Workflow
Branching Strategy
Making Changes
- Write code following existing style
- Test thoroughly (see testing checklist above)
- Commit with clear messages:
Submitting Pull Requests
-
Push your branch:
-
Create PR on GitHub with:
- Clear title describing the change
- Description of what changed and why
- Screenshots/examples if UI changes
- Testing steps you performed
-
PR template:
Code Style Guidelines
JavaScript Conventions
Current style (maintain consistency):Console Messages
Use emojis for visual clarity:- ✅ - Success
- ❌ - Error
- ⚠️ - Warning
- ℹ️ - Info
- 🎉 - Celebration
- ⏳ - Loading/waiting
- 🔄 - Processing
Telegram Messages
Use Markdown formatting:Common Pitfalls
Forgetting to Save Data
Forgetting to Save Data
Problem: Changes to
priceData or chats not persistedSolution: Call saveData() after modificationsNot Closing Browsers
Not Closing Browsers
Problem: Memory leaks from unclosed browser instancesSolution: Always close in
finally or after useMarkdown Escaping
Markdown Escaping
Problem: User input breaks Markdown formattingSolution: Use
escapeMD() for all user dataAsync/Await Issues
Async/Await Issues
Problem: Forgetting
await on async operationsSolution: Mark functions as async and use awaitError Handling
Error Handling
Problem: Unhandled promise rejections crash botSolution: Wrap risky operations in try/catch
Documentation
When adding features, update:- Code comments - Explain complex logic
- README.md - Add new commands to list
- Help command (line 384) - Include new commands
- This guide - Document architecture changes
Getting Help
If you need assistance:- Check existing issues on GitHub
- Read the code - It’s well-structured and commented
- Open a discussion - Ask questions before major changes
- Join the community - Connect with other contributors
No contribution is too small! Bug reports, documentation improvements, and feature suggestions are all valuable.
Recognition
All contributors will be acknowledged in:- Project README
- Release notes
- GitHub contributors page