Overview
The Price Tracker Bot uses environment variables to securely manage sensitive configuration data. All environment variables are loaded from a.env file in the project root.
Loading Configuration
The bot uses thedotenv package to load environment variables:
index.mjs (lines 5-8)
Required Variables
TELEGRAM_TOKEN
The authentication token for your Telegram bot. This is the only required environment variable.
TELEGRAM_TOKEN is mandatory and validates on startup:
index.mjs (lines 11-15)
123456789:ABCdefGHIjklMNOpqrsTUVwxyz-1234567890
Getting Your Telegram Bot Token
Step 1: Contact BotFather
Telegram’s @BotFather manages all bot creation and token generation.- Open Telegram and search for
@BotFather - Start a conversation by clicking Start or sending
/start
Step 2: Create a New Bot
Send the/newbot command to BotFather:
- Choose a name: The display name for your bot (e.g., “Price Tracker Bot”)
- Choose a username: Must end in
bot(e.g.,my_price_tracker_bot)
Usernames must be unique across all Telegram bots. If your choice is taken, try adding numbers or underscores.
Step 3: Receive Your Token
Upon successful creation, BotFather sends a message containing:Step 4: Add Token to .env File
Copy the token and add it to your.env file:
.env
Token Usage in Code
The token is used to initialize the Telegram bot instance:index.mjs (line 17)
polling: true- Enables long polling to receive updates from Telegram
Optional Variables
While the bot currently only requiresTELEGRAM_TOKEN, you may want to add additional environment variables for customization:
Suggested Optional Variables
The extended configuration example shows potential future enhancements. Currently, only
TELEGRAM_TOKEN is used by the bot.Security Best Practices
1. Never Commit Secrets
Add.env to your .gitignore:
.gitignore
2. Use Environment-Specific Files
For multiple environments, use separate env files:3. Restrict File Permissions
Limit who can read your.env file:
4. Rotate Tokens Regularly
Periodically regenerate your bot token:- Open @BotFather
- Send
/mybots - Select your bot
- Choose API Token → Revoke current token
- Update
.envwith the new token
Token Validation
Testing Your Token
Verify your token is valid before starting the bot:Common Token Issues
| Issue | Symptom | Solution |
|---|---|---|
| Missing token | Bot exits: “TELEGRAM_TOKEN no definido” | Add token to .env file |
| Invalid format | Bot fails to connect | Verify token format from BotFather |
| Revoked token | API returns 401 Unauthorized | Generate new token via BotFather |
| Extra whitespace | Connection errors | Remove spaces around token in .env |
Environment Variable Precedence
When using multiple configuration sources, variables are loaded in this order (later sources override earlier ones):- System environment variables
.envfile variables.env.localfile variables (if using)
Deployment Considerations
Docker
When deploying with Docker, pass environment variables via docker-compose:docker-compose.yml
.env file alongside docker-compose.yml:
.env
Cloud Platforms
For cloud deployments, use the platform’s secret management: Heroku:Cloud platforms typically inject environment variables directly into the runtime, so you don’t need a
.env file in production.Troubleshooting
Bot Can’t Find .env File
Symptom: “TELEGRAM_TOKEN no definido” despite having.env file
Solutions:
- Verify
.envis in the same directory asindex.mjs - Check file is named exactly
.env(notenv.txtor.env.txt) - Ensure no UTF-8 BOM or special characters in the file
Token Not Loading
Symptom: Token is in.env but bot says it’s undefined
Solutions:
- Check for spaces: Use
TELEGRAM_TOKEN=valuenotTELEGRAM_TOKEN = value - Verify no quotes needed:
TELEGRAM_TOKEN=123:ABCnotTELEGRAM_TOKEN="123:ABC" - Ensure
dotenv.config()is called before accessing variables
Multiple Bots on Same Server
Symptom: Need to run multiple bot instances Solution: Use separate directories with individual.env files:
Next Steps
Setup Guide
Complete initial setup and configuration
Scheduling
Configure automated price checks and notifications