A ready-to-run starting point for building a Telegram AI agent in Python. If you're looking for how to build a Telegram bot that answers with an AI model, clone this template, add your bot token, plug in your model, and run — the messaging plumbing (receiving updates, replying in the right chat, deduping) is handled by Caspian, so all you write is one handler.
- Use this template — click "Use this template" at the top of this repo (or clone it), then
cdinto your new project. - Install dependencies:
pip install -r requirements.txt
- Get a Telegram bot token — message @BotFather on Telegram, send
/newbot, and copy the token it gives you. - Get a Caspian API key — copy
.env.exampleto.env, then fill it in:Paste your bot token intocp .env.example .env pipx run caspian-cli init # writes your CASPIAN_API_KEYTELEGRAM_BOT_TOKENin the same.env. - Run it:
Message your bot on Telegram — it replies. Now open
python main.py
main.pyand replace the placeholder inrespond()with your model call.
CommClient()readsCASPIAN_API_KEYandCASPIAN_BASE_URLfrom your environment or.env.client.connect_telegram(bot_token=...)connects your bot — no webhook or server to host.- The
@client.on_messagehandler fires for every inbound message;message.textis what the user sent. message.reply(...)answers in the same chat.client.listen()runs the loop for the lifetime of your agent.
The template ships with an echo placeholder. In main.py, the respond(text) -> str function is the one spot to change:
def respond(text: str) -> str:
# PLUG YOUR LLM CALL HERE
reply = my_model.generate(text)
return replyIt's provider-neutral — use any model or agent framework you like.
The same handler works across every channel Caspian supports — you just add one more connect_* call. For example, to also answer on Discord:
client.connect_discord(bot_token=os.environ["DISCORD_BOT_TOKEN"])No new handler code. Email, Slack, WhatsApp, SMS, and more work the same way.
- Main repo: github.com/TryCaspian/caspian-sdk — star it if useful.
- Docs: trycaspianai.com
MIT — see LICENSE.