Build / Bots

Telegram address check bot

Send /check with coordinates, get back a county forecast grade and a past event count.

PythonTelegram Bot APILong polling
Telegram address check bot: illustrative mockup with sample data
Example build (illustrative). Sample data, not a real product, not real results.

What it does

A short long-polling script. People message your bot, your script calls the API, the bot answers. You make the bot with the chat platform, so the bot token and the API key are both yours.

  • Geocode a street address first, or accept lat and lon as above.
  • Add a per-user daily limit.

API calls

Set your key once: export TFX_KEY=tfx_live_.... Free previews need no key.

Per message: one property

request
curl -H "Authorization: Bearer $TFX_KEY" "https://data.truefixr.com/v1/data?dataset=address&lat=32.78&lon=-96.80"

Code (Python)

Cut down on purpose. Print the real response keys on your first call before you map fields.

telegram-check-bot.py
import os
import time
import requests

TG = f"https://api.telegram.org/bot{os.environ['TG_TOKEN']}"
API = "https://data.truefixr.com/v1/data"
HEAD = {"Authorization": f"Bearer {os.environ['TFX_KEY']}"}


def lookup(lat: str, lon: str) -> str:
    r = requests.get(API, params={"dataset": "address", "lat": lat, "lon": lon}, headers=HEAD, timeout=60)
    if r.status_code != 200:
        return f"Lookup failed ({r.status_code})."
    d = r.json()
    return (f"Past events near this location: {d.get('storm_event_count')}\n"
            f"Forecast: {d.get('current_forecast_risk')}\n"
            "Reported and radar-detected. Not an inspection.")


def main():
    offset = 0
    while True:
        updates = requests.get(f"{TG}/getUpdates", params={"timeout": 30, "offset": offset}, timeout=40).json()
        for u in updates.get("result", []):
            offset = u["update_id"] + 1
            msg = u.get("message", {})
            text = msg.get("text", "")
            if text.startswith("/check"):
                parts = text.split()
                if len(parts) == 3:
                    reply = lookup(parts[1], parts[2])
                else:
                    reply = "Usage: /check LAT LON"
                requests.post(f"{TG}/sendMessage", json={"chat_id": msg["chat"]["id"], "text": reply}, timeout=30)
        time.sleep(1)


if __name__ == "__main__":
    main()