ai-system / backend /telegram_bot.py
Evogoatml's picture
fix: backend now regular folder, not submodule
65464fc
import os
import asyncio
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
from backend.router_crypto import get_price
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hello, I'm your AI System bot. Use /price BTC USD to get crypto prices.")
async def handle_price(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
args = context.args
base = args[0].upper() if len(args) > 0 else "BTC"
quote = args[1].upper() if len(args) > 1 else "USD"
data = get_price(base, quote)
if "error" in data:
await update.message.reply_text("Error fetching data.")
else:
await update.message.reply_text(f"1 {base} = {data['rate']} {quote}")
except Exception as e:
await update.message.reply_text(f"Error: {e}")
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(f"You said: {update.message.text}")
async def start_bot():
app = ApplicationBuilder().token(BOT_TOKEN).build()
backend.add_handler(CommandHandler("start", start))
backend.add_handler(CommandHandler("price", handle_price))
backend.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
print("Telegram bot running...")
await backend.run_polling()