Manishx commited on
Commit
3c468c3
·
verified ·
1 Parent(s): 1963b93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -46
app.py CHANGED
@@ -1,18 +1,13 @@
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
17
 
18
  app = Flask("")
@@ -21,50 +16,58 @@ app = Flask("")
21
  def home():
22
  return "You have found the home of a Python program!"
23
 
 
 
 
 
 
 
 
 
 
24
  def run():
25
  app.run()
26
 
27
- # def ping(target, debug):
28
- # while True:
29
- # r = requests.get(target)
30
- # if debug == True:
31
- # print("Status Code: " + str(r.status_code))
32
- # time.sleep(random.randint(
33
- # 180, 300)) # alternate ping time between 3 and 5 minutes
34
-
35
- # def awake(target, debug=False):
36
- # log = logging.getLogger("werkzeug")
37
- # log.disabled = True
38
- # app.logger.disabled = True
39
- # t = Thread(target=run)
40
- # r = Thread(
41
- # target=ping,
42
- # args=(
43
- # target,
44
- # debug,
45
- # ),
46
- # )
47
- # t.start()
48
- # r.start()
49
 
50
  if __name__ == '__main__':
51
 
52
- # # If you are deploying on Replit, you can use this code to keep your bot alive
53
- # if 'y' in input('Are you deploying on Replit? (y/n): ').lower():
54
- # awake(REPL_URL, False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # Setting up uvloop
57
- try:
58
- uvloop.install()
59
- except:
60
- print("Could not apply uvloop on project")
61
 
62
- # Defining path to plugins
63
- plugins = dict(root="plugins")
64
 
65
- # Defining the pyrogram client's instance
66
- Client("UploadBot",
67
- api_id=API_ID,
68
- api_hash=API_HASH,
69
- bot_token=BOT_TOKEN,
70
- plugins=plugins).run()
 
1
  import asyncio
2
+ from flask import Flask, request
3
  from threading import Thread
4
  import random
5
  import time
6
  import requests
7
  import logging
8
+ import json
9
 
10
+ from pyrogram import Client, Filters
 
 
 
 
 
 
11
  from config import API_ID, API_HASH, BOT_TOKEN, REPL_URL
12
 
13
  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
+ # Set the webhook URL
45
+ if not set_webhook():
46
+ exit(1)
47
+
48
+ # Setting up uvloop
49
+ try:
50
+ import uvloop
51
+ uvloop.install()
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 the Pyrogram client
70
+ client.start()
71
 
72
+ # Keep the client running
73
+ client.idle()