Skip to content

MeSrabon/DescoBalanceNotification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DESCO Prepaid Balance Notification Worker

A production-ready Cloudflare Worker that automatically checks DESCO prepaid electricity meter balances daily and sends notifications via Telegram.

Features

  • Automated Daily Checks: Runs every day at 1:00 AM (Asia/Dhaka timezone)
  • Multiple Accounts: Supports checking multiple DESCO account numbers
  • Telegram Notifications: Sends formatted balance updates to your Telegram chat
  • Retry Logic: Implements exponential backoff for API failures
  • Manual Trigger: Test anytime via /run endpoint
  • Error Handling: Robust error handling with detailed logging
  • Zero Dependencies: Uses only Cloudflare Workers runtime
  • Multiple Account Support: You Can get balance notification for multiple accounts

Example Telegram Message Output

🔌 DESCO Prepaid Balance Update  
🕐 20 Oct 2025, 1:00 AM (Asia/Dhaka)

• Account 12345511 → 374.04 ৳  
• Account 12345512 → 250.32 ৳

💰 Total Balance: 624.36 ৳

Prerequisites

  • Node.js (v18 or later)
  • npm or yarn
  • Cloudflare account (free tier works)
  • Telegram bot token and chat ID

Setup Instructions

1. Clone and Install Dependencies

cd ./desco-balance-notification
npm install

2. Create a Telegram Bot

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow the instructions
  3. Save the bot token (looks like 123456789:ABCdefGHIjklMNOpqrsTUVwxyz)
  4. Start a chat with your new bot and send any message
  5. Get your chat ID by visiting:
    https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
    
    Look for "chat":{"id":123456789} in the response

3. Configure Environment Secrets

Set your secrets using the Wrangler CLI:

# Set Telegram Bot Token
wrangler secret put TELEGRAM_BOT_TOKEN
# When prompted, paste your bot token

# Set Telegram Chat ID
wrangler secret put TELEGRAM_CHAT_ID
# When prompted, paste your chat ID (e.g., 123456789)

# Set Account Numbers (comma-separated)
wrangler secret put ACCOUNT_NUMBERS
# When prompted, enter: 12345511,12345512,12345513
# Or as JSON array: ["12345511","12345512","12345513"]

Alternative: Use npm scripts

npm run secret:telegram-token
npm run secret:chat-id
npm run secret:accounts

4. Test Locally (Optional)

# Start local development server
npm run dev

# In another terminal, test the endpoint
curl http://localhost:8787/run

5. Deploy to Cloudflare

npm run deploy

After deployment, you'll get a URL like:

https://desco-balance-notification.<your-subdomain>.workers.dev

Usage

Automatic Daily Checks

The worker runs automatically every day at 1:00 AM Asia/Dhaka time (19:00 UTC).

Manual Trigger

Test the worker anytime by visiting:

https://desco-balance-notification.<your-subdomain>.workers.dev/run

Or use curl:

curl https://desco-balance-notification.<your-subdomain>.workers.dev/run

Health Check

curl https://desco-balance-notification.<your-subdomain>.workers.dev/health

Configuration

Cron Schedule

The cron trigger is configured in wrangler.toml:

[triggers]
crons = ["0 19 * * *"]  # 1:00 AM Asia/Dhaka (UTC+6)

To change the schedule, modify the cron expression:

  • 0 19 * * * = 1:00 AM Dhaka
  • 0 13 * * * = 7:00 PM Dhaka
  • 0 0 * * * = 6:00 AM Dhaka

Multiple Accounts

You can specify account numbers in two formats:

Comma-separated:

12345511,12345512,12345513

JSON array:

["12345511","12345512","12345513"]

Project Structure

desco-balance-notification/
├── src/
│   └── index.ts           # Main worker code
├── wrangler.toml          # Cloudflare Workers configuration
├── package.json           # Dependencies and scripts
├── tsconfig.json          # TypeScript configuration
├── .gitignore            # Git ignore rules
└── README.md             # This file

API Details

DESCO API Endpoint

GET https://prepaid.desco.org.bd/api/unified/customer/getBalance?accountNo=12345511

Response:

{
  "code": 200,
  "desc": "OK",
  "data": {
    "accountNo": "12345511",
    "meterNo": "1234556025",
    "balance": 374.04,
    "currentMonthConsumption": null,
    "readingTime": "2025-10-20"
  }
}

Telegram API

POST https://api.telegram.org/bot<token>/sendMessage

Monitoring

View Logs

# Stream real-time logs
npm run tail

# Or directly with wrangler
wrangler tail

Check Cron Triggers

Visit the Cloudflare Dashboard:

  1. Go to Workers & Pages
  2. Select your worker
  3. Click on "Triggers" tab
  4. View cron trigger history

Troubleshooting

Worker not sending messages?

  1. Check secrets are set correctly:

    wrangler secret list
  2. Verify bot token and chat ID:

    curl "https://api.telegram.org/bot<YOUR_TOKEN>/getMe"
  3. Check worker logs:

    npm run tail

API timeout errors?

The worker implements retry logic with exponential backoff. If DESCO API is slow:

  • Default timeout: 10 seconds
  • Retries: 3 attempts
  • Backoff: 1s, 2s, 3s

Cron not triggering?

  • Cron triggers only work on deployed workers (not local dev)
  • Check the Cloudflare dashboard for trigger history
  • Verify the cron expression in wrangler.toml

Security Notes

  • ✅ Secrets are stored securely in Cloudflare's encrypted storage
  • ✅ Never commit secrets to git
  • ✅ Account numbers are treated as sensitive data
  • ✅ Use Telegram bot tokens, not user tokens

Cost

Cloudflare Workers Free Tier includes:

  • 100,000 requests/day
  • 10ms CPU time per request

This worker uses:

  • 1 cron trigger/day
  • ~2-5ms CPU time per run
  • Well within free tier limits

License

MIT

Support

For issues or questions:

  1. Check the Cloudflare Workers docs
  2. Review the worker logs with wrangler tail
  3. Test the DESCO API directly to verify it's working

Powered Cloudflare Workers

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors