Manishx commited on
Commit
f289e03
1 Parent(s): 8a15f00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -50
app.py CHANGED
@@ -1,11 +1,16 @@
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
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
- # 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()
 
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)