Mohammed Foud commited on
Commit ·
479470a
1
Parent(s): deb2780
Add application file
Browse files- Dockerfile +1 -1
- api.py +101 -0
- requirements.txt +3 -1
- telegram_bot.py +40 -14
Dockerfile
CHANGED
|
@@ -30,4 +30,4 @@ COPY . .
|
|
| 30 |
EXPOSE 7860
|
| 31 |
|
| 32 |
# Command to run the application
|
| 33 |
-
CMD ["python", "
|
|
|
|
| 30 |
EXPOSE 7860
|
| 31 |
|
| 32 |
# Command to run the application
|
| 33 |
+
CMD ["python", "api.py"]
|
api.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, BackgroundTasks
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import uvicorn
|
| 4 |
+
from typing import Optional
|
| 5 |
+
from telegram_bot import WeBookBot
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Load environment variables
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# Create FastAPI app
|
| 13 |
+
app = FastAPI(title="WeBook Bot API", description="API for controlling the WeBook Telegram Bot")
|
| 14 |
+
|
| 15 |
+
# Global bot instance
|
| 16 |
+
bot: Optional[WeBookBot] = None
|
| 17 |
+
|
| 18 |
+
@app.on_event("startup")
|
| 19 |
+
async def startup_event():
|
| 20 |
+
"""Initialize bot on startup"""
|
| 21 |
+
global bot
|
| 22 |
+
token = os.getenv('TELEGRAM_BOT_TOKEN')
|
| 23 |
+
if not token:
|
| 24 |
+
raise ValueError("TELEGRAM_BOT_TOKEN not found in environment variables")
|
| 25 |
+
bot = WeBookBot(token)
|
| 26 |
+
|
| 27 |
+
@app.on_event("shutdown")
|
| 28 |
+
async def shutdown_event():
|
| 29 |
+
"""Cleanup on shutdown"""
|
| 30 |
+
global bot
|
| 31 |
+
if bot and bot.is_running:
|
| 32 |
+
await bot.stop_bot()
|
| 33 |
+
|
| 34 |
+
@app.post("/startbot")
|
| 35 |
+
async def start_bot(background_tasks: BackgroundTasks):
|
| 36 |
+
"""Start the Telegram bot"""
|
| 37 |
+
global bot
|
| 38 |
+
if not bot:
|
| 39 |
+
return JSONResponse(
|
| 40 |
+
status_code=500,
|
| 41 |
+
content={"status": "error", "message": "Bot not initialized"}
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if bot.is_running:
|
| 45 |
+
return JSONResponse(
|
| 46 |
+
status_code=200,
|
| 47 |
+
content={"status": "success", "message": "Bot is already running"}
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Start bot in background
|
| 51 |
+
background_tasks.add_task(bot.start_bot)
|
| 52 |
+
|
| 53 |
+
return JSONResponse(
|
| 54 |
+
status_code=200,
|
| 55 |
+
content={"status": "success", "message": "Bot starting in background"}
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
@app.post("/stopbot")
|
| 59 |
+
async def stop_bot():
|
| 60 |
+
"""Stop the Telegram bot"""
|
| 61 |
+
global bot
|
| 62 |
+
if not bot:
|
| 63 |
+
return JSONResponse(
|
| 64 |
+
status_code=500,
|
| 65 |
+
content={"status": "error", "message": "Bot not initialized"}
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
success, message = await bot.stop_bot()
|
| 69 |
+
|
| 70 |
+
return JSONResponse(
|
| 71 |
+
status_code=200 if success else 500,
|
| 72 |
+
content={"status": "success" if success else "error", "message": message}
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
@app.get("/botstatus")
|
| 76 |
+
async def get_bot_status():
|
| 77 |
+
"""Get the current status of the bot"""
|
| 78 |
+
global bot
|
| 79 |
+
if not bot:
|
| 80 |
+
return JSONResponse(
|
| 81 |
+
status_code=500,
|
| 82 |
+
content={"status": "error", "message": "Bot not initialized"}
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
return JSONResponse(
|
| 86 |
+
status_code=200,
|
| 87 |
+
content={
|
| 88 |
+
"status": "success",
|
| 89 |
+
"is_running": bot.is_running,
|
| 90 |
+
"message": "Bot is running" if bot.is_running else "Bot is stopped"
|
| 91 |
+
}
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
# Run FastAPI app
|
| 96 |
+
uvicorn.run(
|
| 97 |
+
"api:app",
|
| 98 |
+
host="0.0.0.0",
|
| 99 |
+
port=7860,
|
| 100 |
+
reload=True
|
| 101 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
python-telegram-bot>=20.0
|
| 2 |
requests>=2.31.0
|
| 3 |
playwright>=1.40.0
|
| 4 |
-
python-dotenv>=1.0.0
|
|
|
|
|
|
|
|
|
| 1 |
python-telegram-bot>=20.0
|
| 2 |
requests>=2.31.0
|
| 3 |
playwright>=1.40.0
|
| 4 |
+
python-dotenv>=1.0.0
|
| 5 |
+
fastapi>=0.68.0
|
| 6 |
+
uvicorn>=0.15.0
|
telegram_bot.py
CHANGED
|
@@ -7,6 +7,7 @@ from datetime import datetime, timedelta
|
|
| 7 |
from monitor import EventMonitor
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
import os
|
|
|
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
|
@@ -36,8 +37,9 @@ class WeBookBot:
|
|
| 36 |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 37 |
}
|
| 38 |
self.monitor = EventMonitor(self)
|
| 39 |
-
# Add storage for user credentials
|
| 40 |
self.user_credentials = {}
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def _get_events_payload(self, category=None, date_filter=None, price_min=None, price_max=None, zones=None, country_code="SA", limit=20, skip=0):
|
| 43 |
"""Generate the payload for the events API request with flexible filtering using the getShows query."""
|
|
@@ -849,27 +851,51 @@ class WeBookBot:
|
|
| 849 |
response.raise_for_status()
|
| 850 |
return response.json()
|
| 851 |
|
| 852 |
-
def
|
| 853 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 854 |
try:
|
| 855 |
-
application = Application.builder().token(self.token).build()
|
| 856 |
|
| 857 |
# Add handlers
|
| 858 |
-
application.add_handler(CommandHandler("start", self.start_command))
|
| 859 |
-
application.add_handler(CommandHandler("setinfoaccountwebook", self.set_account_info_command))
|
| 860 |
-
application.add_handler(CommandHandler("stateloginwebook", self.account_status_command))
|
| 861 |
-
application.add_handler(CommandHandler("help", self.help_command))
|
| 862 |
-
application.add_handler(CommandHandler("info", self.info_command))
|
| 863 |
-
application.add_handler(CallbackQueryHandler(self.button_callback))
|
| 864 |
-
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_credentials_input))
|
| 865 |
|
| 866 |
# Start the bot
|
| 867 |
logger.info("Starting bot...")
|
| 868 |
-
application.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
|
| 870 |
except Exception as e:
|
| 871 |
logger.error(f"Error running bot: {str(e)}")
|
| 872 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
|
| 874 |
if __name__ == "__main__":
|
| 875 |
# Get bot token from environment variable
|
|
@@ -878,4 +904,4 @@ if __name__ == "__main__":
|
|
| 878 |
raise ValueError("TELEGRAM_BOT_TOKEN not found in environment variables")
|
| 879 |
|
| 880 |
bot = WeBookBot(BOT_TOKEN)
|
| 881 |
-
|
|
|
|
| 7 |
from monitor import EventMonitor
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
import os
|
| 10 |
+
import asyncio
|
| 11 |
|
| 12 |
# Load environment variables
|
| 13 |
load_dotenv()
|
|
|
|
| 37 |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 38 |
}
|
| 39 |
self.monitor = EventMonitor(self)
|
|
|
|
| 40 |
self.user_credentials = {}
|
| 41 |
+
self.application = None
|
| 42 |
+
self.is_running = False
|
| 43 |
|
| 44 |
def _get_events_payload(self, category=None, date_filter=None, price_min=None, price_max=None, zones=None, country_code="SA", limit=20, skip=0):
|
| 45 |
"""Generate the payload for the events API request with flexible filtering using the getShows query."""
|
|
|
|
| 851 |
response.raise_for_status()
|
| 852 |
return response.json()
|
| 853 |
|
| 854 |
+
async def start_bot(self):
|
| 855 |
+
"""Start the bot asynchronously"""
|
| 856 |
+
if self.is_running:
|
| 857 |
+
return False, "Bot is already running"
|
| 858 |
+
|
| 859 |
try:
|
| 860 |
+
self.application = Application.builder().token(self.token).build()
|
| 861 |
|
| 862 |
# Add handlers
|
| 863 |
+
self.application.add_handler(CommandHandler("start", self.start_command))
|
| 864 |
+
self.application.add_handler(CommandHandler("setinfoaccountwebook", self.set_account_info_command))
|
| 865 |
+
self.application.add_handler(CommandHandler("stateloginwebook", self.account_status_command))
|
| 866 |
+
self.application.add_handler(CommandHandler("help", self.help_command))
|
| 867 |
+
self.application.add_handler(CommandHandler("info", self.info_command))
|
| 868 |
+
self.application.add_handler(CallbackQueryHandler(self.button_callback))
|
| 869 |
+
self.application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_credentials_input))
|
| 870 |
|
| 871 |
# Start the bot
|
| 872 |
logger.info("Starting bot...")
|
| 873 |
+
await self.application.initialize()
|
| 874 |
+
await self.application.start()
|
| 875 |
+
await self.application.run_polling()
|
| 876 |
+
self.is_running = True
|
| 877 |
+
return True, "Bot started successfully"
|
| 878 |
|
| 879 |
except Exception as e:
|
| 880 |
logger.error(f"Error running bot: {str(e)}")
|
| 881 |
+
self.is_running = False
|
| 882 |
+
return False, f"Error starting bot: {str(e)}"
|
| 883 |
+
|
| 884 |
+
async def stop_bot(self):
|
| 885 |
+
"""Stop the bot"""
|
| 886 |
+
if not self.is_running:
|
| 887 |
+
return False, "Bot is not running"
|
| 888 |
+
|
| 889 |
+
try:
|
| 890 |
+
if self.application:
|
| 891 |
+
await self.application.stop()
|
| 892 |
+
await self.application.shutdown()
|
| 893 |
+
self.is_running = False
|
| 894 |
+
return True, "Bot stopped successfully"
|
| 895 |
+
return False, "No application instance found"
|
| 896 |
+
except Exception as e:
|
| 897 |
+
logger.error(f"Error stopping bot: {str(e)}")
|
| 898 |
+
return False, f"Error stopping bot: {str(e)}"
|
| 899 |
|
| 900 |
if __name__ == "__main__":
|
| 901 |
# Get bot token from environment variable
|
|
|
|
| 904 |
raise ValueError("TELEGRAM_BOT_TOKEN not found in environment variables")
|
| 905 |
|
| 906 |
bot = WeBookBot(BOT_TOKEN)
|
| 907 |
+
asyncio.run(bot.start_bot())
|