update
This commit is contained in:
66
bot/handlers/callback_vouch.py
Normal file
66
bot/handlers/callback_vouch.py
Normal 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)
|
Reference in New Issue
Block a user