96Astro API

Documentation

Send a daily horoscope to Telegram

A small scheduled job that posts each sign's day to a Telegram channel.

This guide posts today's reading for each sign to a Telegram channel, once a day, from a scheduled job. It needs a Telegram bot token from BotFather, and your API key.

The job

import datetime as dt
import os
import httpx

API = "https://api.96astroapi.com/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['ASTRO_API_KEY']}"}
SIGNS = ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"]

today = dt.date.today().isoformat()
lines = []
for sign in SIGNS:
    res = httpx.post(f"{API}/predictions/sign-daily-areas", headers=HEADERS, json={"sign": sign, "date": today}, timeout=60)
    res.raise_for_status()
    areas = res.json()["data"]["areas"]
    lines.append(f"*{sign}*: {areas['profession']['text']} {areas['personal_life']['text']}")

httpx.post(
    f"https://api.telegram.org/bot{os.environ['TELEGRAM_TOKEN']}/sendMessage",
    json={"chat_id": os.environ["TELEGRAM_CHAT"], "text": "\n\n".join(lines), "parse_mode": "Markdown"},
    timeout=30,
).raise_for_status()

Run it once a day with cron, or any scheduler you already have.

Good to know

  • Each call costs 1 credit, so this is 12 credits a day, or about 360 a month.
  • sign-daily-areas gives six areas: personal_life, profession, health, emotions, travel and luck. Each has a rating out of 5 and a text.
  • A Telegram message is limited to 4,096 characters. Choose fewer areas, or send one message per sign, if you go over.
  • Send an Idempotency-Key with the date and sign if your scheduler can run a job twice. Then a repeat is never charged again.

On this page