Skip to content

Commit 4ce5cd3

Browse files
committed
first commit
1 parent 4c8b9d3 commit 4ce5cd3

20 files changed

+133
-0
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOT_TOKEN=6429367674:AAGvUJwy_I1PXp6MEDHgpytqkj8huoRvEgU
2+
ADMINS=6207237828
3+
ip=localhost

app.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import middlewares, filters, handlers
2+
import asyncio
3+
import logging
4+
import sys
5+
from utils.notify_admins import on_startup_notify
6+
from utils.set_bot_commands import set_default_commands
7+
from loader import dp, bot
8+
from middlewares import ThrottlingMiddleware
9+
10+
11+
async def main():
12+
await on_startup_notify()
13+
await set_default_commands()
14+
dp.update.middleware.register(ThrottlingMiddleware())
15+
try:
16+
await bot.delete_webhook(drop_pending_updates=True)
17+
await dp.start_polling(bot)
18+
finally:
19+
await bot.session.close()
20+
21+
22+
if __name__ == "__main__":
23+
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
24+
asyncio.run(main())

data/__init__.py

Whitespace-only changes.

data/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from environs import Env
2+
3+
# environs kutubxonasidan foydalanish
4+
env = Env()
5+
env.read_env()
6+
7+
# .env fayl ichidan quyidagilarni o'qiymiz
8+
BOT_TOKEN = env.str("BOT_TOKEN") # Bot toekn
9+
ADMINS = env.list("ADMINS") # adminlar ro'yxati
10+
IP = env.str("ip") # Xosting ip manzili

filters/__init__.py

Whitespace-only changes.

handlers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import users

handlers/users/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import help
2+
from . import start
3+
from . import echo

handlers/users/help.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from aiogram import types
2+
from aiogram.filters import Command
3+
4+
from loader import dp
5+
6+
7+
@dp.message(Command(commands='help'))
8+
async def help_handler(message: types.Message):
9+
text = ("Buyruqlar: ",
10+
"/start - Botni ishga tushirish",
11+
"/help - Yordam")
12+
await message.answer("\n".join(text))

handlers/users/start.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from aiogram import types
2+
from aiogram.filters import Command
3+
from loader import dp
4+
5+
6+
@dp.message(Command(commands="start"))
7+
async def start_handler(message: types.Message):
8+
await message.answer(f"Salom {message.from_user.full_name}")

keyboards/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import default
2+
from . import inline

keyboards/default/__init__.py

Whitespace-only changes.

keyboards/inline/__init__.py

Whitespace-only changes.

loader.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from aiogram import Bot, Dispatcher
2+
from aiogram.enums import ParseMode
3+
from aiogram.fsm.storage.memory import MemoryStorage
4+
from data import config
5+
6+
bot = Bot(config.BOT_TOKEN, parse_mode=ParseMode.HTML)
7+
stroge = MemoryStorage()
8+
dp = Dispatcher(storage=stroge)

middlewares/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .throttling import ThrottlingMiddleware

middlewares/throttling.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import time
2+
3+
from aiogram import BaseMiddleware, types
4+
from aiogram.dispatcher.event.handler import HandlerObject
5+
6+
7+
class ThrottlingMiddleware(BaseMiddleware):
8+
def __init__(self, default_rate: int = 0.5) -> None:
9+
self.limiters = {}
10+
11+
self.default_rate = default_rate
12+
self.count_throttled = 1
13+
self.last_throttled = 0
14+
15+
async def __call__(self, handler, event: types.Update, data):
16+
real_handler: HandlerObject = data["handler"]
17+
skip_pass = True
18+
19+
if real_handler.flags.get("skip_pass") is not None:
20+
skip_pass = real_handler.flags.get("skip_pass")
21+
22+
if skip_pass:
23+
if int(time.time()) - self.last_throttled >= self.default_rate:
24+
self.last_throttled = int(time.time())
25+
self.default_rate = 0.5
26+
self.count_throttled = 0
27+
return await handler(event, data)
28+
else:
29+
if self.count_throttled >= 2:
30+
self.default_rate = 3
31+
else:
32+
self.count_throttled += 1
33+
await event.message.reply("<b>So'rov ko'payib ketdi!</b>")
34+
35+
self.last_throttled = int(time.time())
36+
else:
37+
return await handler(event, data)

states/__init__.py

Whitespace-only changes.

utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import db_api
2+
from .notify_admins import on_startup_notify

utils/db_api/__init__.py

Whitespace-only changes.

utils/notify_admins.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from data.config import ADMINS
2+
from aiogram import Dispatcher
3+
from loader import bot
4+
import logging
5+
6+
async def on_startup_notify():
7+
for admin in ADMINS:
8+
try:
9+
await bot.send_message(chat_id=admin, text="Bot ishga tushdi!")
10+
except Exception as Err:
11+
logging.exception(Err)

utils/set_bot_commands.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from aiogram import types
2+
from loader import bot
3+
4+
async def set_default_commands():
5+
await bot.set_my_commands(
6+
[
7+
types.BotCommand(command='start', description='Botni ishga tushurish'),
8+
types.BotCommand(command="help", description="Yordam"),
9+
types.BotCommand(command="admin", description="Faqat adminlar uchun"),
10+
]
11+
)

0 commit comments

Comments
 (0)