Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,16 @@
|
|
1 |
import asyncio
|
2 |
-
from flask import Flask
|
3 |
from threading import Thread
|
4 |
import random
|
5 |
import time
|
6 |
import requests
|
7 |
import logging
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
from pyrogram import Client
|
11 |
from config import API_ID, API_HASH, BOT_TOKEN, REPL_URL
|
@@ -16,58 +21,21 @@ app = Flask("")
|
|
16 |
def home():
|
17 |
return "You have found the home of a Python program!"
|
18 |
|
19 |
-
# Handle updates sent by Telegram
|
20 |
-
@app.route(f"/{BOT_TOKEN}", methods=["POST"])
|
21 |
-
def handle_updates():
|
22 |
-
update = json.loads(request.data)
|
23 |
-
# Do something with the update, e.g., process incoming messages
|
24 |
-
# For example, you can forward incoming messages to another chat:
|
25 |
-
# app.send_message(chat_id="@your_chat", text=update["message"]["text"])
|
26 |
-
return "OK"
|
27 |
-
|
28 |
def run():
|
29 |
app.run()
|
30 |
|
31 |
-
def set_webhook():
|
32 |
-
# Set the webhook URL to your bot on Telegram's server
|
33 |
-
url = f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook"
|
34 |
-
data = {"url": f"https://manishx-genatoz.hf.space/{BOT_TOKEN}"}
|
35 |
-
r = requests.post(url, json=data)
|
36 |
-
if r.status_code != 200:
|
37 |
-
print(f"Failed to set webhook: {r.text}")
|
38 |
-
return False
|
39 |
-
print("Webhook set successfully")
|
40 |
-
return True
|
41 |
-
|
42 |
if __name__ == '__main__':
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
except:
|
53 |
-
print("Could not apply uvloop on project")
|
54 |
-
|
55 |
-
# Defining path to plugins
|
56 |
-
plugins = dict(root="plugins")
|
57 |
-
|
58 |
-
# Defining the pyrogram client's instance
|
59 |
-
client = Client("UploadBot",
|
60 |
-
api_id=API_ID,
|
61 |
-
api_hash=API_HASH,
|
62 |
-
bot_token=BOT_TOKEN,
|
63 |
-
plugins=plugins)
|
64 |
-
|
65 |
-
# Start the Flask app in a separate thread
|
66 |
t = Thread(target=run)
|
67 |
t.start()
|
68 |
|
69 |
-
# Start
|
70 |
-
|
71 |
-
|
72 |
-
# Keep the client running
|
73 |
-
client.idle()
|
|
|
1 |
import asyncio
|
2 |
+
from flask import Flask
|
3 |
from threading import Thread
|
4 |
import random
|
5 |
import time
|
6 |
import requests
|
7 |
import logging
|
8 |
+
|
9 |
+
# uvloop is optional, but it's recommended to install it for better performance of pyrogram
|
10 |
+
try:
|
11 |
+
import uvloop
|
12 |
+
except:
|
13 |
+
print("uvloop is not installed")
|
14 |
|
15 |
from pyrogram import Client
|
16 |
from config import API_ID, API_HASH, BOT_TOKEN, REPL_URL
|
|
|
21 |
def home():
|
22 |
return "You have found the home of a Python program!"
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def run():
|
25 |
app.run()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
if __name__ == '__main__':
|
28 |
+
# Set webhook for bot
|
29 |
+
bot = Client("UploadBot",
|
30 |
+
api_id=API_ID,
|
31 |
+
api_hash=API_HASH,
|
32 |
+
bot_token=BOT_TOKEN)
|
33 |
+
bot.start()
|
34 |
+
bot.set_webhook(url=f"{REPL_URL}/{BOT_TOKEN}")
|
35 |
+
|
36 |
+
# Start Flask app in a separate thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
t = Thread(target=run)
|
38 |
t.start()
|
39 |
|
40 |
+
# Start bot polling
|
41 |
+
bot.polling(interval=0, timeout=30)
|
|
|
|
|
|