-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGapp.py
114 lines (94 loc) · 5.3 KB
/
Gapp.py
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
113
114
from flask import Flask, request
import requests
app = Flask(__name__)
TELEGRAM_TOKEN = "7808291028:AAGRsVUGT2id7yrO_XaRPlYBtoYLYb_jzcg"
TELEGRAM_API = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
ADMIN_ID = 5943119285 # Admin ID
CHANNEL_ID = -1002403053494 # Private Channel ID
# Store current admin video state
admin_state = {}
@app.route("/", methods=["POST", "GET"])
def index():
if request.method == "POST":
data = request.json
chat_id = data["message"]["chat"]["id"]
text = data["message"].get("text")
user_id = data["message"]["from"]["id"]
# Admin Check: If the user is the admin
if user_id == ADMIN_ID:
if text == "/start":
# Send greeting to admin
response_text = "Hello My Dear Friend. You are My admin. How are you My God. Do you want to upload a movie? Please Send a video or any File."
requests.post(f"{TELEGRAM_API}/sendMessage", json={"chat_id": chat_id, "text": response_text})
elif "video" in data["message"]:
# If admin sends a video, save it to the channel
video_file_id = data["message"]["video"]["file_id"]
file_path = get_file_path(video_file_id)
# Send video to channel
send_video_to_channel(file_path)
# Ask for movie details
admin_state[user_id] = {"movie_video": file_path, "waiting_for_movie_name": True}
response_text = "It is looking nice movie! My God. Please provide this movie name."
requests.post(f"{TELEGRAM_API}/sendMessage", json={"chat_id": chat_id, "text": response_text})
elif user_id in admin_state and admin_state[user_id].get("waiting_for_movie_name"):
# Handle movie name input from admin
movie_name = text
admin_state[user_id]["movie_name"] = movie_name
admin_state[user_id]["waiting_for_movie_name"] = False
admin_state[user_id]["waiting_for_date"] = True
response_text = f"Your Provided Movie Name - {movie_name}, Now provide the Date (DD/MM/YYYY) when it was released."
requests.post(f"{TELEGRAM_API}/sendMessage", json={"chat_id": chat_id, "text": response_text})
elif user_id in admin_state and admin_state[user_id].get("waiting_for_date"):
# Validate and handle movie release date input from admin
if validate_date_format(text):
release_date = text
admin_state[user_id]["release_date"] = release_date
send_movie_details_to_google_script(admin_state[user_id])
# Reset state after sending details
del admin_state[user_id]
response_text = "Movie details submitted successfully!"
requests.post(f"{TELEGRAM_API}/sendMessage", json={"chat_id": chat_id, "text": response_text})
else:
response_text = "Invalid date format. Please provide the date in DD/MM/YYYY format."
requests.post(f"{TELEGRAM_API}/sendMessage", json={"chat_id": chat_id, "text": response_text})
else:
# If admin sends any other video before providing all details, reject the video
if "video" in data["message"]:
response_text = "Please complete the details for the previous video before uploading a new one. Or type /cancel to cancel."
requests.post(f"{TELEGRAM_API}/sendMessage", json={"chat_id": chat_id, "text": response_text})
else:
# Non-admin user message handling
response_text = "You are not authorized to interact with this bot."
requests.post(f"{TELEGRAM_API}/sendMessage", json={"chat_id": chat_id, "text": response_text})
return {"status": "ok"}
return "Telegram bot is running!"
def get_file_path(file_id):
"""Fetch the file path from Telegram server."""
file_info = requests.get(f"{TELEGRAM_API}/getFile", params={"file_id": file_id}).json()
file_path = file_info["result"]["file_path"]
return f"https://api.telegram.org/file/bot{TELEGRAM_TOKEN}/{file_path}"
def send_video_to_channel(file_path):
"""Send video to the private channel."""
response = requests.post(f"{TELEGRAM_API}/sendVideo", json={"chat_id": CHANNEL_ID, "video": file_path})
if response.ok:
result = response.json()
return result["result"]["message_id"]
def validate_date_format(date_str):
"""Validate date format as DD/MM/YYYY."""
try:
from datetime import datetime
datetime.strptime(date_str, "%d/%m/%Y")
return True
except ValueError:
return False
def send_movie_details_to_google_script(movie_details):
"""Send movie details to Google Script via POST request."""
movie_data = {
"Movie Name": movie_details["movie_name"],
"Massage Id": movie_details["movie_video"],
"Date Added": movie_details["release_date"]
}
google_script_url = "https://script.google.com/macros/s/AKfycbzixknSdjyAV62WD95udegTHZsYMC27cPQnB0FqysnIjAWV2qvNRJPwnpFho8TOcCXf/exec"
requests.post(google_script_url, json=movie_data)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)