Skip to content

feat: Create broadcast script to send messages to all active users #169

@LeonardoVieira1630

Description

@LeonardoVieira1630

Context

We need a way to send broadcast messages to all active users in our notification system. This is useful for announcements, updates, or important communications that need to reach all subscribers.

Objective

Create a command-line script that reads all active users from the database and sends a broadcast message to them via Telegram.

Technical Requirements

  1. Script Location: /apps/consumers/scripts/broadcast-message.ts

  2. Command Line Interface:

    npm run broadcast "Your message here"
    • Add script to package.json: "broadcast": "ts-node scripts/broadcast-message.ts"
  3. User Selection:

    • Query database for all users where isActive = true
    • Only send to users with valid Telegram chat IDs
  4. Message Sending:

    • Plain text messages only
    • Same message for all users (no personalization)
    • Respect Telegram rate limits (30 messages/second)
  5. Confirmation Flow:

    • Display total number of active users found
    • Show message preview
    • Require Y/N confirmation before sending
  6. Output Report:

    • Track successful sends
    • Track failed sends with error reasons
    • Display summary at the end:
      Broadcast Summary:
      - Total users: X
      - Successfully sent: Y
      - Failed: Z
      - Error details: [list of failures]
      
  7. Error Handling:

    • Continue sending even if some messages fail
    • Log each failure with user ID and error message
    • Handle rate limiting gracefully

Example Usage

$ npm run broadcast "🎉 New feature available! Check out our latest updates."

Found 150 active users.
Message preview:
"🎉 New feature available! Check out our latest updates."

Send this message to all 150 users? (Y/N): Y

Sending messages...
Progress: 150/150

Broadcast Summary:
- Total users: 150
- Successfully sent: 148
- Failed: 2
- Errors:
  - User 12345: 403 Forbidden: bot was blocked by the user
  - User 67890: 400 Bad Request: chat not found

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions