0.0.7 fixes and graph
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from tgbot.api import send_message, forward_message, delete_message, \
|
||||
ban_member, unban_member, mute_member, unmute_member, \
|
||||
approve_chat_join_request
|
||||
approve_chat_join_request, send_graph
|
||||
from tgbot.graph import generate_chart
|
||||
from tgbot.config import REDIS_URL, FEEDBACK_CHAT_ID, BUTTON_VOUCH, NEWCOMER_MSG
|
||||
import json
|
||||
import redis
|
||||
@@ -13,7 +14,7 @@ storage = redis.from_url(REDIS_URL)
|
||||
# хранение необходимой информации о пользователях
|
||||
Profile = ProfileObj(storage)
|
||||
|
||||
def show_request_msg(msg):
|
||||
def newcomer_show(msg):
|
||||
reply_markup = {
|
||||
"inline_keyboard": [
|
||||
[
|
||||
@@ -24,10 +25,12 @@ def show_request_msg(msg):
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
identity = f"{msg['from']['first_name']} {msg['from'].get('last_name', '')}"
|
||||
if 'username' in msg['from']:
|
||||
identity += f" @{msg['from']['username']}"
|
||||
r = send_message(
|
||||
msg['chat']['id'],
|
||||
NEWCOMER_MSG + f"{msg['from']['first_name']} {msg['from']['last_name']}({msg['from']['username']})",
|
||||
NEWCOMER_MSG + identity,
|
||||
reply_to=msg['message_id'],
|
||||
reply_markup=reply_markup
|
||||
)
|
||||
@@ -67,11 +70,15 @@ def handle_join(msg):
|
||||
|
||||
actor["name"] = msg['from']['first_name'] + msg['from'].get('last_name', '')
|
||||
actor["mention"] = msg['from'].get('username', '')
|
||||
|
||||
if from_id == str(msg['new_chat_member']['id']):
|
||||
newcomer_id = str(msg['new_chat_member']['id'])
|
||||
if from_id == newcomer_id:
|
||||
if len(actor['parents']) == 0:
|
||||
# показываем сообщение с кнопкой "поручиться"
|
||||
welcome_msg_id = show_request_msg(msg)
|
||||
welcome_msg_id = newcomer_show(msg)
|
||||
|
||||
# до одобрения - мьют
|
||||
r = mute_member(chat_id, newcomer_id)
|
||||
print(r.json())
|
||||
|
||||
# обновляем профиль присоединившегося
|
||||
actor['welcome_id'] = welcome_msg_id
|
||||
@@ -88,6 +95,8 @@ def handle_join(msg):
|
||||
newcomer['parents'].append(from_id)
|
||||
Profile.save(newcomer)
|
||||
actor['children'].append(m['id'])
|
||||
r = unmute_member(chat_id, newcomer['id'])
|
||||
print(r.json())
|
||||
|
||||
# обновляем профиль пригласившего
|
||||
Profile.save(actor)
|
||||
@@ -118,7 +127,7 @@ def handle_button(callback_query):
|
||||
|
||||
newcomer_id = callback_data[len(BUTTON_VOUCH):]
|
||||
newcomer = Profile.get(newcomer_id)
|
||||
if newcomer_id not in inviter['children'] and \
|
||||
if newcomer_id not in actor['children'] and \
|
||||
actor_id not in newcomer['parents']:
|
||||
newcomer['parents'].append(newcomer_id)
|
||||
actor['children'].append(actor_id)
|
||||
@@ -127,13 +136,14 @@ def handle_button(callback_query):
|
||||
try:
|
||||
chat_id = str(callback_query['message']['chat']['id'])
|
||||
|
||||
print('accept join request for public chat')
|
||||
r = approve_chat_join_request(chat_id, newcomer_id)
|
||||
print(r.json())
|
||||
|
||||
print('unmute newcomer')
|
||||
r = unmute_member(chat_id, newcomer_id)
|
||||
print(r.json())
|
||||
|
||||
print('accept join request')
|
||||
r = approve_chat_join_request(chat_id, newcomer_id)
|
||||
print(r.json())
|
||||
|
||||
except:
|
||||
pass
|
||||
|
||||
@@ -150,4 +160,26 @@ def handle_join_request(update):
|
||||
if from_id == str(update['message']['new_chat_member']['id']):
|
||||
if len(actor['parents']) == 0:
|
||||
# показываем сообщение с кнопкой "поручиться"
|
||||
welcome_msg_id = show_request_msg(update)
|
||||
welcome_msg_id = show_request_msg(update)
|
||||
|
||||
|
||||
def handle_graph(_msg):
|
||||
cursor = 0
|
||||
keys = []
|
||||
while True:
|
||||
# Scan for keys starting with 'urs-*' in batches of 100
|
||||
cursor, batch_keys = r.scan(cursor=cursor, match='urs-*', count=100)
|
||||
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
|
||||
members = []
|
||||
for value in values:
|
||||
member = json.loads(value)
|
||||
members.append(member)
|
||||
png_data = generate_chart(values)
|
||||
r = send_graph(png_data, chat_id)
|
||||
print(r.json())
|
||||
|
Reference in New Issue
Block a user