lono/send_users_list.py

41 lines
1.0 KiB
Python
Raw Normal View History

2019-03-03 23:23:12 +00:00
#!/usr/bin/env python3
2020-06-17 11:50:54 +00:00
import json
2019-03-03 23:23:12 +00:00
from telegram import Bot
from db import get_conn, Subscriber
2019-03-03 23:26:51 +00:00
from config import BOT_TOKEN, MANAGEMENT_CHAT
2019-03-03 23:23:12 +00:00
2019-03-14 07:41:28 +00:00
def send_users_list(bot: Bot = None, conn=None):
if conn is None:
conn = get_conn(read_only=True)
2019-03-03 23:23:12 +00:00
if not bot:
bot = Bot(BOT_TOKEN)
2020-06-17 11:50:54 +00:00
try:
with open('fake_users.json') as f:
subs = [Subscriber(*data) for data in json.load(f).items()]
except:
subs = conn.root.subscribers.values()
2019-03-03 23:23:12 +00:00
2019-03-03 23:25:10 +00:00
messages = [f'Count: {len(subs)}\n']
2019-03-03 23:23:12 +00:00
for sub in subs: # type: Subscriber
2019-03-03 23:25:45 +00:00
msg = f'<code>{sub.uid:>14}</code> '
2019-03-03 23:23:12 +00:00
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):
2019-03-03 23:26:51 +00:00
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()
2019-03-03 23:23:12 +00:00
if __name__ == '__main__':
send_users_list()