welcomecenterbot/bot/handlers/command_my.py

56 lines
2.0 KiB
Python
Raw Normal View History

2023-09-11 20:21:55 +00:00
from storage import Profile, scan
2023-09-18 11:02:24 +00:00
from api import get_member, send_message
2023-09-11 20:21:55 +00:00
from utils.mention import userdata_extract
2023-09-18 07:02:15 +00:00
import json
2023-09-18 07:50:48 +00:00
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
2023-04-23 16:54:58 +00:00
2023-09-11 20:04:53 +00:00
async def construct_unlink_buttons(actor):
2023-09-18 07:16:33 +00:00
print(f"constructing unlink buttons for {actor['children']}")
2023-04-23 16:54:58 +00:00
buttons = []
2023-09-11 20:04:53 +00:00
for vouch in actor["children"]:
for chat_id in actor["chats"]:
r = await get_member(chat_id, vouch)
2023-09-18 07:50:48 +00:00
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}
)
2023-09-11 17:02:29 +00:00
return buttons
2023-04-23 16:54:58 +00:00
2023-04-28 12:24:14 +00:00
2023-09-11 20:04:53 +00:00
async def handle_command_my(msg, state):
2023-09-18 11:02:24 +00:00
logger.info("handle my command")
2023-09-11 20:04:53 +00:00
from_id = str(msg["from"]["id"])
2023-04-23 16:54:58 +00:00
sender = Profile.get(from_id, msg)
2023-05-01 16:31:17 +00:00
2023-04-23 16:54:58 +00:00
# генерируем кнопки для всех, за кого поручились
2023-09-11 20:04:53 +00:00
buttons = await construct_unlink_buttons(sender)
reply_markup = {
"inline_keyboard": [
buttons,
]
}
2023-09-11 17:02:29 +00:00
if len(buttons) == 0:
2023-09-11 20:04:53 +00:00
if msg["from"].get("language_code", "ru") == "ru":
body = "Вас ещё никто не узнал? Напишите, я передам нашему кругу"
2023-09-11 17:02:29 +00:00
else:
2023-09-11 20:04:53 +00:00
body = (
"Nobody recognized you? Speak, I will pass your message to the circle"
)
r = await send_message(from_id, body)
2023-09-18 07:50:48 +00:00
logger.debug(r)
2023-09-11 20:04:53 +00:00
chat_id = msg["chat"]["id"]
2023-09-11 17:02:29 +00:00
state.make_talking(from_id, chat_id)
2023-04-23 16:54:58 +00:00
else:
2023-09-11 20:04:53 +00:00
if msg["from"].get("language_code", "ru") == "ru":
body = "Нажмите кнопки ниже, чтобы удалить ваши связи"
2023-09-11 17:02:29 +00:00
else:
2023-09-11 20:04:53 +00:00
body = "Unlink your connections pressing the buttons below"
2023-04-23 16:54:58 +00:00
2023-09-11 20:04:53 +00:00
r = await send_message(from_id, body, reply_markup=reply_markup)
2023-05-01 16:31:17 +00:00
print(r)