This commit is contained in:
2023-09-11 18:52:35 +03:00
parent 6b30c6dc0c
commit 2929c5f58f
19 changed files with 53 additions and 54 deletions

187
bot/api.py Normal file
View File

@@ -0,0 +1,187 @@
import requests
import aiohttp
import json
import os
from bot.config import BOT_TOKEN, WEBHOOK
apiBase = f"https://api.telegram.org/bot{TOKEN}/"
def register_webhook():
r = requests.get(apiBase + f'setWebhook?url={WEBHOOK}')
return r.json()
def delete_message(cid: str, mid: str):
url = apiBase + f"deleteMessage?chat_id={cid}&message_id={mid}"
r = requests.post(url)
return r.json()
# https://core.telegram.org/bots/api#sendmessage
def send_message(cid: str, body, reply_to=None, reply_markup=None):
url = f"sendMessage?chat_id={cid}&text={body}"
if reply_to:
url += f'&reply_to_message_id={reply_to}'
if reply_markup:
reply_markup = json.dumps(reply_markup)
reply_markup = requests.utils.quote(reply_markup)
url += f'&reply_markup={reply_markup}'
url += f'&parse_mode=html'
print(url)
r = requests.post(apiBase + url)
return r.json()
# https://core.telegram.org/bots/api#sendphoto
def send_photo(cid: str, file_id: str, caption="", reply_to=None, reply_markup=None):
url = f"sendPhoto?chat_id={cid}&photo={file_id}&caption={caption}"
if reply_to:
url += f'&reply_to_message_id={reply_to}'
if reply_markup:
reply_markup = json.dumps(reply_markup)
reply_markup = requests.utils.quote(reply_markup)
url += f'&reply_markup={reply_markup}'
url += f'&parse_mode=html'
print(url)
r = requests.post(apiBase + url)
return r.json()
# https://core.telegram.org/bots/api#banchatmember
def ban_member(chat_id, user_id, until_date=None):
url = apiBase + f"banChatMember?chat_id={chat_id}&user_id={user_id}"
if until_date:
url += f'&until_data={until_date}'
r = requests.post(url)
return r.json()
# https://core.telegram.org/bots/api#unbanchatmember
def unban_member(chat_id, user_id):
url = apiBase + f"unbanChatMember?chat_id={chat_id}&user_id={user_id}&only_if_banned=1"
r = requests.post(url)
return r.json()
# https://core.telegram.org/bots/api#addchatmember
def add_member(chat_id, user_id):
url = apiBase + f"addChatMember?chat_id={chat_id}&user_id={user_id}"
r = requests.post(url)
return r.json()
def forward_message(cid, mid, to_chat_id):
url = apiBase + f"forwardMessage?chat_id={to_chat_id}" + \
f"&from_chat_id={cid}&message_id={mid}"
r = requests.post(url)
return r.json()
# https://core.telegram.org/bots/api#restrictchatmember
def mute_member(chat_id, member_id):
chat_permissions = json.dumps({ "can_send_messages": False })
chat_permissions = requests.utils.quote(chat_permissions)
url = apiBase + f'restrictChatMember?chat_id={chat_id}' + \
f'&user_id={member_id}&permissions={chat_permissions}' + \
f'&use_independent_chat_permissions=1'
r = requests.post(url)
return r.json()
# https://core.telegram.org/bots/api#restrictchatmember
def unmute_member(chat_id, member_id, chat_permissions=None):
if not chat_permissions:
chat_permissions = json.dumps({
"can_send_messages": True,
"can_send_photos": True,
"can_send_other_messages": True,
"can_send_polls": True,
"can_add_web_page_previews": True,
"can_send_audios": True,
"can_invite_users": True,
"can_send_voice_notes": True,
"can_send_video_notes": True,
"can_send_videos": True,
"can_send_documents": True
})
chat_permissions = requests.utils.quote(chat_permissions)
url = apiBase + f'restrictChatMember?chat_id={chat_id}' + \
f'&user_id={member_id}&permissions={chat_permissions}' + \
f'&use_independent_chat_permissions=1'
r = requests.post(url)
return r.json()
# https://core.telegram.org/bots/api#approvechatjoinrequest
def approve_chat_join_request(chat_id, user_id):
url = apiBase + f"approveChatJoinRequest?chat_id={chat_id}" + \
f'&user_id={user_id}'
r = requests.post(url)
return r.json()
# https://core.telegram.org/bots/api#senddocument
async def send_document(chat_id, data='', filename='chart.svg'):
url = apiBase + "sendDocument"
params = { "chat_id": chat_id }
filedata = aiohttp.FormData()
filedata.add_field('document', data, filename=filename)
async with aiohttp.ClientSession() as session:
async with session.post(url, params=params, data=filedata) as response:
if response.status != 200:
error_text = await response.text()
print(f"Error sending document: {response.status} - {error_text}")
return None
try:
return await response.json()
except ValueError as e:
print(f"Error decoding JSON: {e}")
return None
# https://core.telegram.org/bots/api#getchatadministrators
def get_chat_administrators(chat_id):
url = apiBase + f"getChatAdministrators?chat_id={chat_id}"
r = requests.get(url)
return r.json()
# https://core.telegram.org/bots/api#getchatmember
def get_member(chat_id, member_id):
url = apiBase + f"getChatMember?chat_id={member_id}&user_id={member_id}"
r = requests.get(url)
return r.json()
# https://core.telegram.org/bots/api#getuserprofilephotos
def get_userphotos(user_id):
url = apiBase + f"getUserProfilePhotos?user_id={user_id}"
r = requests.get(url)
return r.json()
# https://core.telegram.org/bots/api#editmessagereplymarkup
def edit_replymarkup(cid, mid, reply_markup):
reply_markup = json.dumps(reply_markup)
reply_markup = requests.utils.quote(reply_markup)
url = f"editMessageReplyMarkup?chat_id={cid}&message_id={mid}&reply_markup={reply_markup}"
r = requests.post(apiBase + url)
return r.json()
# https://core.telegram.org/bots/api#getchat
def get_chat(cid):
url = apiBase + f"getChat?chat_id={cid}"
r = requests.get(url)
return r.json()
# https://core.telegram.org/bots/api#banchatmember
def kick_member(chat_id, member_id):
url = f"banChatSenderChat?chat_id={cid}&user_id={member_id}"
r = requests.post(apiBase + url)
print(r.json())
url = f"unbanChatSenderChat?chat_id={cid}&user_id={member_id}&only_if_banned=1"
r = requests.post(apiBase + url)
return r.json()

6
bot/config.py Normal file
View File

@@ -0,0 +1,6 @@
import os
BOT_TOKEN = os.environ.get('BOT_TOKEN') or ''
WEBHOOK = os.environ.get('VERCEL_URL') or 'http://localhost:8000'
REDIS_URL = os.environ.get('REDIS_URL') or 'redis://localhost:6379'
FEEDBACK_CHAT_ID = os.environ.get('FEEDBACK_CHAT_ID').replace("-", "-100")

View File

@@ -0,0 +1,54 @@
from bot.api import send_message, delete_message, kick_member
from bot.handlers.command_my import handle_command_my
from bot.utils.mention import userdata_extract
from bot.storage import Profile
# remove link of callback sender
# from member vouched before
def handle_unlink(payload):
print('handle unlink button pressed or command, private chat only')
from_id = str(payload['from']['id'])
linked_id = ''
if 'data' in payload:
linked_id = str(payload['data'].replace('unlink', ''))
elif 'text' in payload:
linked_id = str(payload['text'].replace('/unlink ', ''))
# удаляем связь с потомком
actor = Profile.get(from_id, payload)
actor['children'].remove(str(linked_id))
Profile.save(actor)
# удаляем связь с предком
linked = Profile.get(linked_id)
linked['parents'].remove(str(from_id))
Profile.save(linked)
# удаляем старое сообщение с кнопками-unlink
reply_msg_id = payload['message']['message_id']
r = delete_message(from_id, reply_msg_id)
print(r)
# если ещё есть связи - посылаем новое сообщение
if len(actor['children']) > 0:
handle_command_my(payload)
lang = payload['from'].get('language_code', 'ru')
for chat_id in linked['chats']:
# если больше никто не поручился - kick out
if len(linked['parents']) == 0:
r = kick_member(chat_id, linked_id)
print(r)
if r['ok']:
_, identity, username = userdata_extract(linked['result']['user'])
body = ('Участник %s%s был удалён' if lang == 'ru' else 'Member %s%s was deleted') % (identity, username)
r = send_message(chat_id, body)
print(r)
# обновление счётчика
update_button(linked_id, chat_id)

View File

@@ -0,0 +1,66 @@
from bot.api import send_message, forward_message, delete_message, \
approve_chat_join_request, edit_replymarkup, get_chat
from bot.storage import Profile, storage
def update_button(chat_id, member_id, text='❤️'):
button_message_id = storage.get(f'btn-{chat_id}-{member_id}')
print(f'button_message_id: {button_message_id}')
if button_message_id:
button_message_id = button_message_id.decode('utf-8')
print(f'button_message_id: {button_message_id}')
print('update reply markup')
newcomer = Profile.get(member_id)
amount = len(newcomer['parents']) + 1
text += f' {amount}'
rm = {
"inline_keyboard": [
[
{
"text": text,
"callback_data": 'vouch' + member_id
}
]
]
}
r = edit_replymarkup(chat_id, button_message_id, reply_markup=rm)
print(r)
def handle_button(callback_query):
# получаем профиль нажавшего кнопку
actor_id = str(callback_query['from']['id'])
actor = Profile.get(actor_id, callback_query)
callback_data = callback_query['data']
if callback_data.startswith('vouch'):
print(f'button pressed by {actor_id}')
newcomer_id = callback_data[5:]
print(f'button pressed for {newcomer_id}')
newcomer = Profile.get(newcomer_id)
print(f'newcomer profile {newcomer}')
if newcomer_id == actor_id:
# нажал сам, не реагируем, прописываем данные
newcomer = Profile.get(newcomer_id, callback_query)
else:
# нажал кто-то другой
if str(actor_id) not in newcomer['parents']:
print(f'save parent for {newcomer_id}')
newcomer['parents'].append(str(actor_id))
Profile.save(newcomer)
if str(newcomer_id) not in actor['children']:
print(f'save child for {actor_id}')
actor['children'].append(str(newcomer_id))
Profile.save(actor)
chat_id = str(callback_query['message']['chat']['id'])
print('accept join request')
r = approve_chat_join_request(chat_id, newcomer_id)
print(r)
update_button(chat_id, newcomer_id)

View File

@@ -0,0 +1,17 @@
from bot.storage import Profile
from bot.handlers.send_button import show_request_msg
from bot.api import get_member
def handle_command_ask(msg):
print(f'handling request resend')
cmd, chat_id, member_id = msg['text'].split(' ')
chat_id = chat_id.replace('-', '-100')
r = get_member(chat_id, member_id)
print(r)
m = {}
if 'result' in r:
m['from'] = r['result']['user']
m['chat'] = { 'id': str(chat_id) }
show_request_msg(m)
elif 'error_code' in r:
print(r)

View File

@@ -0,0 +1,12 @@
from bot.utils.graph import generate_chart
from bot.api import send_document
from bot.storage import storage, scan
import json
async def handle_command_graph(msg):
usr_ids, members = scan(match='usr-*', count=100)
data = generate_chart(members)
if data:
r = await send_document(msg['chat']['id'], data, 'chart.svg')
print(r)

View File

@@ -0,0 +1,56 @@
from bot.storage import Profile, scan
from bot.api import get_member, send_message, get_chat_administrators
from bot.utils.mention import userdata_extract
def construct_unlink_buttons(actor):
buttons = []
for vouch in actor['children']:
for chat_id in actor['chats']:
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
})
return { "inline_keyboard": [ buttons, ] }
def handle_command_my(msg):
print(f'handle my command')
from_id = str(msg['from']['id'])
sender = Profile.get(from_id, msg)
handle_command_owner_my(msg)
# генерируем кнопки для всех, за кого поручились
reply_markup = construct_unlink_buttons(sender)
if msg['from'].get('language_code', 'ru') == 'ru':
body = 'Нажмите кнопки ниже, чтобы удалить ваши связи'
else:
body = 'Unlink your connections pressing the buttons below'
r = send_message(from_id, body, reply_markup=reply_markup)
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)
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)

View File

@@ -0,0 +1,41 @@
from bot.api import send_message, delete_message, get_chat_administrators
from bot.storage import Profile, storage
from bot.handlers.send_button import show_request_msg
def handle_default(msg):
print(f'default handler for all messages')
chat_id = str(msg['chat']['id'])
from_id = str(msg['from']['id'])
sender = Profile.get(from_id, msg)
if msg['text'].startswith('/my'):
# команда в групповом чате
print(f'remove some messages in group chat')
# удалить сообщение с командой /my
r = delete_message(chat_id, msg['message_id'])
print(r)
# показать новое сообщение с кнопкой
show_request_msg(msg)
else:
# любое другое сообщение
if len(sender['parents']) == 0:
# владелец чата автоматически ручается
print(f'setting owner as parent for {from_id}')
r = get_chat_administrators(chat_id)
print(r)
owner_id = ''
for admin in r['result']:
if admin['status'] == 'creator':
owner_id = admin['user']['id']
break
if owner_id:
sender['parents'].append(str(owner_id))
# обновляем профиль владельца
owner = Profile.get(owner_id)
owner['children'].append(str(from_id))
Profile.save(owner)
# сохранить профиль отправителя
Profile.save(sender)

View File

@@ -0,0 +1,44 @@
import json
from bot.api import send_message, forward_message, delete_message, get_chat_administrators
from bot.handlers.send_button import show_request_msg
from bot.utils.mention import userdata_extract
from bot.storage import storage, Profile
from bot.config import FEEDBACK_CHAT_ID
def handle_feedback(msg):
mid = msg['message_id']
cid = msg['chat']['id']
if msg['text'] == '/start':
r = send_message(cid, 'Напишите своё сообщение для администрации чата')
print(r)
else:
r = forward_message(cid, mid, FEEDBACK_CHAT_ID)
support_msg_id = r['result']['message_id']
# сохранение айди сообщения в приватной переписке с ботом
storage.set(f'fbk-{support_msg_id}', json.dumps({
"author_id": msg["from"]["id"],
"message_id": mid,
"chat_id": cid
}))
def handle_answer(msg):
answered_msg = msg['reply_to_message']
r = get_chat_administrators(msg['chat']['id'])
print(r)
admins = []
for a in r['result']:
admins.append(a['user']['id'])
if answered_msg['from']['is_bot'] and msg['from']['id'] in admins:
support_msg_id = str(answered_msg['message_id'])
# получение сохраненного информации о сообщении для ответа
stored_feedback = storage.get(f'fbk-{support_msg_id}')
if stored_feedback:
print(f'handle answer from support')
stored_feedback = json.loads(stored_feedback)
r = send_message(f'{stored_feedback["chat_id"]}', msg['text'], reply_to=stored_feedback["message_id"])
print(r)

View File

@@ -0,0 +1,20 @@
from bot.api import approve_chat_join_request, delete_message
from bot.handlers.send_button import show_request_msg
from bot.storage import Profile, storage
def handle_join_request(msg):
print(f'handle join request {msg}')
chat_id = str(msg['chat']['id'])
from_id = str(msg['from']['id'])
actor = Profile.get(from_id, msg)
if len(actor['parents']) == 0:
# показываем сообщение с кнопкой "поручиться"
show_request_msg(msg)
else:
# за пользователя поручились ранее
r = approve_chat_join_request(chat_id, from_id)
print(r)
Profile.save(actor)

View File

@@ -0,0 +1,44 @@
from bot.handlers.send_button import show_request_msg
from bot.api import delete_message
from bot.storage import Profile, storage
from bot.config import FEEDBACK_CHAT_ID
def handle_join(msg):
chat_id = str(msg['chat']['id'])
from_id = str(msg['from']['id'])
actor = Profile.get(from_id, msg)
newcomer_id = str(msg['new_chat_member']['id'])
if from_id == newcomer_id:
if len(actor['parents']) == 0 and str(chat_id) != FEEDBACK_CHAT_ID:
# показываем сообщение с кнопкой "поручиться"
show_request_msg(msg)
else:
# за пользователя поручились ранее
pass
else:
# пользователи приглашены другим участником
print(f'{len(msg["new_chat_members"])} members were invited by {from_id}')
for m in msg['new_chat_members']:
newcomer = Profile.get(m['id'])
newcomer['parents'].append(str(from_id))
Profile.save(newcomer)
actor['children'].append(str(m['id']))
# обновляем профиль пригласившего
Profile.save(actor)
def handle_left(msg):
print(f'handling member leaving')
member_id = msg["left_chat_member"]["id"]
chat_id = msg['chat']['id']
# удаление сообщения с кнопкой в этом чате
prev_msg = storage.get(f'btn-{chat_id}-{member_id}')
if prev_msg:
r = delete_message(chat_id, prev_msg['id'])
print(r)
storage.remove(f'btn-{chat_id}-{member_id}')

View File

@@ -0,0 +1,29 @@
from storage import scan, Profile
from bot.api import approve_chat_join_request, kick_member
from bot.handlers.callback_vouch import update_button
from bot.utils.mention import userdata_extract
# устанавливает соответствие данных
def handle_startup():
btn_ids, btns = scan(match='btn-*', count=100)
for btnid in btn_ids:
# для каждой ранее созданной кнопки
btnid_str = btnid.decode("utf-8").replace("btn-", "")
parts = btnid_str.split('-')
print(parts)
_, chat_id, member_id = parts
chat_id = "-" + chat_id
newcomer = Profile.get(member_id)
if len(newcomer.get('parents', [])) > 0:
# принять заявку если её нажимали
r = approve_chat_join_request(chat_id, member_id)
print(r)
update_button(chat_id, member_id)
elif len(newcomer.get('parents', [])) == 0:
r = kick_member(chat_id, member_id)
print(r)
if r['ok']:
_, identity, username = userdata_extract(newcomer['result']['user'])
body = ('Участник %s%s был удалён' if lang == 'ru' else 'Member %s%s was deleted') % (identity, username)
r = send_message(chat_id, body)
print(r)

View File

@@ -0,0 +1,52 @@
from bot.api import send_message, send_photo, get_userphotos
from bot.utils.mention import mention, userdata_extract
from bot.storage import storage
def show_request_msg(msg):
chat_id = str(msg['chat']['id'])
from_id = str(msg['from']['id'])
lang = msg['from'].get('language_code', 'ru')
reply_markup = {
"inline_keyboard": [
[
{
"text": '❤️',
"callback_data": 'vouch' + from_id
}
]
]
}
newcomer_message = "Нажмите, чтобы одобрить заявку " if lang == 'ru' \
else "There is a newcomer, press the button if you are connected with "
r = get_userphotos(user_id=from_id)
print(r)
if r['ok'] and r['result']['total_count'] > 0:
print('show button with photo')
file_id = r['result']['photos'][0][0]['file_id']
_uid, identity, username = userdata_extract(msg['from'])
r = send_photo(
chat_id,
file_id,
caption=newcomer_message + f'{identity}{username}',
reply_to=msg.get('message_id', ''),
reply_markup=reply_markup
)
else:
print('show button without photo')
r = send_message(
chat_id,
newcomer_message + mention(msg['from']),
reply_to=msg.get('message_id', ''),
reply_markup=reply_markup
)
print(r)
if 'message_id' in r:
# удаляем предыдущее сообщение с кнопкой в этом чате
prevbtn = storage.get(f'btn-{chat_id}-{from_id}')
if prevbtn:
r = delete_message(chat_id, prevbtn)
print(r)
# создаём новое
newbtn = r['message_id']
print(f'button message id: {newbtn}')
storage.set(f'btn-{chat_id}-{from_id}', newbtn)

35
bot/storage/__init__.py Normal file
View File

@@ -0,0 +1,35 @@
import redis
from profile import Profile as ProfileObj
from bot.config import REDIS_URL
import json
# сохраняет сессии, айди кнопок в чатах для удаления и пересылаемые сообщения между перезагрузками
storage = redis.from_url(REDIS_URL)
# хранение необходимой информации о пользователях
Profile = ProfileObj(storage)
# достаёт из хранилища jsonы по маске и количеству
def scan(match='usr-*', count=100):
cursor = 0
keys = []
r = storage
while True:
# Scan for keys starting with <match> in batches of <count>
cursor, batch_keys = r.scan(cursor=cursor, match=match, count=count)
keys += batch_keys
# If the cursor is 0, then we've reached the end of the keys
if cursor == 0:
break
# Get the values of all the keys
values = r.mget(keys)
# Parse the JSON data from each value
items = []
for value in values:
value_str = value.decode('utf-8')
i = json.loads(value_str)
items.append(i)
print(f'scan found {len(items)} items')
return keys, items

41
bot/storage/profile.py Normal file
View File

@@ -0,0 +1,41 @@
import json
class Profile:
def __init__(self, storage):
self.storage = storage
def create(self, member_id, msg=None):
s = {
"id": member_id,
"parents": [],
"children": [],
"chats": []
}
if msg:
if 'from' in msg:
sender = msg.get('from')
s["mention"] = sender.get('username')
s["name"] = f"{sender['first_name']} {sender.get('last_name', '')}".strip()
if 'chat' in msg:
chat_id = str(msg['chat']['id'])
if chat_id not in s['chats']:
s["chats"].append(chat_id)
self.storage.set(f'usr-{member_id}', json.dumps(s))
return s
def save(self, s):
self.storage.set(f'usr-{s["id"]}', json.dumps(s))
def get(self, member_id, msg=None):
data = self.storage.get(f'usr-{member_id}')
if data is None:
r = self.create(member_id, msg)
else:
r = json.loads(data)
return r

67
bot/utils/graph.py Normal file
View File

@@ -0,0 +1,67 @@
# Define SVG code generation function with member_id parameter
def generate_chart(members, member_id = None):
if not member_id:
member_id = members[0]['id']
# Define some constants for layout
node_radius = 30
node_spacing = 80
node_y = 100
parent_y_offset = 50
child_y_offset = 150
# Find the specified member and its descendants
member = None
descendants = set()
for m in members:
if m["id"] == member_id:
member = m
descendants.add(member_id)
break
stack = member["children"].copy()
while stack:
child_id = stack.pop()
descendants.add(child_id)
for m in members:
if m["id"] == child_id:
stack.extend(m["children"])
break
# Define the x position for each member
x_positions = {}
x_positions[member_id] = 0
for i, m in enumerate(members):
if m["id"] in descendants:
x_positions[m["id"]] = (i * node_spacing) + node_radius
# Start building the SVG code
svg_width = (len(descendants) * node_spacing) + (2 * node_radius)
svg_height = 200
svg_code = f'<svg width="{svg_width}" height="{svg_height}">'
# Generate nodes and names for each descendant
for m in members:
if m["id"] in descendants:
node_x = x_positions[m["id"]]
node_code = f'<circle cx="{node_x}" cy="{node_y}" r="{node_radius}" stroke="black" stroke-width="2" fill="white"/>'
name_code = f'<text x="{node_x}" y="{node_y}" font-size="16" text-anchor="middle">{m["name"]}</text>'
svg_code += node_code + name_code
# Generate links to parent nodes
for parent_id in m["parents"]:
if parent_id in descendants:
parent_x = x_positions[parent_id]
link_code = f'<line x1="{node_x}" y1="{node_y - parent_y_offset}" x2="{parent_x}" y2="{node_y}" stroke="black" stroke-width="2"/>'
svg_code += link_code
# Generate links to child nodes
for child_id in m["children"]:
if child_id in descendants:
child_x = x_positions[child_id]
link_code = f'<line x1="{node_x}" y1="{node_y + child_y_offset}" x2="{child_x}" y2="{node_y}" stroke="black" stroke-width="2"/>'
svg_code += link_code
# Finish the SVG code
svg_code += '</svg>'
return svg_code.encode('utf-8')

20
bot/utils/mention.py Normal file
View File

@@ -0,0 +1,20 @@
def escape_username(username):
# Replace any non-ASCII and non-alphanumeric characters with underscores
return ''.join(c if c.isalnum() or c.isspace() else '-' for c in username)
# generates a mention from standard telegram web json 'from' field
# using HTML markup
def mention(user):
uid, identity, username = userdata_extract(user)
identity = escape_username(identity)
return f'<a href="tg://user?id={uid}">{identity} {username}</a>'
def userdata_extract(user):
identity = f"{user['first_name']} {user.get('last_name', '')}".strip()
uid = user['id']
username = user.get('username', '')
if username:
username = f'(@{username})'
return uid, identity, username