generated from tema/telegram-bot-template
Initial commit
This commit is contained in:
commit
59bab1d070
51
bot.py
Normal file
51
bot.py
Normal file
@ -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()
|
15
config.ini
Normal file
15
config.ini
Normal file
@ -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
|
||||
|
||||
|
25
config.py
Normal file
25
config.py
Normal file
@ -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']
|
6
filters/__init__.py
Normal file
6
filters/__init__.py
Normal file
@ -0,0 +1,6 @@
|
||||
from load import dp
|
||||
|
||||
#from .yourscript import FilterClass
|
||||
|
||||
#dp.filters_factory.bind(FilterClass)
|
||||
|
4
handlers/__init__.py
Normal file
4
handlers/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
from . import callback
|
||||
from . import channel
|
||||
from . import groups
|
||||
from . import private
|
0
handlers/callback/__init__.py
Normal file
0
handlers/callback/__init__.py
Normal file
0
handlers/channel/__init__.py
Normal file
0
handlers/channel/__init__.py
Normal file
0
handlers/groups/__init__.py
Normal file
0
handlers/groups/__init__.py
Normal file
0
handlers/private/__init__.py
Normal file
0
handlers/private/__init__.py
Normal file
14
load.py
Normal file
14
load.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user