IntegrationsIntermediate

How to schedule Telegram channel posts with cron

Use a cron job and a small script to publish queued posts to a Telegram channel at fixed times.

8 minIntermediate

If you want a Telegram channel to post at 9 am every day without you touching anything, cron plus a posting script is the simplest setup that just works. This guide builds a queue of posts and a scheduler that drains one entry per run.

What you need

  • A bot that is an admin of your channel
  • A Linux or macOS machine that stays on (or a small VPS)
  • Node installed and your bot token in the environment

Step 1: Create a post queue

Keep your scheduled content in a plain JSON file. Each run pops the first item and posts it. This keeps the logic dead simple and easy to edit by hand.

queue.json
[
  { "text": "Monday tip: batch your DMs into one daily window." },
  { "text": "Tuesday tip: pin your best post for new subscribers." },
  { "text": "Wednesday tip: ask one question to spark replies." }
]

Step 2: Write the scheduler script

drain.mjs
import { readFile, writeFile } from "node:fs/promises";

const token = process.env.TELEGRAM_BOT_TOKEN;
const channel = "@my_channel";
const queue = JSON.parse(await readFile("queue.json", "utf8"));

if (queue.length === 0) {
  console.log("Queue empty, nothing to post.");
  process.exit(0);
}

const next = queue.shift();
const res = await fetch(
  `https://api.telegram.org/bot${token}/sendMessage`,
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ chat_id: channel, text: next.text }),
  }
);
const data = await res.json();
if (!data.ok) throw new Error(data.description);

await writeFile("queue.json", JSON.stringify(queue, null, 2));
console.log("Posted and removed one item. Remaining:", queue.length);
Edit the queue any time
Because the queue is just a file, you can add, reorder, or remove future posts whenever you like. The next cron run picks up your changes automatically.

Step 3: Add the cron entry

Open your crontab with crontab -e and add a line that runs the script every weekday at 9 am. Load your token from a file the cron environment can read.

crontab
# minute hour day month weekday  command
0 9 * * 1-5 cd /home/me/tg && /usr/bin/node --env-file=.env drain.mjs >> post.log 2>&1
crontab - active jobs
$ crontab -l
0 9 * * 1-5 cd /home/me/tg && node --env-file=.env drain.mjs
Job will fire Mon-Fri at 09:00 server time.
Run crontab -l to confirm the job is registered.
Mind the time zone
Cron uses the server's local time. Check it with the date command and either set the server time zone or adjust the hour so posts land when your audience is awake.

Result

Every weekday morning the scheduler posts the next queued item and shortens the file by one. Top up the queue once a week and your channel runs itself.

Watch related tutorials

Tags
#telegram#cron#scheduling#auto-post#channel