azils3 commited on
Commit
cef4c33
·
verified ·
1 Parent(s): d6829f5

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +27 -84
bot.py CHANGED
@@ -1,93 +1,36 @@
1
- import os, math, logging, datetime, pytz, logging.config
2
- from aiohttp import web
3
- from pyrogram import Client, types
4
- from database.users_chats_db import db
5
- from database.ia_filterdb import Media
6
- from typing import Union, Optional, AsyncGenerator
7
- from utils import temp, __repo__, __license__, __copyright__, __version__
8
- from info import API_ID, API_HASH, BOT_TOKEN, LOG_CHANNEL, UPTIME, WEB_SUPPORT, LOG_MSG
9
- import asyncio
10
- import uvicorn
11
- from fastapi_app import app as fastapi_app
12
 
13
- # Get logging configurations
14
- logging.config.fileConfig("logging.conf")
15
- logger = logging.getLogger(__name__)
 
16
 
17
- class Bot(Client):
18
- def __init__(self):
19
- super().__init__(
20
- name="Professor-Bot",
21
- api_id=API_ID,
22
- api_hash=API_HASH,
23
- bot_token=BOT_TOKEN,
24
- plugins=dict(root="plugins")
25
- )
26
- logger.info("Bot initialized.")
27
 
28
- async def start(self):
29
- logger.info("Starting bot...")
30
- b_users, b_chats = await db.get_banned()
31
- temp.BANNED_USERS = b_users
32
- temp.BANNED_CHATS = b_chats
33
- logger.info("Banned users and chats loaded.")
34
 
35
- await super().start()
36
- logger.info("Pyrogram client started.")
 
37
 
38
- await Media.ensure_indexes()
39
- logger.info("Indexes ensured for Media collection.")
40
 
41
- me = await self.get_me()
42
- temp.U_NAME = me.username
43
- temp.B_NAME = me.first_name
44
- self.id = me.id
45
- self.name = me.first_name
46
- self.mention = me.mention
47
- self.username = me.username
48
- self.log_channel = LOG_CHANNEL
49
- self.uptime = UPTIME
50
- curr = datetime.datetime.now(pytz.timezone("Asia/Kolkata"))
51
- date = curr.strftime('%d %B, %Y')
52
- tame = curr.strftime('%I:%M:%S %p')
53
- log_message = LOG_MSG.format(me.first_name, date, tame, __repo__, __version__, __license__, __copyright__)
54
- logger.info(log_message)
55
 
56
- try:
57
- await self.send_message(LOG_CHANNEL, text=log_message, disable_web_page_preview=True)
58
- logger.info("Log message sent to LOG_CHANNEL.")
59
- except Exception as e:
60
- logger.warning(f"Bot Isn't Able To Send Message To LOG_CHANNEL \n{e}")
61
 
62
- if bool(WEB_SUPPORT) is True:
63
- app = web.AppRunner(web.Application(client_max_size=30000000))
64
- await app.setup()
65
- await web.TCPSite(app, "0.0.0.0", 8080).start()
66
- logger.info("Web Response Is Running......🕸️")
67
 
68
- # Run FastAPI app in a separate task
69
- asyncio.create_task(uvicorn.run(fastapi_app, host="0.0.0.0", port=8000, log_level="info"))
70
-
71
- async def stop(self, *args):
72
- logger.info("Stopping bot...")
73
- await super().stop()
74
- logger.info(f"Bot Is Restarting ⟳...")
75
-
76
- async def iter_messages(self, chat_id: Union[int, str], limit: int, offset: int = 0) -> Optional[AsyncGenerator["types.Message", None]]:
77
- logger.info(f"Iterating messages in chat_id: {chat_id}, limit: {limit}, offset: {offset}")
78
- current = offset
79
- while True:
80
- new_diff = min(200, limit - current)
81
- if new_diff <= 0:
82
- logger.info("No more messages to iterate.")
83
- return
84
- messages = await self.get_messages(chat_id, list(range(current, current+new_diff+1)))
85
- logger.info(f"Retrieved {len(messages)} messages.")
86
- for message in messages:
87
- yield message
88
- current += 1
89
- logger.info(f"Yielding message with ID: {message.id}")
90
-
91
- if __name__ == "__main__":
92
- bot = Bot()
93
- asyncio.run(bot.start())
 
1
+ # Dockerfile
2
+ FROM python:3.10
 
 
 
 
 
 
 
 
 
3
 
4
+ # Create a non-root user with valid username format
5
+ RUN adduser --disabled-password --gecos '' professor && \
6
+ mkdir -p /app && \
7
+ chown -R professor:professor /app
8
 
9
+ # Install system dependencies
10
+ RUN apt-get update && apt-get upgrade -y
 
 
 
 
 
 
 
 
11
 
12
+ # Copy requirements and install
13
+ COPY requirements.txt /requirements.txt
14
+ RUN pip install --upgrade pip && \
15
+ pip install -r /requirements.txt
 
 
16
 
17
+ # Switch to non-root user
18
+ USER professor
19
+ WORKDIR /app
20
 
21
+ # Copy application files
22
+ COPY --chown=professor:professor . .
23
 
24
+ # Set environment variables
25
+ ENV LOG_PATH=/app/BotLog.txt
26
+ ENV UVICORN_HOST=0.0.0.0
27
+ ENV UVICORN_PORT=8000
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # Ensure the log file is writable
30
+ RUN touch $LOG_PATH && chmod 666 $LOG_PATH
 
 
 
31
 
32
+ # Expose port for FastAPI
33
+ EXPOSE 8000
 
 
 
34
 
35
+ # Start the FastAPI app
36
+ CMD ["uvicorn", "fastapi_app:app", "--host", "$UVICORN_HOST", "--port", "$UVICORN_PORT", "--log-level", "info"]