Skip to content

RodEsp/daily-leetcode-bot

Repository files navigation

Daily Leetcode Bot

If you're a recurser, ping me on Zulip for any questions.
If you're a vertical flat plate, ping me on Slack for any questions.

The Daily Leetcode Bot can post questions from leetcode.com to Zulip or Slack every day.

It is primarily intended to be used for the Recurse Center but it can be easily configured to post with any frequency to any Zulip realm or Slack workspace.

Getting Started

To run this bot you must have Node.js v24.0.0 or higher installed.

Configuration

The bot can be configured by using the following environment variables:

  • Zulip

    • ZULIP_USERNAME - the e-mail address of your Zulip bot
    • ZULIP_API_KEY - the API_KEY of your Zulip bot
    • ZULIP_REALM - your Zulip Realm or the https address of your Zulip instance

    You can also place a zuliprc at the root of this repo.

  • Slack

  • Bot

    • DLB_TIMEZONE - A timezone identifier as defined by IANA (list here)
    • DLB_CRON_SCHEDULE - A cron expression as defined by node-cron
    • DLB_USER_ID - A Zulip user ID or a stream name
    • DLB_TOPIC - A Zulip topic name

Running

  1. Clone this repo
  2. Install dependencies
    1. npm install
  3. Run the bot
    1. npm start

Repo Overview

This repository is organized into the following structure:

Source Files (src/)

The main entry point and orchestration logic. Handles:

  • Cron job scheduling
  • Fetching LeetCode daily problems (with fallback to alfa-leetcode-api)
  • Selecting random Grind75 problems
  • Fetching Advent of Code problems (during December)
  • Coordinating message posting
  • Graceful shutdown handling

Zulip client initialization and configuration management. Validates and parses environment variables for Zulip setup.

Message posting logic for both Zulip and Slack. Formats messages with LeetCode, Grind75, and Advent of Code problems.

Utility functions including:

  • fetchWithRetry() - HTTP requests with exponential backoff retry logic
  • random() - Random number generation helper

GraphQL queries for the LeetCode API.
Unfortunately the LeetCode API is not documented but useful information about it can be found here, here, and by searching the intertubez.

Type Definitions (types/)

TypeScript type definitions for:

  • LeetCode API responses (leetcode.ts)
  • Grind75 problem data structure (grind75.ts)
  • Message data structure (message.ts)
  • Zulip-js module declarations (zulip-js.d.ts)

Data Files (data/)

A JSON dataset containing the problems found here which were compiled by the talented @yangshun.

Contributing

Please read our Code of Conduct

Please create an issue if you want to submit a PR and link it to your PR with one of these keywords.

Feel free to create issues for any bugs or feature requests. If submitting a bug, please include as many details as possible. Including, but not limited to, reproduction steps, logs, and screenshots that show the problem.

TODO

Future improvements and enhancements:

  • Structured Logging: Replace console.log/error/warn with a structured logging for better log parsing and production observability
  • Permanent Logs: Log to a file so we can debug if the server crashes
  • Health Check Endpoint: Add a simple HTTP server with a health check endpoint for monitoring and alerting
  • Error Handling Consistency: Standardize error handling patterns across the codebase for consistent error visibility
  • Message Posting Retry Logic: Add retry logic with exponential backoff for message posting failures to prevent silent message loss
  • Response Validation: Add runtime validation for API responses (e.g., using Zod) to handle unexpected API response structures gracefully
  • Test Suite: Add unit and integration tests for critical paths (fetchWithRetry, message formatting, etc.)
  • Rate Limiting: Add rate limiting considerations or documentation for API usage to prevent hitting API rate limits
  • Type Safety Improvements: Add runtime type validation for type assertions (e.g., difficulty levels) to catch unexpected API values early

About

A bot that posts leetcode questions to Zulip and Slack

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published