welcomecenterbot/bot/handlers/command_my.py

66 lines
2.3 KiB
Python
Raw Normal View History

2023-09-11 15:52:35 +00:00
from bot.storage import Profile, scan
from bot.api import get_member, send_message, get_chat_administrators
from bot.utils.mention import userdata_extract
2023-04-28 12:24:14 +00:00
2023-04-23 16:54:58 +00:00
def construct_unlink_buttons(actor):
buttons = []
for vouch in actor['children']:
for chat_id in actor['chats']:
2023-04-28 12:24:14 +00:00
r = get_member(chat_id, vouch)
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 17:02:29 +00:00
def handle_command_my(msg, state):
print('handle my command')
2023-04-23 16:54:58 +00:00
from_id = str(msg['from']['id'])
sender = Profile.get(from_id, msg)
2023-05-01 16:31:17 +00:00
handle_command_owner_my(msg)
2023-04-23 16:54:58 +00:00
# генерируем кнопки для всех, за кого поручились
2023-09-11 17:02:29 +00:00
buttons = 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 = send_message(from_id, body)
print(r)
chat_id = msg['chat']['id']
state.make_talking(from_id, chat_id)
2023-04-23 16:54:58 +00:00
else:
2023-09-11 17:02:29 +00:00
if msg['from'].get('language_code', 'ru') == 'ru':
body = 'Нажмите кнопки ниже, чтобы удалить ваши связи'
else:
body = 'Unlink your connections pressing the buttons below'
2023-04-23 16:54:58 +00:00
r = send_message(from_id, body, reply_markup=reply_markup)
2023-05-01 16:31:17 +00:00
print(r)
def handle_command_owner_my(msg):
chat_id = msg['chat']['id']
r = get_chat_administrators(chat_id)
print(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)
2023-09-11 17:02:29 +00:00
_uids, members = scan()
2023-05-01 16:31:17 +00:00
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)