-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
112 lines (95 loc) · 4.51 KB
/
bot.py
File metadata and controls
112 lines (95 loc) · 4.51 KB
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
102
103
104
105
106
107
108
109
110
111
112
import os
import asyncio
from dotenv import load_dotenv
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
from flask import Flask, request, jsonify
import logging
logging.basicConfig(level=logging.DEBUG)
# Load environment variables
load_dotenv()
TOKEN = os.getenv("BOT_TOKEN")
PORT = int(os.getenv("PORT", 8000))
RENDER_URL = os.getenv("RENDER_URL") # Webhook URL from Render
MODE = os.getenv("MODE", "polling")
if not TOKEN:
raise ValueError("BOT_TOKEN environment variable is missing!")
# Initialize Flask App & Telegram Bot Application
app = Flask(__name__)
application = Application.builder().token(TOKEN).build()
# Define bot commands
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends a welcome message with available commands."""
await update.message.reply_text(
"🎬 Welcome to the Film Making Department 🎬\n\n"
"/help - Contact my team for more info\n"
"/content - View sample direction styles\n"
"/contact - Reach out to my team 📞 9542862232\n"
"/chitti_video - Chitti cover song 🎶\n"
"/kingfisher_video - Kingfisher short film 🎥\n"
"/amrutha_video - Amrutha cover song 🎵"
)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends help information."""
await update.message.reply_text(
"💡 **Help Center** 💡\n\n"
"/start - Restart the bot\n"
"/content - View amazing direction styles\n"
"/contact - 📞 Call my team at 9542862232\n"
"/chitti_video - Chitti cover song 🎶\n"
"/kingfisher_video - Kingfisher short film 🎥\n"
"/amrutha_video - Amrutha cover song 🎵"
)
async def content(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends YouTube link for film content."""
await update.message.reply_text("🎥 Check out our playlists: https://www.youtube.com/@localtouringtalkiesltt4999")
async def chitti_video(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends the Chitti cover song link."""
await update.message.reply_text("🎶 Chitti Cover Song: https://www.youtube.com/watch?v=ro77dYAYmGM")
async def kingfisher_video(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends the Kingfisher short film link."""
await update.message.reply_text("🎬 Kingfisher Short Film: https://www.youtube.com/watch?v=6N10zpHvS9I")
async def amrutha_video(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends the Amrutha cover song link."""
await update.message.reply_text("🎵 Amrutha Cover Song: https://www.youtube.com/watch?v=lIo46bYftZE")
# Register command handlers
application.add_handler(CommandHandler('start', start))
application.add_handler(CommandHandler('help', help_command))
application.add_handler(CommandHandler('content', content))
application.add_handler(CommandHandler('chitti_video', chitti_video))
application.add_handler(CommandHandler('kingfisher_video', kingfisher_video))
application.add_handler(CommandHandler('amrutha_video', amrutha_video))
# Webhook Update Handler
async def webhook_update(update_data):
"""Handles incoming Telegram updates from webhook."""
update = Update.de_json(update_data, application.bot)
if not application._initialized:
await application.initialize()
await application.process_update(update)
# Flask Routes
@app.route("/", methods=["GET"])
def health_check():
"""Health check endpoint to verify the server is running."""
return jsonify({"status": "Bot is running!"}), 200
@app.route(f"/{TOKEN}", methods=["POST"])
async def webhook():
"""Receives updates from Telegram Webhook."""
try:
update_data = request.get_json()
logging.info(f"Received update: {update_data}")
await webhook_update(update_data)
return jsonify({"status": "OK"}), 200
except Exception as e:
logging.error(f"Error processing webhook: {str(e)}", exc_info=True)
return jsonify({"status": "error", "message": str(e)}), 500
# Fix: Assign the Flask app as a WSGI application callable for Gunicorn
get_app = app
if __name__ == "__main__":
print(f"Starting bot in {MODE} mode on port {PORT}...")
if MODE == "polling":
print("Bot is running in polling mode...")
application.run_polling()
else:
print(f"Bot is running in webhook mode on {RENDER_URL}")
asyncio.run(application.bot.setWebhook(f"{RENDER_URL}/{TOKEN}"))
app.run(host="0.0.0.0", port=PORT)