Home

/

Library

/

tech/how-to-telegram-alerts.md

How to Implement Telegram Alerts

How to Implement Telegram Alerts

Note: Some of this might need slight tweaking depending on your python setup, but it should be 97% there.

  1. In your Python .env file, add the following:

    # Telegram Alerts
    TELEGRAM_TOKEN=8502040967:AAFXYDNuOl0XScvIGzlw88aXa_UkZbqNGwM # Telegram bot token
    TELEGRAM_CHAT_ID=-5166325869 # Prediction Arb Group ID, of which we are now members
    TELEGRAM_ENABLED=true # Set to false to disable alerts

  2. Add this after the imports of your startup Python script:

    from dotenv import load_dotenv
    import requests
    import os
    load_dotenv()
    print("DEBUG .env load:")
    print("Token:", os.getenv("TELEGRAM_TOKEN") or "MISSING")
    print("Chat ID:", os.getenv("TELEGRAM_CHAT_ID") or "MISSING")
    print("Enabled:", os.getenv("TELEGRAM_ENABLED", "false"))
    print("Backend starting up...")
    send_telegram_alert("Cornice Backend started - Telegram alerts active! 🚀")

  3. Add this near the top of your startup Python script. Also add this to the .py script that is checking for arbitrage opportunities.

    def send_telegram_alert(message: str):
    token = os.getenv("TELEGRAM_TOKEN")
    chat_id = os.getenv("TELEGRAM_CHAT_ID")
    enabled = os.getenv("TELEGRAM_ENABLED", "false").lower() == "true"

    **if not enabled or not token or not chat\_id:**  
        **print("Telegram alert skipped: disabled or missing config")**  
        **return**  
      
    **url \= f"https://api.telegram.org/bot{token}/sendMessage"**  
    **payload \= {**  
        **"chat\_id": chat\_id,**  
        **"text": message,**  
        **"parse\_mode": "HTML",**  
        **"disable\_web\_page\_preview": True**  
    **}**  
      
    **try:**  
        **response \= requests.post(url, data=payload, timeout=10)**  
        **if response.status\_code \== 200:**  
            **print("Telegram alert sent successfully")**  
        **else:**  
            **print(f"Telegram alert failed: {response.status\_code} \- {response.text}")**  
    **except Exception as e:**  
        **print(f"Telegram send error: {e}")**  
    
  4. Where your script finds an arbitrage opportunity, add this code to build the telegram message. You may need to change some of the variables based on what variables you use in your code.
    alert_msg = f"<b>🚨 BTC HOURLY ARB OPPORTUNITY!</b>\n\n"
    alert_msg += f"<b>Exact Trade to Make:</b>\n"
    alert_msg += f"• Buy <b>Polymarket DOWN</b> @ ${poly_down_cost:.3f}\n"
    alert_msg += f"• Buy <b>Kalshi YES</b> @ ${kalshi_yes_cost:.3f}\n\n"
    alert_msg += f"<b>Strikes:</b>\n"
    alert_msg += f"• Polymarket Strike: ${poly_strike:.0f}\n"
    alert_msg += f"• Kalshi Strike: ${kalshi_strike:.0f}\n\n"
    alert_msg += f"<b>Financials:</b>\n"
    alert_msg += f"• Total Cost: <code>${total_cost:.3f}</code>\n"
    alert_msg += f"• Guaranteed Profit: <b>${margin:.3f}</b> per $1 pair ({margin*100:.1f}% gross)\n"
    alert_msg += f"\n<i>Risk-free hedge — execute quickly!</i>"
    send_telegram_alert(alert_msg)

Appendix

What changed

  • Added async Telegram sender in app/infra/telegram.py.
  • Added telegram_* settings in app/infra/settings.py.
  • Startup ping in app/main.py.
  • Arb alert message in app/data/ingest.py.
  • Documented env vars in docs/config.md.

Notes

  • Please put your TELEGRAM_TOKEN, TELEGRAM_CHAT_ID, and TELEGRAM_ENABLED=true in your local .env (not committed). I did not add or echo the token anywhere in code.

If you want different alert formatting or throttling, tell me and I’ll adjust.

Suggested next steps:

  1. Add the Telegram vars to your .env
  2. Restart the backend and confirm you see the startup alert
  3. Trigger an arb candidate to verify the alert format