Docs Integrations

Telegram astrology bot

A bot that answers /horoscope leo with the day's reading and /panchang Delhi with the day's panchang. You need a bot token from Telegram's BotFather, one AstroLogics key, and a small Node.js program on a server.

Pick the path first

You want Build AstroLogics calls
/horoscope leo returns the day's reading A command bot (this guide) getHoroscope at 1 credit
Free questions like "what does my chart say about work?" An AI bot Our MCP servers; see the AI chatbot guide

A command bot is cheaper and gives the same answer every time. Start with it, and add the AI path when people start typing questions.

Step 1: Prove the call

Before Telegram is involved, check that your key works:

curl 'https://api.astrologics.in/v1/horoscope/leo?period=weekly&date=2026-10-02&lang=hi' \
  -H 'Authorization: Bearer al_test_YOUR_KEY'

data.reading is one paragraph, ready to send. data.sign_label is the sign's name in the language you asked for, so with lang=hi both are in Hindi.

Step 2: Create the bot

  1. In Telegram, open a chat with @BotFather and send /newbot.
  2. Give the bot a name, then a username that ends in bot.
  3. BotFather replies with a token. Keep it secret: anyone with it controls your bot.
  4. Optionally, send /setcommands to BotFather so the commands show in the menu:
horoscope - The day's reading for a sign, e.g. /horoscope leo
panchang - The day's panchang for a city, e.g. /panchang Delhi

Step 3: Write the bot

Node.js 18 or later, with no packages. The bot asks Telegram for new messages (long polling), so it needs no public address.

// bot.mjs: run with TELEGRAM_BOT_TOKEN=... ASTROLOGICS_API_KEY=... node bot.mjs
const TG = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}`;
const API = "https://api.astrologics.in/v1";
const KEY = process.env.ASTROLOGICS_API_KEY;
const SIGNS = ["aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces"];
const today = () => new Date().toLocaleDateString("en-CA", { timeZone: "Asia/Kolkata" });   // YYYY-MM-DD in India
const cache = new Map();   // a reading is the same for everyone: fetch each sign once a day

async function astro(path) {
  const res = await fetch(API + path, { headers: { Authorization: `Bearer ${KEY}` } });
  const body = await res.json();
  if (!res.ok) throw new Error(body.error ? body.error.message : `HTTP ${res.status}`);
  return body.data;
}

async function horoscope(sign, lang) {
  const key = `${sign}:${lang}:${today()}`;
  if (!cache.has(key)) cache.set(key, await astro(`/horoscope/${sign}?period=daily&date=${today()}&lang=${lang}`));
  const d = cache.get(key);
  return `${d.sign_label}, ${d.date}\n\n${d.reading}`;
}

async function panchang(city) {
  const places = await astro(`/places/search?q=${encodeURIComponent(city)}&country=IN&limit=1`);
  const place = places.results[0];
  if (!place) return `I could not find ${city}.`;
  const p = await astro(`/panchang?date=${today()}&place_id=${place.place_id}`);
  const time = (iso) => iso.slice(11, 16);
  return `Panchang for ${place.name}, ${p.date}\n\nTithi: ${p.tithi.label} (${p.tithi.paksha})\nNakshatra: ${p.nakshatra.label}\nYoga: ${p.yoga.label}\nSunrise ${time(p.sunrise)}, sunset ${time(p.sunset)}\nRahu kaal: ${time(p.rahu_kaal.start)}–${time(p.rahu_kaal.end)}`;
}

async function reply(chatId, text) {
  await fetch(`${TG}/sendMessage`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ chat_id: chatId, text }) });
}

async function handle(msg) {
  const [cmd, ...args] = msg.text.trim().split(/\s+/);
  const command = cmd.toLowerCase().split("@")[0];   // "/horoscope@YourBot" in groups
  if (command === "/horoscope") {
    const sign = (args[0] || "").toLowerCase(), lang = args[1] === "hi" ? "hi" : "en";
    return reply(msg.chat.id, SIGNS.includes(sign) ? await horoscope(sign, lang) : "Send a sign, e.g. /horoscope leo (add hi for Hindi).");
  }
  if (command === "/panchang") return reply(msg.chat.id, args.length ? await panchang(args.join(" ")) : "Send a city, e.g. /panchang Delhi");
  if (command === "/start") return reply(msg.chat.id, "Try /horoscope leo or /panchang Delhi.");
}

let offset = 0;
for (;;) {
  const res = await fetch(`${TG}/getUpdates?timeout=30&offset=${offset}`).then((r) => r.json()).catch(() => ({ result: [] }));
  for (const u of res.result || []) {
    offset = u.update_id + 1;
    const msg = u.message;
    if (msg && msg.text) handle(msg).catch((e) => reply(msg.chat.id, `Sorry, that did not work: ${e.message}`));
  }
}

Send /horoscope leo to your bot. With a test key the reply costs nothing.

Step 4: Go live

  • Swap in a live key (al_live_) when you are ready. Calls are then charged at the credits shown in the reference.
  • Run it as a service (systemd, pm2 or a container) so it restarts after a crash or a reboot.
  • A webhook instead of polling: Telegram can POST each message to your HTTPS address instead; see setWebhook in its documentation. A bot cannot use both at once.

Costs

Command Calls Credits
/horoscope leo one getHoroscope per sign, language and day (then cached) 1
/panchang Delhi searchPlaces, then getPanchang 1 + 1

Keep the place each city resolves to, and you save the search on the next request.

Gotchas

  • Readings are shared. Everyone with the same sign, period, date and language gets the same reading, so a busy bot still makes at most 12 calls a day per language.
  • Dates are in India time in this example. Use your audience's time zone if it is elsewhere.
  • Groups: with privacy mode on (the default), a bot in a group sees only commands addressed to it, which is all this bot needs.
  • Plain text: this example sends plain text. If you switch on parse_mode, escape the reading first, or a stray character makes Telegram refuse the message.
  • Places: a city name can match several places; country=IN ranks places in India first. Pass lat and lon instead if you already have them.

What to build next