This commit is contained in:
2023-04-28 15:24:14 +03:00
parent 351b53f007
commit 318b24243a
20 changed files with 244 additions and 154 deletions

View File

@@ -1,9 +1,35 @@
import redis
from tgbot.storage.profile import Profile as ProfileObj
from tgbot.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

View File

@@ -14,15 +14,17 @@ class Profile:
"chats": []
}
if msg.get('from'):
sender = msg.get('from')
s["mention"] = sender.get('username')
s["name"] = f"{sender['first_name']} {sender.get('last_name', '')}".strip()
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 msg.get('chat'):
chat_id = str(msg['chat']['id'])
if chat_id not in s['chats']:
s["chats"].append(chat_id)
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