lono/send_users_list.py
2020-06-17 14:50:54 +03:00

41 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
import json
from telegram import Bot
from db import get_conn, Subscriber
from config import BOT_TOKEN, MANAGEMENT_CHAT
def send_users_list(bot: Bot = None, conn=None):
if conn is None:
conn = get_conn(read_only=True)
if not bot:
bot = Bot(BOT_TOKEN)
try:
with open('fake_users.json') as f:
subs = [Subscriber(*data) for data in json.load(f).items()]
except:
subs = conn.root.subscribers.values()
messages = [f'Count: {len(subs)}\n']
for sub in subs: # type: Subscriber
msg = f'<code>{sub.uid:>14}</code> '
if sub.uid < 0:
msg += str(sub.name)
else:
msg += f'<a href="tg://user?id={sub.uid}">{sub.name}</a>'
messages.append(msg)
for i in range(0, len(messages), 40):
bot.send_message(MANAGEMENT_CHAT, '\n'.join(messages[i:i+40]), parse_mode='html')
print('User list was sent to the management chat')
conn.close()
if __name__ == '__main__':
send_users_list()