AXYNERA

AXYNERA ACADEMY / PRACTICE

Connect trading signals to Telegram

From a confirmed signal to a readable bot alert, with a private test chat and a runnable Python example.

1. Define the signal

Example: A 15-minute candle closes above the high of the previous 20 completed candles. The strategy emits a signal after the candle closes. Store the venue, instrument, side, timeframe, UTC close time, rule version and a stable signal ID so you can reconstruct the alert.

2. Set up a bot and private test chat

Create a dedicated test bot with @BotFather and send /start in its private chat. Read message.chat.id from getUpdates using a server-side client. If the bot already uses a webhook, read the incoming update there. Keep the token and chat ID in environment variables. Store the token outside code, websites and logs.

3. Preview locally, then send once

Save the example as telegram_signal.py. The first command prints the alert locally. The second sends one fictional alert to your configured test chat. For integration, replace the example signal with output from your strategy.

python telegram_signal.py
python telegram_signal.py --send
import argparse
import json
import os
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--send", action="store_true")
    args = parser.parse_args()
    signal_id = "demo-v1:BTC-USDC:15m:2026-11-09T15:45Z"
    message = (
        "DEMO | BTC/USDC | 15m | LONG\n"
        "2026-11-09 15:45 UTC | Breakout confirmed\n"
        f"ID: {signal_id}"
    )
    if not args.send:
        print(message)
        return 0

    token = os.environ.get("TELEGRAM_BOT_TOKEN")
    chat_id = os.environ.get("TELEGRAM_TEST_CHAT_ID")
    if not token or not chat_id:
        print("Set TELEGRAM_BOT_TOKEN and TELEGRAM_TEST_CHAT_ID.")
        return 1

    request = Request(
        f"https://api.telegram.org/bot{token}/sendMessage",
        data=json.dumps({"chat_id": chat_id, "text": message}).encode(),
        headers={"Content-Type": "application/json"},
    )
    try:
        with urlopen(request, timeout=10) as response:
            result = json.load(response)
    except HTTPError as error:
        if error.code == 429:
            try:
                wait = json.load(error).get("parameters", {}).get("retry_after")
            except (ValueError, OSError):
                wait = None
            print(f"RATE_LIMITED retry_after={wait}")
        else:
            print(f"HTTP_ERROR {error.code}")
        return 1
    except (URLError, OSError, ValueError):
        print("DELIVERY_UNKNOWN: check the test chat before resending.")
        return 1

    if result.get("ok") is not True:
        print("TELEGRAM_REJECTED")
        return 1
    print("DELIVERED", result["result"]["message_id"])
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

4. Filter signals and track delivery

Store confirmed signals in a persistent outbox with a unique signal ID per destination. A worker claims each item before sending and stores the Telegram message_id on success. Route routine heartbeats and ordinary rate limits to logs. Group shadow results in a digest and clearly label demo, shadow and live alerts.

For HTTP 429, schedule the next attempt after retry_after when Telegram supplies it. After a timeout, delivery may already have happened. Mark delivery as unknown and verify before sending again. The demo reports these states. The outbox and deduplication belong in the production integration.

Official sources ยท Telegram

Bot TutorialgetUpdatessendMessageretry_after