What the bot does
A reader sends a birth date, a time and a city. The bot finds the city's coordinates and time zone, computes the birth chart and answers with the three placements people ask about first.
/chart 1990-07-15 14:30 London
London, England
Sun: Cancer
Moon: Aries
Rising: Scorpio
The positions come from AstroMansion's engine, which computes from NASA JPL data. The bot never guesses a sign from the date alone, so the Moon and the rising sign are right too.
What you need
- A bot token. Talk to @BotFather in Telegram, send
/newbotand keep the token it gives you. - An AstroMansion API key. Sign up on the developers page and copy the key from your account page.
- Two packages.
pip install python-telegram-bot astromansion
Put the two secrets in the environment, never in the code.
export TELEGRAM_TOKEN="123456:ABC..."
export ASTROMANSION_API_KEY="am_..."
The whole bot
import os
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
from astromansion import AsyncAstroMansion
astro = AsyncAstroMansion() # reads ASTROMANSION_API_KEY
places_seen = {} # city -> place, so a city is searched only once
async def find_place(city):
"""Return a city's coordinates and time zone, from memory when it was asked before."""
key = city.casefold()
if key not in places_seen:
found = await astro.search_places(q=city, limit=1)
places_seen[key] = found.results[0] if found.results else None
return places_seen[key]
async def chart(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Answer /chart YYYY-MM-DD HH:MM City with the Sun, Moon and rising signs."""
if len(context.args) < 3:
await update.message.reply_text("Usage: /chart 1990-07-15 14:30 London")
return
date, time, city = context.args[0], context.args[1], " ".join(context.args[2:])
place = await find_place(city)
if place is None:
await update.message.reply_text(f"I could not find {city}.")
return
natal = await astro.natal(
date=date, time=time, lat=place.lat, lon=place.lon, timezone=place.timezone
)
big_three = natal.summary
await update.message.reply_text(
f"{place.label}\n"
f"Sun: {big_three.Sun.sign}\n"
f"Moon: {big_three.Moon.sign}\n"
f"Rising: {big_three.ASC.sign}"
)
async def close_client(app: Application) -> None:
"""Close the API client's connections when the bot stops."""
await astro.aclose()
app = Application.builder().token(os.environ["TELEGRAM_TOKEN"]).post_shutdown(close_client).build()
app.add_handler(CommandHandler("chart", chart))
app.run_polling()
Run it with python bot.py, open the bot in Telegram and send a /chart command.
How it works
- The place.
search_placesturns a city's name into its latitude, longitude and IANA time zone. It also understands Turkish names, such as Londra. - The time zone. The bot passes the zone's name, such as Europe/London, not an offset. The API applies the daylight saving rules in force on the birth date, so a summer birth in 1990 gets its summer time.
- The chart.
natalreturns the whole chart. The bot reads onlysummary, which holds the Sun, the Moon, the ascendant and the Midheaven, each with its sign and degree. - The async client.
AsyncAstroMansionlets the bot answer other readers while it waits for the API, andaclosereleases its connections at shutdown.
Staying inside the free plan
The free plan gives 100 requests a month, at most 3 a minute and 30 a day. A birth chart costs one request, and a place search counts as six, since it searches the whole gazetteer. A command with a new city therefore costs seven requests, and one with a city the bot has already seen costs one.
That is why the bot keeps every place it found in places_seen. For a bot with real users, keep them in a small database instead, so they survive a restart. When the bot outgrows the free plan, the Basic plan gives 2,000 requests a month at 30 a minute for $19, as the pricing page shows.
Where to take it next
- A chart image. The SDK's
render_pngdraws the wheel, and Telegram'sreply_photosends it. Chart images open on the paid plans. - Daily transits.
transitslists the planets' aspects to a saved chart, a natural morning message. - Compatibility. Two births give a score from 0 to 100, as the compatibility guide shows.
- An AI assistant instead of commands. Claude and ChatGPT can call the same engine through MCP, as the MCP guide explains.