Skip to main content

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.

ResponseWithBarcodeBot Interface


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​

ParameterConfigured ValueEngineering Rationale
SymbologyCode 128 (ISO/IEC 15417)High-density linear symbology supporting full 128 ASCII character set
module_width0.3 mmPreserves crisp bar separation without triggering Telegram JPEG/PNG downscaling
module_height15.0 mmEnsures reliable laser and camera scanning across mobile screens
write_textFalseOmits human-readable caption inside the PNG; raw text is returned in the Telegram photo caption
Max Input Length64 charactersPrevents 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_photo completes.

Get notified about updates to this tool

No spam. Just high-quality updates about new features and guides.