welcomecenterbot/handlers/callback_vouch.py

63 lines
2.3 KiB
Python
Raw Normal View History

2024-01-06 11:25:35 +00:00
from bot.api import approve_chat_join_request, edit_replymarkup
2023-09-11 20:21:55 +00:00
from storage import Profile, storage
2023-09-18 07:50:48 +00:00
import logging
2024-01-06 11:25:35 +00:00
2023-09-18 07:50:48 +00:00
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 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}")
2023-09-06 10:08:05 +00:00
if button_message_id:
2023-09-11 20:04:53 +00:00
button_message_id = button_message_id.decode("utf-8")
print(f"button_message_id: {button_message_id}")
print("update reply markup")
2023-09-06 10:08:05 +00:00
newcomer = Profile.get(member_id)
2023-09-11 20:04:53 +00:00
amount = len(newcomer["parents"]) + 1
2023-10-30 06:37:11 +00:00
text = f"❤️ {amount}"
2023-09-06 10:08:05 +00:00
rm = {
2023-09-11 20:04:53 +00:00
"inline_keyboard": [[{"text": text, "callback_data": "vouch" + member_id}]]
2023-09-06 10:08:05 +00:00
}
2023-09-11 20:04:53 +00:00
r = await edit_replymarkup(chat_id, button_message_id, reply_markup=rm)
2023-09-06 10:08:05 +00:00
print(r)
2023-05-04 08:42:52 +00:00
2023-09-11 20:04:53 +00:00
async def handle_button(callback_query):
2023-04-23 16:54:58 +00:00
# получаем профиль нажавшего кнопку
2023-09-11 20:04:53 +00:00
actor_id = str(callback_query["from"]["id"])
2023-04-23 16:54:58 +00:00
actor = Profile.get(actor_id, callback_query)
2023-09-11 20:04:53 +00:00
callback_data = callback_query["data"]
if callback_data.startswith("vouch"):
print(f"button pressed by {actor_id}")
2023-04-28 12:24:14 +00:00
newcomer_id = callback_data[5:]
2023-09-11 20:04:53 +00:00
print(f"button pressed for {newcomer_id}")
2023-04-23 16:54:58 +00:00
newcomer = Profile.get(newcomer_id)
2023-09-11 20:04:53 +00:00
print(f"newcomer profile {newcomer}")
2023-04-23 16:54:58 +00:00
if newcomer_id == actor_id:
# нажал сам, не реагируем, прописываем данные
2023-09-11 20:04:53 +00:00
_newcomer = Profile.get(newcomer_id, callback_query)
2023-04-28 12:24:14 +00:00
else:
2023-09-11 20:04:53 +00:00
# нажал кто-то другой
2023-04-28 12:24:14 +00:00
2023-09-11 20:04:53 +00:00
if str(actor_id) not in newcomer["parents"]:
print(f"save parent for {newcomer_id}")
newcomer["parents"].append(str(actor_id))
Profile.save(newcomer)
2023-04-23 16:54:58 +00:00
2023-09-11 20:04:53 +00:00
if str(newcomer_id) not in actor["children"]:
print(f"save child for {actor_id}")
actor["children"].append(str(newcomer_id))
Profile.save(actor)
2023-04-23 16:54:58 +00:00
2023-10-26 09:00:49 +00:00
chat_id = str(vars(callback_query["message"]["chat"])["id"])
2023-04-28 12:24:14 +00:00
2023-09-11 20:04:53 +00:00
print("accept join request")
r = await approve_chat_join_request(chat_id, newcomer_id)
print(r)
2023-04-28 12:24:14 +00:00
2023-09-11 20:04:53 +00:00
await update_button(chat_id, newcomer_id)