unbloat-code
This commit is contained in:
110
main.py
110
main.py
@@ -1,21 +1,17 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import signal
|
||||
import sys
|
||||
from aiohttp import ClientSession
|
||||
from handlers.command_my import send_message
|
||||
from handlers.routing import handle_routing
|
||||
from handlers.callback_unlink import handle_unlink
|
||||
from handlers.callback_vouch import handle_button
|
||||
from handlers.commands import handle_command
|
||||
from handlers.messages_routing import messages_routing
|
||||
from handlers.handle_join_request import handle_join_request
|
||||
from handlers.handle_startup import handle_startup
|
||||
from handlers.handle_members_change import handle_join, handle_left
|
||||
from bot.config import BOT_TOKEN, FEEDBACK_CHAT_ID
|
||||
from bot.state import State
|
||||
from storage import Profile
|
||||
from bot.api import send_message
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
state = State()
|
||||
state = dict()
|
||||
|
||||
api_url = f'https://api.telegram.org/bot{BOT_TOKEN}/'
|
||||
|
||||
@@ -25,87 +21,33 @@ async def fetch(session, url):
|
||||
return await response.json()
|
||||
|
||||
|
||||
async def command_start_handler(message):
|
||||
caption = "Напишите своё сообщение для нас" if message.from_user.llanguage_code == 'ru' else "Write your message for us"
|
||||
await message.answer(caption)
|
||||
|
||||
|
||||
async def process_callback(cbq):
|
||||
try:
|
||||
data = cbq.get("data")
|
||||
if data.startswith("vouch"):
|
||||
await handle_button(cbq)
|
||||
elif data.startswith("unlink"):
|
||||
await handle_unlink(cbq, state)
|
||||
except Exception as e:
|
||||
logger.error(f"[main.process_callback] ERROR {e}")
|
||||
logger.debug(cbq)
|
||||
import traceback
|
||||
text = traceback.format_exc()
|
||||
await send_message(FEEDBACK_CHAT_ID, text)
|
||||
|
||||
|
||||
async def join_request_handler(join_request):
|
||||
try:
|
||||
await handle_join_request(join_request)
|
||||
except Exception as e:
|
||||
logger.error(f"[main.join_request_handler] ERROR {e}")
|
||||
logger.debug(join_request)
|
||||
import traceback
|
||||
text = traceback.format_exc()
|
||||
await send_message(FEEDBACK_CHAT_ID, text)
|
||||
|
||||
|
||||
async def all_handler(message):
|
||||
try:
|
||||
await handle_routing(message, state)
|
||||
except Exception as e:
|
||||
logger.error(f"[main.all_handler] ERROR {e}")
|
||||
logger.debug(message)
|
||||
import traceback
|
||||
text = traceback.format_exc()
|
||||
await send_message(FEEDBACK_CHAT_ID, text)
|
||||
|
||||
|
||||
async def chat_members_change(update):
|
||||
try:
|
||||
old = update.get("old_chat_member")
|
||||
if old:
|
||||
if old.get("status") == 'KICKED':
|
||||
Profile.erase(update["from"]["id"])
|
||||
await handle_left(update)
|
||||
elif update.get("new_chat_member"):
|
||||
await handle_join(update)
|
||||
else:
|
||||
logger.info("unhandled members update")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[main.my_chat_member] ERROR {e}")
|
||||
logger.debug(update)
|
||||
import traceback
|
||||
text = traceback.format_exc()
|
||||
await send_message(FEEDBACK_CHAT_ID, text)
|
||||
|
||||
|
||||
async def main():
|
||||
# storage revalidation
|
||||
await handle_startup()
|
||||
|
||||
async with ClientSession() as session:
|
||||
offset = 0 # начальное значение offset
|
||||
while True:
|
||||
updates = await fetch(session, f"{api_url}getUpdates?offset={offset}")
|
||||
for update in updates["result"]:
|
||||
if "message" in update:
|
||||
await all_handler(update["message"])
|
||||
elif "callback_query" in update:
|
||||
await process_callback(update["callback_query"])
|
||||
elif "join_chat_request" in update:
|
||||
await join_request_handler(update["join_chat_request"])
|
||||
elif "my_chat_member" in update:
|
||||
await chat_members_change(update["my_chat_member"])
|
||||
for update in updates.get("result", []):
|
||||
try:
|
||||
message = update.get("message")
|
||||
join_chat_request = update.get("join_chat_request")
|
||||
if message:
|
||||
text = message.get("text")
|
||||
if text.startswith('/'):
|
||||
await handle_command(message, state)
|
||||
else:
|
||||
await messages_routing(message, state)
|
||||
|
||||
offset = update["update_id"] + 1 # обновление offset
|
||||
elif join_chat_request:
|
||||
await handle_join_request(join_chat_request)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
logger.debug(update)
|
||||
import traceback
|
||||
text = traceback.format_exc()
|
||||
await send_message(FEEDBACK_CHAT_ID, text)
|
||||
|
||||
offset = update["update_id"] + 1
|
||||
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
|
Reference in New Issue
Block a user