• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
colebemis

colebemis

jarvis

Public
Like
jarvis
Home
Code
3
README.md
H
index.ts
system-prompt.md
Branches
1
Pull requests
Remixes
History
Environment variables
2
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
/
x
/
colebemis
/
jarvis
/
branch
/
main
/
version
/
52
/
code
/
README.md
/
README.md
Code
/
/
x
/
colebemis
/
jarvis
/
branch
/
main
/
version
/
52
/
code
/
README.md
/
README.md
Search
9/23/2025
Viewing readonly version of main branch: v52
View latest version
README.md

Telegram Bot with In-Memory Queue

This Telegram bot uses an in-memory queue system to handle chat messages efficiently, ensuring fast webhook responses while processing AI-generated replies asynchronously.

Architecture

Fast Webhook Response

  • Webhook handler receives messages and immediately adds them to an in-memory queue
  • Returns quickly to avoid Telegram webhook timeouts
  • Shows "typing" indicator to acknowledge receipt

Asynchronous Processing

  • Background queue processor runs every 500ms
  • Processes messages one by one with AI text generation
  • Sends responses back to Telegram using the Bot API
  • Includes error handling and rate limiting

Queue Structure

interface ChatTask { chatId: number; // Telegram chat ID messageId: number; // Original message ID for replies userMessage: string; // User's message text timestamp: number; // When message was queued }

Key Features

  1. Fast Response: Webhook completes in milliseconds
  2. Reliable Processing: Messages are queued and processed reliably
  3. Error Handling: Failed messages get error responses
  4. Rate Limiting: Small delays between processing to avoid API limits
  5. Logging: Console logs for monitoring queue activity

Environment Variables

  • TELEGRAM_TOKEN: Your Telegram bot token
  • OPENAI_API_KEY: OpenAI API key for text generation

How It Works

  1. User sends message to Telegram bot
  2. Telegram webhook calls our endpoint
  3. Message is added to queue, webhook responds immediately
  4. Background processor picks up message from queue
  5. AI generates response using OpenAI
  6. Response is sent back to user via Telegram API

This pattern ensures your middleware stays fast while allowing complex processing to happen asynchronously.

FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.