-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (41 loc) · 1.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from telethon import TelegramClient
import os
import time
sent_to = map(int, open("sent.txt", "r", encoding="utf-8").read().split("\n"))
api_id = int(os.environ['API_ID'])
api_hash = os.environ['API_HASH']
client = TelegramClient('dmbroadcast', api_id, api_hash)
client.start()
async def main():
dialogs = await client.get_dialogs()
groups = [dialog for dialog in dialogs if dialog.is_group]
for index in range(len(groups)):
group = groups[index]
print(index, group.name, group.id)
group_index = int(input("Enter group to broadcast messages to: "))
group = groups[group_index]
self = await client.get_me()
participants = await client.get_participants(group.id)
if not any(participant.id == self.id and hasattr(participant.participant, "admin_rights") and participant.participant.admin_rights is not None for participant in participants):
print("You are not an admin in this group")
return
message = open("message.txt", "r", encoding="utf-8").read()
print(f"Sending message to {len(participants)} participants in {group.name}")
print("Message:")
print(message)
input("Press enter to continue")
async for participant in client.iter_participants(group.id):
if participant.id in sent_to:
continue
if participant.id == self.id:
continue
try:
await client.send_message(participant.id, message)
print(f"Sent message to {participant.id}")
except Exception as e:
print(f"Failed to send message to {participant.id}")
print(e.__class__.__name__)
print(e)
time.sleep(2)
with client:
client.loop.run_until_complete(main())