lono/send_users_list.py
2019-03-14 10:41:28 +03:00

36 lines
888 B
Python
Executable File

#!/usr/bin/env python3
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)
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()