DEV Community

Castanderness
Castanderness

Posted on

Auto-Posting Telegram Channel Bot with APScheduler and aiogram 3

Auto-Posting Telegram Channel Bot

Post on a schedule. Works for crypto updates, news digests, daily tips.

Setup

from aiogram import Bot
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import asyncio

bot = Bot(token=TOKEN)
scheduler = AsyncIOScheduler(timezone="UTC")
CHANNEL = "@yourchannel"
Enter fullscreen mode Exit fullscreen mode

Scheduled Posts

@scheduler.scheduled_job("cron", hour=9, minute=0)
async def morning_post():
    await bot.send_message(CHANNEL, get_daily_digest())

@scheduler.scheduled_job("interval", hours=6)
async def crypto_update():
    import requests
    prices = requests.get(
        "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd"
    ).json()
    btc = prices["bitcoin"]["usd"]
    eth = prices["ethereum"]["usd"]
    await bot.send_message(CHANNEL, f"BTC: ${btc:,.0f} | ETH: ${eth:,.0f}")
Enter fullscreen mode Exit fullscreen mode

Run Forever

async def main():
    scheduler.start()
    await asyncio.Event().wait()

asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

Full source: https://github.com/Castanderness/telegram-bots-portfolio

Custom bot: https://castanderness.github.io/telegram-bots-portfolio/portfolio/ | from $49

Top comments (0)