welcomecenterbot/bot/handlers/command_my.py
2023-09-18 14:02:24 +03:00

56 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from storage import Profile, scan
from api import get_member, send_message
from utils.mention import userdata_extract
import json
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
async def construct_unlink_buttons(actor):
print(f"constructing unlink buttons for {actor['children']}")
buttons = []
for vouch in actor["children"]:
for chat_id in actor["chats"]:
r = await get_member(chat_id, vouch)
logger.debug(r)
if "result" in r:
member = r["result"]["user"]
_uid, identity, username = userdata_extract(member)
buttons.append(
{"text": f"{identity} {username}", "callback_data": "unlink" + vouch}
)
return buttons
async def handle_command_my(msg, state):
logger.info("handle my command")
from_id = str(msg["from"]["id"])
sender = Profile.get(from_id, msg)
# генерируем кнопки для всех, за кого поручились
buttons = await construct_unlink_buttons(sender)
reply_markup = {
"inline_keyboard": [
buttons,
]
}
if len(buttons) == 0:
if msg["from"].get("language_code", "ru") == "ru":
body = "Вас ещё никто не узнал? Напишите, я передам нашему кругу"
else:
body = (
"Nobody recognized you? Speak, I will pass your message to the circle"
)
r = await send_message(from_id, body)
logger.debug(r)
chat_id = msg["chat"]["id"]
state.make_talking(from_id, chat_id)
else:
if msg["from"].get("language_code", "ru") == "ru":
body = "Нажмите кнопки ниже, чтобы удалить ваши связи"
else:
body = "Unlink your connections pressing the buttons below"
r = await send_message(from_id, body, reply_markup=reply_markup)
print(r)