DEV Community

Malcolm Low
Malcolm Low

Posted on • Originally published at malcolmlow.net

SingIDBot — Telegram ID Retrieval Bot for OpenClaw Topic Configuration

Originally published on malcolmlow.net

If you have been working through the OpenClaw Telegram integration guide, you already know that one of the trickiest steps is obtaining the correct Chat ID and Topic Thread ID before you can configure your bot endpoints. Getting those IDs wrong means your bot silently sends messages into the void — no errors, no feedback, just nothing arriving in the right place.

SingIDBot was built specifically to solve that problem. Drop it into any Telegram chat or group, fire the /getids command, and it instantly reports back the Chat ID and — crucially — the message_thread_id for whichever topic the command was sent from.

Beyond the ID retrieval utility, SingIDBot has a second personality: it responds to free-text messages in Singlish — Singapore's beloved colloquial English creole — cycling through randomised phrases like "Aiyoh, you finally came! Welcome lah!" and "Wah, interesting leh. Tell me more can?"

This guide walks through the complete source code, explains each architectural decision, and covers deployment on PythonAnywhere including the free-tier network restrictions you will inevitably hit.


Prerequisites

Requirement Details
Python 3.10+ Required for python-telegram-bot v20+ async support
python-telegram-bot v20+ The async-native version. Install with pip install python-telegram-bot
Telegram Bot Token Create via @botfather — send /newbot and copy the token
PythonAnywhere account Free Beginner account is sufficient to run and test the bot

Section 1 — What SingIDBot Does

The message_thread_id Mechanism

Telegram supergroups can be divided into named Topics, each identified by a message_thread_id. When a message is sent inside a topic, Telegram attaches this integer to the message object. When sent outside any topic, the field is absent entirely — not zero, not null, but genuinely missing.

This is the exact value OpenClaw requires to route notifications to the correct topic thread. SingIDBot's /getids command reads message_thread_id from the incoming message and reports it in plain text for direct copy-paste into your configuration.

Because the field may be absent, the code uses getattr(update.effective_message, 'message_thread_id', None) rather than direct attribute access, which would raise an AttributeError in non-topic contexts.

The Singlish Responder

Any free-text message (not a command) triggers the singlish_chat handler. It classifies the message into greeting, thanks, or default using keyword matching, then picks a random phrase from the corresponding pool.


Section 2 — Code Walkthrough

2.1 Logging Setup

Two handlers run simultaneously: a FileHandler writing UTF-8 lines to singidbot.log, and a StreamHandler for the console.

LOG_FILE = "singidbot.log"
logging.basicConfig(
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
    level=logging.INFO,
    handlers=[
        logging.FileHandler(LOG_FILE, encoding="utf-8"),
        logging.StreamHandler(),
    ],
)
logger = logging.getLogger(__name__)
Enter fullscreen mode Exit fullscreen mode

Tip: The encoding="utf-8" argument is essential if any Telegram usernames contain non-ASCII characters. Without it, a single emoji in a username can crash the logger with a UnicodeEncodeError.

2.2 Singlish Response Bank

SINGLISH_RESPONSES maps category keys to phrase lists. get_singlish_response() uses dict.get() with a "default" fallback so unknown categories never raise a KeyError.

SINGLISH_RESPONSES = {
    "greeting": [
        "Wah, you here ah! Come come, sit down lah!",
        "Eh hello! Long time no see, how are you?",
        "Aiyoh, you finally came! Welcome lah!",
        "Oi! Good to see you sia. What can I help you?",
    ],
    "thanks": [
        "Aiyah, no need to thank one lah!",
        "Wah, so polite! Welcome lah, anytime!",
        "No problem one! Next time also can help you.",
        "Eh, paiseh lah - that is what I am here for!",
    ],
    "default": [
        "Hah? Can repeat or not? I blur blur lah.",
        "Wah, interesting leh. Tell me more can?",
        "Aiyoh, I also dunno how to answer you sia.",
        "Got it lah! But I not so sure about that one.",
        "Eh, you very clever leh. I learn from you!",
    ],
}

def get_singlish_response(category):
    pool = SINGLISH_RESPONSES.get(category, SINGLISH_RESPONSES["default"])
    return random.choice(pool)
Enter fullscreen mode Exit fullscreen mode

2.3 log_activity() Async Helper

async def log_activity(user, event_type, metadata):
    user_info = (
        f"@{user.username}" if user and user.username
        else f"id={user.id}" if user
        else "unknown"
    )
    timestamp = datetime.now(timezone.utc).isoformat()
    logger.info("[%s] event=%s user=%s metadata=%s",
                timestamp, event_type, user_info, metadata)
Enter fullscreen mode Exit fullscreen mode

2.4 /start and /help Handlers

async def start(update, context):
    user = update.effective_user
    await log_activity(user, "START", {"chat_id": update.effective_chat.id})
    greeting = get_singlish_response("greeting")
    name = user.first_name if user and user.first_name else "friend"
    welcome_text = (
        f"{greeting}

Eh {name}, I am *SingIDBot* lah!

"
        "Commands:
- /start
- /help
- /getids"
    )
    await update.message.reply_text(welcome_text, parse_mode="Markdown")
Enter fullscreen mode Exit fullscreen mode

2.5 /getids Handler

The key line is the getattr call. Telegram only populates message_thread_id when a message arrives inside a topic thread — in a regular chat the attribute simply does not exist.

async def get_ids(update, context):
    user = update.effective_user
    await log_activity(user, "ID_RETRIEVAL", {"chat_id": update.effective_chat.id})
    chat_id = update.effective_chat.id
    topic_id = getattr(update.effective_message, "message_thread_id", None)
    response = f"Chat/Group ID: `{chat_id}`
"
    if topic_id is not None:
        response += f"Topic ID: `{topic_id}`
(This ID confirms the specific topic thread)"
    else:
        response += "Topic ID: Not available
(Topics may not be enabled)"
    response += "

Tip: Record these IDs for configuring bot endpoints!"
    await update.message.reply_text(response, parse_mode="Markdown")
    await update.message.reply_text(f"{get_singlish_response('thanks')} Lah!")
Enter fullscreen mode Exit fullscreen mode

2.6 singlish_chat Handler

Registered with filters.TEXT and ~filters.COMMAND. A guard clause checks if the sender is the bot itself to prevent infinite reply loops.

async def singlish_chat(update, context):
    user = update.effective_user
    await log_activity(user, "MESSAGE", {
        "text_preview": update.message.text[:50] if update.message else "N/A"
    })
    if (not update.message
            or update.message.text.startswith("/")
            or update.effective_user.id == context.bot.id):
        return
    user_text = update.message.text.lower().strip()
    if any(w in user_text for w in ["thanks", "thank", "appreciate"]):
        category = "thanks"
    elif any(w in user_text for w in ["hello", "hi", "hey", "oi", "wah", "alamak"]):
        category = "greeting"
    else:
        category = "default"
    await update.message.reply_text(get_singlish_response(category))
Enter fullscreen mode Exit fullscreen mode

2.7 error_handler — Two-Tier Approach

async def error_handler(update, context):
    error = context.error
    if isinstance(error, Exception):
        error_name = type(error).__name__
        error_msg = str(error)
        if "ProxyError" in error_msg or "NetworkError" in error_name:
            logger.warning("Network error (transient): %s", error_msg)
        elif "Conflict" in error_name or "terminated by other getUpdates" in error_msg:
            logger.warning("Conflict error (duplicate instance?): %s", error_msg)
        else:
            logger.error("Unhandled exception: %s: %s",
                         error_name, error_msg, exc_info=context.error)
Enter fullscreen mode Exit fullscreen mode

2.8 main() — Builder Chain and Handler Registration

def main():
    TOKEN = "YOUR_BOT_TOKEN_HERE"
    application = (
        Application.builder()
        .token(TOKEN)
        .connect_timeout(30)
        .read_timeout(30)
        .write_timeout(30)
        .build()
    )
    application.add_handler(CommandHandler("start", start))
    application.add_handler(CommandHandler("help", help_command))
    application.add_handler(CommandHandler("getids", get_ids))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, singlish_chat))
    application.add_error_handler(error_handler)
    logger.info("--- SingIDBot Initializing ---")
    application.run_polling(allowed_updates=Update.ALL_TYPES)
Enter fullscreen mode Exit fullscreen mode

Section 3 — Deployment on PythonAnywhere

Upload singidbot.py via the Files tab, open a Bash console, and run:

pip install python-telegram-bot --upgrade
python singidbot.py
Enter fullscreen mode Exit fullscreen mode

Free-Tier Proxy Restriction

Free accounts route outbound traffic through a proxy that does not whitelist api.telegram.org. Outbound sendMessage calls can fail with httpx.ProxyError: 503 Service Unavailable. Inbound polling via getUpdates works fine — it is only the reply direction that is intermittently blocked.

In practice across four live sessions totalling over 5 hours, only 2 ProxyErrors occurred in 1800+ polling cycles.

Options if you need reliable reply delivery: Upgrade to a paid PythonAnywhere plan, or switch from polling to webhook mode via PythonAnywhere's WSGI layer.

Avoiding Duplicate Instance Conflicts

Telegram only allows one active getUpdates poller per token. Always kill the old process before restarting:

pkill -f singidbot.py
pgrep -a -f singidbot
python singidbot.py
Enter fullscreen mode Exit fullscreen mode

Section 4 — Log Analysis

Session Duration Key Events Errors
Session 1 ~2 min First run. /getids succeeded returning Chat ID 5024469893. 1 ProxyError (no error handler yet)
Session 2 ~3 min Error handler added. /help ProxyError suppressed to WARNING. 1 ProxyError (WARNING only)
Session 3 ~14 min Extended run. 47 consecutive clean polling cycles. 1 ProxyError (WARNING only)
Session 4 ~5 hours 1800+ log lines. Clean polling throughout. No conflicts. 2 ProxyErrors total

SingIDBot is production-stable on the free tier. The intermittent ProxyError is a platform constraint, not a code defect.


Quick Reference

Command What it returns Notes
/start Personalised Singlish greeting and command list Works in any chat context
/help List of all available commands Works in any chat context
/getids Chat ID and Topic Thread ID (if inside a topic) Must be run inside the target topic to get a valid Thread ID

Top comments (0)