Agent recipe ยท 04

Telegram bot on Chinese models

Build a small Telegram webhook worker that forwards messages to an AIWave model and sends the answer back. The provider key and bot token stay server-side.

Intermediate25 minutesEnglishVerified 2026-09-25
Create AIWAVE_API_KEY and TELEGRAM_BOT_TOKEN as server environment variables. Do not paste either value into the bot source or a public webhook URL.

1. The 60-second version

from flask import Flask, request
from openai import OpenAI
import os, requests
app = Flask(__name__)
ai = OpenAI(base_url="https://aiwave.live/v1", api_key=os.environ["AIWAVE_API_KEY"])
BOT = os.environ["TELEGRAM_BOT_TOKEN"]

@app.post("/telegram/webhook")
def webhook():
    update = request.get_json()
    message = update.get("message", {})
    chat_id = message.get("chat", {}).get("id")
    text = message.get("text", "")
    if not chat_id or not text: return {"ok": True}
    answer = ai.chat.completions.create(model="qwen3.5-plus", messages=[{"role":"user","content":text}], max_tokens=300).choices[0].message.content
    requests.post(f"https://api.telegram.org/bot{BOT}/sendMessage", json={"chat_id":chat_id,"text":answer}, timeout=10)
    return {"ok": True}

Run behind HTTPS, register the webhook with Telegram, then send one non-sensitive message. Add signature checks, retries, and rate limits before a public launch.

2. Per-message cost model

AssumptionEstimate
1,000 input + 300 output tokens on qwen3.5-plusabout $0.0012 per message
Rate sourceAIWave pricing, qwen3.5-plus, effective 2026-08-27; checked 2026-09-25

Network, Telegram, retries, and your billing group are outside this token estimate.

3. Extension and error links

Next steps