init
This commit is contained in:
33
bot/__init__.py
Normal file
33
bot/__init__.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import contextlib
|
||||
|
||||
from rich import print
|
||||
|
||||
|
||||
async def runner():
|
||||
from . import handlers
|
||||
from .common import bot, dp
|
||||
from .modules.error import on_error
|
||||
|
||||
dp.error.register(on_error)
|
||||
dp.include_routers(handlers.router)
|
||||
|
||||
await bot.delete_webhook(drop_pending_updates=True)
|
||||
await dp.start_polling(bot)
|
||||
|
||||
|
||||
def plugins():
|
||||
from rich import traceback
|
||||
|
||||
traceback.install(show_locals=True)
|
||||
|
||||
|
||||
def main():
|
||||
import asyncio
|
||||
|
||||
plugins()
|
||||
|
||||
print("Starting...")
|
||||
with contextlib.suppress(KeyboardInterrupt):
|
||||
asyncio.run(runner())
|
||||
|
||||
print("[red]Stopped.[/]")
|
||||
4
bot/__main__.py
Normal file
4
bot/__main__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from . import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
11
bot/common.py
Normal file
11
bot/common.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from aiogram import Bot, Dispatcher
|
||||
from rich.console import Console
|
||||
|
||||
from .utils.config import config
|
||||
|
||||
bot = Bot(token=config.telegram.bot_token)
|
||||
dp = Dispatcher()
|
||||
console = Console()
|
||||
|
||||
|
||||
__all__ = ["bot", "dp", "config", "console"]
|
||||
3
bot/filters/__init__.py
Normal file
3
bot/filters/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .filter1686 import Filter1686, FilterBusiness1686
|
||||
|
||||
__all__ = ["Filter1686", "FilterBusiness1686"]
|
||||
23
bot/filters/filter1686.py
Normal file
23
bot/filters/filter1686.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from aiogram.filters import BaseFilter
|
||||
from aiogram.types import Message, BusinessConnection
|
||||
import re
|
||||
|
||||
|
||||
class Filter1686(BaseFilter):
|
||||
async def __call__(self, message: Message):
|
||||
if not message.text:
|
||||
return False
|
||||
|
||||
return re.match(r".*16.*8.*6.*", message.text) or re.match(
|
||||
r".*шест?над?цать.*восемь.*шест?ь.*десят[ыі]х", message.text, flags=re.IGNORECASE
|
||||
)
|
||||
|
||||
|
||||
class FilterBusiness1686(BaseFilter):
|
||||
async def __call__(self, business_message: Message):
|
||||
if not business_message.text:
|
||||
return False
|
||||
|
||||
return re.match(r".*16.*8.*6.*", business_message.text) or re.match(
|
||||
r".*шест?над?цать.*восемь.*шест?ь.*десят[ыі]х", business_message.text, flags=re.IGNORECASE
|
||||
)
|
||||
11
bot/handlers/__init__.py
Normal file
11
bot/handlers/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from aiogram import Router
|
||||
|
||||
from bot.middlewares import LikeMiddleware
|
||||
|
||||
from . import initialize, on_1686, start, inline
|
||||
|
||||
router = Router()
|
||||
|
||||
router.message.middleware(LikeMiddleware())
|
||||
|
||||
router.include_routers(initialize.router, start.router, on_1686.router, inline.router)
|
||||
3
bot/handlers/initialize/__init__.py
Normal file
3
bot/handlers/initialize/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .initializer import router
|
||||
|
||||
__all__ = ["router"]
|
||||
9
bot/handlers/initialize/initializer.py
Normal file
9
bot/handlers/initialize/initializer.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from aiogram import Bot, Router
|
||||
from rich import print
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.startup()
|
||||
async def startup(bot: Bot):
|
||||
print(f"[green]Started as[/] @{(await bot.me()).username}")
|
||||
4
bot/handlers/inline/__init__.py
Normal file
4
bot/handlers/inline/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .on_inline import router
|
||||
|
||||
|
||||
__all__ = ["router"]
|
||||
20
bot/handlers/inline/on_inline.py
Normal file
20
bot/handlers/inline/on_inline.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from aiogram import Router
|
||||
|
||||
from aiogram.types import InlineQuery
|
||||
from aiogram.types import InlineQueryResultCachedVoice
|
||||
|
||||
from ...utils.config import config
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.inline_query()
|
||||
async def default_inline_query(inline_query: InlineQuery):
|
||||
await inline_query.answer(
|
||||
[
|
||||
InlineQueryResultCachedVoice(
|
||||
id=id_[-1:-8:-1], voice_file_id=id_, title=config.send[id_]
|
||||
)
|
||||
for id_ in config.send.keys()
|
||||
]
|
||||
)
|
||||
3
bot/handlers/on_1686/__init__.py
Normal file
3
bot/handlers/on_1686/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .h1686 import router
|
||||
|
||||
__all__ = ["router"]
|
||||
16
bot/handlers/on_1686/h1686.py
Normal file
16
bot/handlers/on_1686/h1686.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from aiogram import Router, types
|
||||
|
||||
from ...filters import Filter1686, FilterBusiness1686
|
||||
from ...utils.config import config
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.message(Filter1686())
|
||||
async def on_message(message: types.Message):
|
||||
await message.reply_voice(list(config.send.keys())[0])
|
||||
|
||||
|
||||
@router.business_message(FilterBusiness1686())
|
||||
async def on_business_message(business_message: types.Message):
|
||||
await business_message.reply_voice(list(config.send.keys())[0])
|
||||
3
bot/handlers/start/__init__.py
Normal file
3
bot/handlers/start/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .start import router
|
||||
|
||||
__all__ = ["router"]
|
||||
9
bot/handlers/start/start.py
Normal file
9
bot/handlers/start/start.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from aiogram import Router, types
|
||||
from aiogram.filters import Command
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.message(Command("start"))
|
||||
async def on_start(message: types.Message):
|
||||
await message.reply(text="1686")
|
||||
3
bot/middlewares/__init__.py
Normal file
3
bot/middlewares/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .like_middleware import LikeMiddleware
|
||||
|
||||
__all__ = ["LikeMiddleware"]
|
||||
16
bot/middlewares/like_middleware.py
Normal file
16
bot/middlewares/like_middleware.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from typing import Any, Awaitable, Callable, Dict
|
||||
|
||||
from aiogram import BaseMiddleware
|
||||
from aiogram.types import Message, ReactionTypeEmoji
|
||||
|
||||
|
||||
class LikeMiddleware(BaseMiddleware):
|
||||
async def __call__(
|
||||
self,
|
||||
handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]],
|
||||
event: Message,
|
||||
data: Dict[str, Any],
|
||||
) -> Any:
|
||||
res = await handler(event, data)
|
||||
await event.react(reaction=[ReactionTypeEmoji(emoji="❤")])
|
||||
return res
|
||||
1
bot/modules/__init__.py
Normal file
1
bot/modules/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
3
bot/modules/error/__init__.py
Normal file
3
bot/modules/error/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .handler import on_error
|
||||
|
||||
__all__ = ["on_error"]
|
||||
41
bot/modules/error/handler.py
Normal file
41
bot/modules/error/handler.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from aiogram import Bot
|
||||
from aiogram.dispatcher import router as s_router
|
||||
from aiogram.types.error_event import ErrorEvent
|
||||
from rich.traceback import Traceback
|
||||
|
||||
from bot.common import console
|
||||
|
||||
|
||||
async def on_error(event: ErrorEvent, bot: Bot):
|
||||
import base64
|
||||
import os
|
||||
|
||||
error_id = base64.urlsafe_b64encode(os.urandom(6)).decode()
|
||||
|
||||
traceback = Traceback.from_exception(
|
||||
type(event.exception),
|
||||
event.exception,
|
||||
event.exception.__traceback__,
|
||||
show_locals=True,
|
||||
suppress=[s_router],
|
||||
)
|
||||
|
||||
if event.update.chosen_inline_result:
|
||||
await bot.edit_message_caption(
|
||||
inline_message_id=event.update.chosen_inline_result.inline_message_id,
|
||||
caption=f"💔 <b>ERROR</b> occurred. Use this code to search in logs: "
|
||||
f"<code>{error_id}</code>",
|
||||
parse_mode="HTML",
|
||||
)
|
||||
|
||||
if event.update.message:
|
||||
await event.update.message.reply(
|
||||
text=f"💔 <b>ERROR</b> occurred. Use this code to search in logs: "
|
||||
f"<code>{error_id}</code>",
|
||||
parse_mode="HTML",
|
||||
)
|
||||
|
||||
console.print(f"[red]{error_id} occurred[/]")
|
||||
console.print(event)
|
||||
console.print(traceback)
|
||||
console.print(f"-{error_id} occurred-")
|
||||
1
bot/utils/__init__.py
Normal file
1
bot/utils/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
3
bot/utils/config/__init__.py
Normal file
3
bot/utils/config/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from ._config import Config
|
||||
|
||||
config = Config()
|
||||
19
bot/utils/config/_config.py
Normal file
19
bot/utils/config/_config.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import tomllib
|
||||
|
||||
|
||||
class Config(dict):
|
||||
def __init__(self, _config: dict = None):
|
||||
try:
|
||||
if _config is None:
|
||||
super().__init__(**tomllib.load(open("config.toml", "rb")))
|
||||
else:
|
||||
super().__init__(**_config)
|
||||
|
||||
except FileNotFoundError:
|
||||
super().__init__()
|
||||
|
||||
def __getattr__(self, item):
|
||||
if not isinstance(self.get(item), dict):
|
||||
return self.get(item)
|
||||
else:
|
||||
return self.__class__(_config=self.get(item))
|
||||
Reference in New Issue
Block a user