fix-left-join
This commit is contained in:
parent
0abf216d87
commit
19d0984466
|
@ -1,5 +1,5 @@
|
|||
from storage import Profile, scan
|
||||
from api import get_member, send_message, get_chat_administrators
|
||||
from api import get_member, send_message
|
||||
from utils.mention import userdata_extract
|
||||
import json
|
||||
import logging
|
||||
|
@ -23,12 +23,10 @@ async def construct_unlink_buttons(actor):
|
|||
|
||||
|
||||
async def handle_command_my(msg, state):
|
||||
print("handle my command")
|
||||
logger.info("handle my command")
|
||||
from_id = str(msg["from"]["id"])
|
||||
sender = Profile.get(from_id, msg)
|
||||
|
||||
await handle_command_owner_my(msg)
|
||||
|
||||
# генерируем кнопки для всех, за кого поручились
|
||||
buttons = await construct_unlink_buttons(sender)
|
||||
reply_markup = {
|
||||
|
@ -55,24 +53,3 @@ async def handle_command_my(msg, state):
|
|||
|
||||
r = await send_message(from_id, body, reply_markup=reply_markup)
|
||||
print(r)
|
||||
|
||||
|
||||
async def handle_command_owner_my(msg):
|
||||
chat_id = msg["chat"]["id"]
|
||||
if chat_id < 0: # is not private
|
||||
r = await get_chat_administrators(chat_id)
|
||||
logger.debug(r)
|
||||
owner_id = ""
|
||||
for admin in r["result"]:
|
||||
if admin["status"] == "creator":
|
||||
owner_id = str(admin["user"]["id"])
|
||||
break
|
||||
if owner_id:
|
||||
owner = Profile.get(owner_id, msg)
|
||||
_uids, members = scan()
|
||||
for mdata in members:
|
||||
m = json.loads(mdata.decode("utf-8"))
|
||||
if owner_id in m["parents"]:
|
||||
if str(m["id"]) not in owner["children"]:
|
||||
owner["children"].append(str(m["id"]))
|
||||
Profile.save(owner)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from api import send_message, delete_message, get_chat_administrators
|
||||
from handlers.command_my import handle_command_my
|
||||
from storage import Profile, storage
|
||||
from handlers.send_button import show_request_msg
|
||||
import logging
|
||||
|
@ -6,7 +7,7 @@ logger = logging.getLogger(__name__)
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
async def handle_default(msg):
|
||||
async def handle_default(msg, state):
|
||||
logger.info(f"default handler for all messages {msg}")
|
||||
chat_id = str(msg["chat"]["id"])
|
||||
from_id = str(msg["from"]["id"])
|
||||
|
@ -20,9 +21,8 @@ async def handle_default(msg):
|
|||
r = await delete_message(chat_id, msg["message_id"])
|
||||
logger.debug(r)
|
||||
|
||||
# показать новое сообщение с кнопкой
|
||||
# для дополнительного поручения
|
||||
await show_request_msg(msg)
|
||||
# показать связи в личке
|
||||
await handle_command_my(msg, state)
|
||||
else:
|
||||
# любое другое сообщение
|
||||
if len(sender["parents"]) == 0:
|
||||
|
|
|
@ -42,8 +42,8 @@ async def handle_feedback(msg, state):
|
|||
async def handle_answer(msg):
|
||||
logger.info("handle answering feedback")
|
||||
logger.debug(msg)
|
||||
if msg.get("reply_to_message"):
|
||||
answered_msg = msg.get("reply_to_message")
|
||||
answered_msg = msg.get("reply_to_message")
|
||||
if answered_msg:
|
||||
if "from" not in answered_msg:
|
||||
answered_msg["from"] = vars(msg.from_user)
|
||||
r = await get_chat_administrators(msg["chat"]["id"])
|
||||
|
|
|
@ -31,9 +31,14 @@ async def revalidate_storage():
|
|||
logger.debug(r)
|
||||
if r["ok"]:
|
||||
_, identity, username = userdata_extract(newcomer["result"]["user"])
|
||||
# feedback report
|
||||
body = f"Участник {identity} {username} был удалён"
|
||||
r = await send_message(FEEDBACK_CHAT_ID, body)
|
||||
logger.debug(r)
|
||||
# pm report
|
||||
body = f"Вы утратили поддержку в чате и были удалены"
|
||||
r = await send_message(member_id, body)
|
||||
logger.debug(r)
|
||||
|
||||
async def handle_startup():
|
||||
await revalidate_storage()
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from handlers.handle_feedback import handle_feedback, handle_answer
|
||||
from handlers.handle_members_change import handle_join, handle_left
|
||||
from handlers.handle_default import handle_default
|
||||
from handlers.command_my import handle_command_my
|
||||
from handlers.command_graph import handle_command_graph
|
||||
|
@ -16,8 +15,8 @@ async def handle_routing(msg, state):
|
|||
if cid == uid:
|
||||
# сообщения в личке с ботом
|
||||
logger.info("private chat message")
|
||||
if "text" in msg:
|
||||
text = msg["text"]
|
||||
text = msg.get("text")
|
||||
if text:
|
||||
if text.startswith("/my"):
|
||||
await handle_command_my(msg, state)
|
||||
elif text.startswith("/graph"):
|
||||
|
@ -37,9 +36,6 @@ async def handle_routing(msg, state):
|
|||
else:
|
||||
# сообщения из всех остальных групп
|
||||
logger.info(f"group {cid} chat message")
|
||||
if "text" in msg:
|
||||
await handle_default(msg)
|
||||
elif "new_chat_member" in msg:
|
||||
await handle_join(msg)
|
||||
elif "left_chat_member" in msg:
|
||||
await handle_left(msg)
|
||||
text = msg.get("text", msg.get("caption"))
|
||||
if text:
|
||||
await handle_default(msg, state)
|
||||
|
|
|
@ -6,7 +6,7 @@ logger = logging.getLogger(__name__)
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
async def show_request_msg(msg):
|
||||
async def show_request_msg(msg, auto = False):
|
||||
logger.info("showing request with button")
|
||||
chat_id = str(msg["chat"]["id"])
|
||||
from_id = str(msg["from"]["id"])
|
||||
|
@ -22,7 +22,7 @@ async def show_request_msg(msg):
|
|||
r = await get_userphotos(user_id=from_id)
|
||||
logger.debug(r)
|
||||
if r["ok"] and r["result"]["total_count"] > 0:
|
||||
print("show button with photo")
|
||||
logger.info("showing button with photo")
|
||||
file_id = r["result"]["photos"][0][0]["file_id"]
|
||||
_uid, identity, username = userdata_extract(msg["from"])
|
||||
r = await send_photo(
|
||||
|
@ -33,7 +33,7 @@ async def show_request_msg(msg):
|
|||
reply_markup=reply_markup,
|
||||
)
|
||||
else:
|
||||
logger.info("show button without photo")
|
||||
logger.info("showing button without photo")
|
||||
r = await send_message(
|
||||
chat_id,
|
||||
newcomer_message + mention(msg["from"]),
|
||||
|
|
23
bot/main.py
23
bot/main.py
|
@ -5,17 +5,21 @@ import signal # Import the signal module
|
|||
from aiogram import Bot, Dispatcher, Router
|
||||
from aiogram.enums import ParseMode
|
||||
from aiogram.filters import CommandStart, Command
|
||||
from aiogram.types import Message, ChatJoinRequest, CallbackQuery
|
||||
from aiogram.types import Message, ChatJoinRequest, CallbackQuery, ChatMemberUpdated, ChatMemberLeft
|
||||
from config import BOT_TOKEN
|
||||
from handlers.routing import handle_routing
|
||||
from handlers.callback_unlink import handle_unlink
|
||||
from handlers.callback_vouch import handle_button
|
||||
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 state import State
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
router = Router()
|
||||
bot = Bot(BOT_TOKEN, parse_mode=ParseMode.HTML)
|
||||
dp = Dispatcher()
|
||||
state = State()
|
||||
|
||||
|
||||
|
@ -44,7 +48,6 @@ async def join_request_handler(update: ChatJoinRequest) -> None:
|
|||
join_request["chat"] = vars(update.chat)
|
||||
await handle_join_request(join_request)
|
||||
|
||||
|
||||
@router.message()
|
||||
async def all_handler(message: Message) -> None:
|
||||
msg = vars(message)
|
||||
|
@ -57,11 +60,23 @@ async def all_handler(message: Message) -> None:
|
|||
await asyncio.sleep(1.0)
|
||||
|
||||
|
||||
@router.my_chat_member()
|
||||
async def chat_members_change(update: ChatMemberUpdated):
|
||||
msg = vars(update)
|
||||
msg["chat"] = vars(update.chat)
|
||||
msg["from"] = vars(update.from_user)
|
||||
logger.debug(msg)
|
||||
if update.old_chat_member == ChatMemberLeft:
|
||||
await handle_left(msg)
|
||||
else:
|
||||
await handle_join(msg)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
bot = Bot(BOT_TOKEN, parse_mode=ParseMode.HTML)
|
||||
dp = Dispatcher()
|
||||
dp.include_router(router)
|
||||
|
||||
# storage revalidation
|
||||
await handle_startup()
|
||||
# Start event dispatching
|
||||
await dp.start_polling(bot)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user