From 59bab1d0708d54c2b9b617b0dfb90beffe410509 Mon Sep 17 00:00:00 2001 From: tema Date: Mon, 31 Jul 2023 14:26:50 +0000 Subject: [PATCH] Initial commit --- bot.py | 51 +++++++++++++++++++++++++++++++++++ config.ini | 15 +++++++++++ config.py | 25 +++++++++++++++++ filters/__init__.py | 6 +++++ handlers/__init__.py | 4 +++ handlers/callback/__init__.py | 0 handlers/channel/__init__.py | 0 handlers/groups/__init__.py | 0 handlers/private/__init__.py | 0 load.py | 14 ++++++++++ 10 files changed, 115 insertions(+) create mode 100644 bot.py create mode 100644 config.ini create mode 100644 config.py create mode 100644 filters/__init__.py create mode 100644 handlers/__init__.py create mode 100644 handlers/callback/__init__.py create mode 100644 handlers/channel/__init__.py create mode 100644 handlers/groups/__init__.py create mode 100644 handlers/private/__init__.py create mode 100644 load.py diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..02cd383 --- /dev/null +++ b/bot.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +import logging + +from aiogram import executor + +from load import dp +from config import parse_bool, config +import filters +import handlers + + +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO +) + +logger = logging.getLogger(__name__) + +async def on_startup(dp): + webhook_url = "{host}{path}{token}".format( + host=config["WebHook"]["webhook_host"], + path=config["WebHook"]["webhook_path"], + token=config["Bot"]["token"] + ) + await dp.bot.set_webhook(url=webhook_url) + + +async def on_shutdown(dp): + await dp.bot.delete_webhook() + + +def main(): + skip_updates = parse_bool(text=config["Bot"]["skip_updates"]) + if parse_bool(text=config["WebHook"]["use"]): + # https://github.com/aiogram/aiogram/pull/795/commits/a7e0ce2971a0a320849c8b11080fa6b737b6935b + # Needed for async tasks :3 + # Only for webhooks! For polling not needed patching file + executor.start_webhook( + dispatcher=dp, + skip_updates=skip_updates, + webhook_path="{}{}".format(config["WebHook"]["webhook_path"], config["Bot"]["token"]), + on_startup=on_startup, + on_shutdown=on_shutdown, + host=config["WebHook"]["app_host"], + port=int(config["WebHook"]["app_port"]) + ) + else: + executor.start_polling(dp, skip_updates=skip_updates) + + +if __name__ == '__main__': + main() diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..d35011a --- /dev/null +++ b/config.ini @@ -0,0 +1,15 @@ +[Bot] +token = 123:abc +base_server = http://127.0.0.1:18888 +skip_updates = true + + +[WebHook] +use = true +webhook_host = http://127.0.0.1:3000 +webhook_path = /bot + +app_host = 127.0.0.1 +app_port = 3000 + + diff --git a/config.py b/config.py new file mode 100644 index 0000000..22e9564 --- /dev/null +++ b/config.py @@ -0,0 +1,25 @@ +from configparser import ConfigParser + +from easydict import EasyDict as edict + + +CONFIG_FILE = 'config.ini' + +data = ConfigParser() +data.read(CONFIG_FILE) + +config = edict() + +for section in data.sections(): + config[section] = edict() + + for key, value in data.items(section): + config[section][key] = value + + +def parse_bool(section=None, key=None, text=None): + if text is not None: + return text.lower() in ['yes', 'true'] + + if section is not None and key is not None: + return config[section][key].lower() in ['yes', 'true'] diff --git a/filters/__init__.py b/filters/__init__.py new file mode 100644 index 0000000..6c663ae --- /dev/null +++ b/filters/__init__.py @@ -0,0 +1,6 @@ +from load import dp + +#from .yourscript import FilterClass + +#dp.filters_factory.bind(FilterClass) + diff --git a/handlers/__init__.py b/handlers/__init__.py new file mode 100644 index 0000000..c3d850f --- /dev/null +++ b/handlers/__init__.py @@ -0,0 +1,4 @@ +from . import callback +from . import channel +from . import groups +from . import private diff --git a/handlers/callback/__init__.py b/handlers/callback/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/handlers/channel/__init__.py b/handlers/channel/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/handlers/groups/__init__.py b/handlers/groups/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/handlers/private/__init__.py b/handlers/private/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/load.py b/load.py new file mode 100644 index 0000000..65021e6 --- /dev/null +++ b/load.py @@ -0,0 +1,14 @@ +from aiogram import Dispatcher, Bot +from aiogram.bot.api import TelegramAPIServer +from aiogram.contrib.fsm_storage.memory import MemoryStorage + +from config import config + + +bot = Bot( + token=config["Bot"]["token"], + server=TelegramAPIServer.from_base(config["Bot"]["base_server"]) +) + +storage = MemoryStorage() +dp = Dispatcher(bot=bot, storage=storage)