Spaces:
Sleeping
Sleeping
import asyncio | |
from flask import Flask, request | |
from threading import Thread | |
import random | |
import time | |
import requests | |
import logging | |
import json | |
from pyrogram import Client | |
from config import API_ID, API_HASH, BOT_TOKEN, REPL_URL | |
app = Flask("") | |
def home(): | |
return "You have found the home of a Python program!" | |
# Handle updates sent by Telegram | |
def handle_updates(): | |
update = json.loads(request.data) | |
# Do something with the update, e.g., process incoming messages | |
# For example, you can forward incoming messages to another chat: | |
# app.send_message(chat_id="@your_chat", text=update["message"]["text"]) | |
return "OK" | |
def run(): | |
app.run() | |
def set_webhook(): | |
# Set the webhook URL to your bot on Telegram's server | |
url = f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook" | |
data = {"url": f"https://manishx-genatoz.hf.space/{BOT_TOKEN}"} | |
r = requests.post(url, json=data) | |
if r.status_code != 200: | |
print(f"Failed to set webhook: {r.text}") | |
return False | |
print("Webhook set successfully") | |
return True | |
if __name__ == '__main__': | |
# Set the webhook URL | |
if not set_webhook(): | |
exit(1) | |
# Setting up uvloop | |
try: | |
import uvloop | |
uvloop.install() | |
except: | |
print("Could not apply uvloop on project") | |
# Defining path to plugins | |
plugins = dict(root="plugins") | |
# Defining the pyrogram client's instance | |
client = Client("UploadBot", | |
api_id=API_ID, | |
api_hash=API_HASH, | |
bot_token=BOT_TOKEN, | |
plugins=plugins) | |
# Start the Flask app in a separate thread | |
t = Thread(target=run) | |
t.start() | |
# Start the Pyrogram client | |
client.start() | |
# Keep the client running | |
client.idle() | |