Spaces:
Running
Running
import sys | |
import asyncio | |
import logging | |
import traceback | |
import logging.handlers as handlers | |
from FileStream.config import Telegram, Server | |
from aiohttp import web | |
from pyrogram import idle | |
from FileStream.bot import FileStream | |
from FileStream.Tools import Time_ISTKolNow | |
from FileStream.server import web_server | |
from FileStream.bot.clients import initialize_clients | |
# Configure logging | |
logging.basicConfig( | |
level=logging.INFO, | |
datefmt="%d/%m/%Y %H:%M:%S", | |
format='[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s', | |
handlers=[ | |
logging.StreamHandler(stream=sys.stdout), | |
handlers.RotatingFileHandler("streambot.log", mode="a", maxBytes=104857600, backupCount=2, encoding="utf-8") | |
], | |
) | |
logging.getLogger("aiohttp").setLevel(logging.ERROR) | |
logging.getLogger("pyrogram").setLevel(logging.ERROR) | |
logging.getLogger("aiohttp.web").setLevel(logging.ERROR) | |
server = web.AppRunner(web_server()) | |
async def start_services(): | |
print( | |
"****\n" | |
+ ( | |
"------------------ Starting as Secondary Server ------------------" | |
if Telegram.SECONDARY | |
else "------------------- Starting as Primary Server -------------------" | |
) | |
+ "\n****\n-------------------- Initializing Telegram Bot --------------------" | |
) | |
await FileStream.start() | |
bot_info = await FileStream.get_me() | |
FileStream.id = bot_info.id | |
FileStream.username = bot_info.username | |
FileStream.fname = bot_info.first_name | |
print("------------------------------ DONE ------------------------------\n\n") | |
print("---------------------- Initializing Clients ----------------------") | |
await initialize_clients() | |
print("------------------------------ DONE ------------------------------\n\n") | |
print("--------------------- Initializing Web Server ---------------------") | |
await server.setup() | |
await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start() | |
print("------------------------------ DONE ------------------------------\n\n") | |
print( | |
f"------------------------- Service Started -------------------------\n" | |
f"Bot =>> {bot_info.first_name}\n" | |
+ (f"DC ID =>> {bot_info.dc_id}\n" if bot_info.dc_id else "") | |
+ f"URL =>> {Server.URL}\n" | |
"------------------------------------------------------------------" | |
) | |
# Uncomment the following section to send messages to specific sources | |
""" | |
all_sources = [Telegram.ULOG_GROUP, Telegram.FLOG_CHANNEL, Telegram.PFLOG_CHANNEL] | |
for source in all_sources: | |
await FileStream.send_message(chat_id=source, text=f"Hi, I am Online @{Time_ISTKolNow()}", disable_web_page_preview=True) | |
""" | |
await idle() | |
async def cleanup(): | |
await server.cleanup() | |
async def main(): | |
try: | |
await start_services() | |
except KeyboardInterrupt: | |
print("\n------------------------ Stopped Services ------------------------") | |
except Exception as err: | |
logging.error("An error occurred:\n%s", traceback.format_exc()) | |
finally: | |
await cleanup() | |
if __name__ == "__main__": | |
asyncio.run(main()) | |