Skip to content

serejaris/telegram-skills / skills/tg-widgets

tg-widgets

Verified

Use when building or sending a Telegram message widget (HTML bubble, HTMLBubbles, `sendWidget`, `messageMediaWidget`, Bot API 10.4): an interactive HTML, CSS and JS program inside a chat bubble, such as a game, poll, calculator or live ticker. Teaches everything needed to write one: size, bubble, input, sandbox rules, state and requests, sending, and which chats accept a widget in production. Not for Mini Apps or Rich Messages.

scripts/widget_tool.py

View on GitHub
#!/usr/bin/env python3
"""Measure a Telegram widget like the server does, and send it once.

check: no network. send: one POST to the Bot API, never retried, because
a timeout does not prove the message was not delivered.
"""
import argparse
import json
import os
import sys
import urllib.error
import urllib.request
import zlib

MAX_HTML = 32768
MAX_ZLIB = 4096
TARGET_ZLIB = 3800
API = "https://api.telegram.org/bot{token}/sendWidget"


def measure(data: bytes) -> dict:
    packed = len(zlib.compress(data, 9))
    if len(data) > MAX_HTML or packed > MAX_ZLIB:
        verdict = "too_large"
    elif packed > TARGET_ZLIB:
        verdict = "near_limit"
    else:
        verdict = "ok"
    return {"html_bytes": len(data), "zlib_bytes": packed, "verdict": verdict}


def send(token: str, html: str, chat: str, caption: str | None) -> dict:
    payload = {"chat_id": chat, "html": html}
    if caption:
        payload["caption"] = caption
    request = urllib.request.Request(
        API.format(token=token),
        data=json.dumps(payload).encode(),
        headers={"Content-Type": "application/json"},
    )
    try:
        with urllib.request.urlopen(request, timeout=30) as response:
            return json.load(response)
    except urllib.error.HTTPError as error:
        return json.load(error)


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    sub = parser.add_subparsers(dest="command", required=True)
    check = sub.add_parser("check")
    check.add_argument("file")
    push = sub.add_parser("send")
    push.add_argument("file")
    push.add_argument("--chat", required=True)
    push.add_argument("--caption")
    args = parser.parse_args()

    with open(args.file, "rb") as handle:
        data = handle.read()
    result = measure(data)
    print(json.dumps(result))
    if result["verdict"] == "too_large":
        return 1
    if args.command == "check":
        return 0

    token = os.environ.get("TELEGRAM_BOT_TOKEN")
    if not token:
        print("TELEGRAM_BOT_TOKEN is not set", file=sys.stderr)
        return 2
    answer = send(token, data.decode(), args.chat, args.caption)
    if not answer.get("ok"):
        print(answer.get("description"), file=sys.stderr)
        return 1
    print(json.dumps({"message_id": answer["result"]["message_id"]}))
    return 0


if __name__ == "__main__":
    sys.exit(main())
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall