-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-channel.py
39 lines (29 loc) · 1.05 KB
/
create-channel.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
from datetime import datetime
from telethon import TelegramClient, functions
api_id =
api_hash = ''
phone = ''
client = TelegramClient('session_name', api_id, api_hash)
async def main():
# Login to Telegram
await client.start(phone)
# Create the channel name
channel_name = f"backup-{datetime.now().strftime('%Y%m%d_%H%M%S')}"
# Create the channel
result = await client(functions.channels.CreateChannelRequest(
title=channel_name,
about="Backup channel",
megagroup=False
))
# Get the invite link to the channel
invite_link = await client(functions.messages.ExportChatInviteRequest(result.chats[0].id))
# Store the invite link in a text file
with open("channel_invite_link.txt", "w") as f:
f.write(f"{invite_link.link}\n")
# Print the result
print(f"Channel '{result.chats[0].title}' (id: {result.chats[0].id}) created successfully.")
print(f"Invite link: {invite_link.link}")
# Logout
await client.disconnect()
with client:
client.loop.run_until_complete(main())