ResponseWithBarcodeBot — Telegram Code 128 Barcode Generator
Convert text strings, SKU identifiers, and URLs into high-density Code 128 barcode PNGs directly inside Telegram via @responseWithBarcodeBot.

Technical Architecture: Reverse-Engineering @responseWithBarcodeBot
I designed @responseWithBarcodeBot as a decoupled, zero-disk-persistence component within my Telegram bot infrastructure. Incoming text updates are validated for length and character bounds in barcodebot/handlers.py and rendered directly into an io.BytesIO PNG buffer in barcodebot/core.py using python-barcode.
+------------------+ +--------------------------+ +------------------------+
| Telegram Server | -----> | Handler & Auth Layer | -----> | Core Barcode Engine |
| (Webhook/Update) | | (handlers.py) | | (core.py) |
+------------------+ +--------------------------+ +------------------------+
| |
v v
[Length & Auth Checks] [In-Memory BytesIO PNG]
1. In-Memory Code 128 Rendering Pipeline
To eliminate temporary file I/O and cleanup cron jobs, generate_barcode() writes the rendered Code 128 raster directly into an io.BytesIO stream and rewinds the pointer (buffer.seek(0)) for immediate transmission to reply_photo:
import io
import logging
from typing import Optional
import barcode
from barcode.writer import ImageWriter
logger = logging.getLogger(__name__)
def generate_barcode(message: str) -> Optional[io.BytesIO]:
try:
writer_options = {
"write_text": False,
"background": "white",
"foreground": "black",
"module_width": 0.3,
"module_height": 15,
"format": "PNG",
}
barcode_obj = barcode.Code128(str(message), writer=ImageWriter())
buffer = io.BytesIO()
barcode_obj.write(buffer, options=writer_options)
buffer.seek(0)
return buffer
except Exception as e:
logger.error(f"Error generating barcode: {e}")
return None
2. Horizontal Density Guardrails & Dispatcher Binding
Linear 1D barcodes scale horizontally in direct proportion to payload length. To prevent generating ultra-wide barcodes that become unreadable on mobile screens due to Telegram's photo compression, parse_update_message() enforces a strict 64-character ceiling (RESPONSEWITHBARCODEBOT_TEXT_LENGTH):
def parse_update_message(update) -> Optional[str]:
if not update.message.text:
return None
if len(update.message.text) > int(settings.RESPONSEWITHBARCODEBOT_TEXT_LENGTH):
return None
return update.message.text
def register_responsewithbarcodebot_handlers(dispatcher):
dispatcher.add_handler(CommandHandler('start', handle_start_message))
dispatcher.add_handler(MessageHandler(Filters.text, handle_message))
dispatcher.add_handler(ChatMemberHandler(user_left, ChatMemberHandler.MY_CHAT_MEMBER))
Code 128 Renderer Specifications
| Parameter | Configured Value | Engineering Rationale |
|---|---|---|
| Symbology | Code 128 (ISO/IEC 15417) | High-density linear symbology supporting full 128 ASCII character set |
module_width | 0.3 mm | Preserves crisp bar separation without triggering Telegram JPEG/PNG downscaling |
module_height | 15.0 mm | Ensures reliable laser and camera scanning across mobile screens |
write_text | False | Omits human-readable caption inside the PNG; raw text is returned in the Telegram photo caption |
| Max Input Length | 64 characters | Prevents horizontal aspect ratio overflow on mobile viewports |
Stateless Processing & Privacy Guarantees
- Zero Disk Storage: Barcode PNG buffers exist exclusively in RAM (
io.BytesIO) for the duration of the HTTP response to Telegram's Bot API. - No Payload Logging: Message contents are converted in memory and discarded immediately after
reply_photocompletes.
Get notified about updates to this tool
No spam. Just high-quality updates about new features and guides.
Related Tools
Related Posts
Learn how to detect Google AdSense on any website using Python. Covers fast HTML scanning with BeautifulSoup, ads.txt validation, and headless Playwright automation to bypass bot protection and lazy loading.
Learn how to easily download YouTube video thumbnails using Python's requests library, without the need for heavy dependencies like Pytube. This guide covers extracting video IDs from various YouTube URL formats and fetching the highest quality thumbnail available.
This piece will introduce Make (formerly Integromat) as a direct competitor to n8n, highlighting its visual, diagram-like workflow builder. The comparison will focus on key differences in pricing models, UI/UX, and advanced features like branching logic, which are a major selling point for Make. Examples will show how to build the same complex workflow in both n8n and Make to illustrate their unique approaches.