File size: 2,655 Bytes
479470a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from fastapi import FastAPI, BackgroundTasks
from fastapi.responses import JSONResponse
import uvicorn
from typing import Optional
from telegram_bot import WeBookBot
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Create FastAPI app
app = FastAPI(title="WeBook Bot API", description="API for controlling the WeBook Telegram Bot")

# Global bot instance
bot: Optional[WeBookBot] = None

@app.on_event("startup")
async def startup_event():
    """Initialize bot on startup"""
    global bot
    token = os.getenv('TELEGRAM_BOT_TOKEN')
    if not token:
        raise ValueError("TELEGRAM_BOT_TOKEN not found in environment variables")
    bot = WeBookBot(token)

@app.on_event("shutdown")
async def shutdown_event():
    """Cleanup on shutdown"""
    global bot
    if bot and bot.is_running:
        await bot.stop_bot()

@app.post("/startbot")
async def start_bot(background_tasks: BackgroundTasks):
    """Start the Telegram bot"""
    global bot
    if not bot:
        return JSONResponse(
            status_code=500,
            content={"status": "error", "message": "Bot not initialized"}
        )

    if bot.is_running:
        return JSONResponse(
            status_code=200,
            content={"status": "success", "message": "Bot is already running"}
        )

    # Start bot in background
    background_tasks.add_task(bot.start_bot)
    
    return JSONResponse(
        status_code=200,
        content={"status": "success", "message": "Bot starting in background"}
    )

@app.post("/stopbot")
async def stop_bot():
    """Stop the Telegram bot"""
    global bot
    if not bot:
        return JSONResponse(
            status_code=500,
            content={"status": "error", "message": "Bot not initialized"}
        )

    success, message = await bot.stop_bot()
    
    return JSONResponse(
        status_code=200 if success else 500,
        content={"status": "success" if success else "error", "message": message}
    )

@app.get("/botstatus")
async def get_bot_status():
    """Get the current status of the bot"""
    global bot
    if not bot:
        return JSONResponse(
            status_code=500,
            content={"status": "error", "message": "Bot not initialized"}
        )

    return JSONResponse(
        status_code=200,
        content={
            "status": "success",
            "is_running": bot.is_running,
            "message": "Bot is running" if bot.is_running else "Bot is stopped"
        }
    )

if __name__ == "__main__":
    # Run FastAPI app
    uvicorn.run(
        "api:app",
        host="0.0.0.0",
        port=7860,
        reload=True
    )