i try to build with python bot that change telegram group url (the real group name) automatically with telethon
If anyone has done this before I would appreciate the help 🙂
import asyncio
import random
import string
from telethon import TelegramClient, events
from telethon.tl.functions.channels import UpdateUsernameRequest
import pprint
bot_token = 'xxxxxxxxxxxxxxxxxxxxxxx'
api_id = 'xxxxxxxx'
api_hash = 'xxxxxxxxxxxxx'
client = TelegramClient('bot', api_id=api_id, api_hash=api_hash).start(bot_token=bot_token)
async def change_group_link(event, group_username):
print("inside Change group link ")
while True:
print("inside While ")
new_username = ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))
try:
result = await client(UpdateUsernameRequest(
channel=event.chat_id,
username=new_username
))
print(f'Group link successfully changed to: @{new_username}')
except Exception as e:
print(f'Failed to change group link for {group_username}: {e}')
await asyncio.sleep(60)
@client.on(events.ChatAction)
async def handler(event):
print("inside handler ")
print(event)
me = await client.get_me()
if event.user_id == me.id:
sender_chat = await event.get_chat()
group_username = sender_chat.username
print(f'Bot was added to group {group_username}')
asyncio.create_task(change_group_link(event, group_username))
client.run_until_disconnected()
`
New contributor
אייל אברהם is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.