-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
67 lines (49 loc) · 1.89 KB
/
bot.py
File metadata and controls
67 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import asyncio
import logging
from aiogram import Bot, Dispatcher
from aiogram.fsm.storage.redis import RedisStorage, Redis
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from config_data.config import load_config
from handlers import user_handlers
from keyboards.set_menu import set_main_menu
from config_data.config import POSTGRESQL_HOST, POSTGRESQL_USER, POSTGRESQL_PASSWORD, POSTGRESQL_DBNAME, \
REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, REDIS_USERNAME
DATABASE_URL = f"postgresql://{POSTGRESQL_USER}:{POSTGRESQL_PASSWORD}@{POSTGRESQL_HOST}/{POSTGRESQL_DBNAME}"
# define SQLAlchemy model for storing data
Base = declarative_base()
# initialize the logger
logger = logging.getLogger(__name__)
# create SQLAlchemy engine and tables
engine = create_engine(DATABASE_URL, echo=True)
Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()
# set up Redis storage for FSMContext
redis = Redis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD, username=REDIS_USERNAME)
storage = RedisStorage(redis=redis)
# configure and launch the bot
async def main():
# configure logging
logging.basicConfig(
level=logging.INFO,
format='%(filename)s:%(lineno)d #%(levelname)-8s '
'[%(asctime)s] - %(name)s - %(message)s'
)
# start bot info
logger.info("Starting bot")
config = load_config()
# initialize bot and dispatcher
bot = Bot(token=config.tg_bot.token,
parse_mode="HTML")
dp = Dispatcher(storage=storage)
# register routers in dispatcher
dp.include_router(user_handlers.router)
# configure Menu button
await set_main_menu(bot)
# launch polling
await bot.delete_webhook(drop_pending_updates=True)
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())