from telethon import TelegramClient, events api_id = 'your_api_id' # your api id api_hash = 'your_api_hash' # your api hash chat_id = 'your_chat_id' # recipient's chat id or username client = TelegramClient('session_name', api_id, api_hash) @client.on(events.NewMessage()) async def my_event_handler(event): if event.chat_id == chat_id: await client.send_message(chat_id, 'Hello world!') client.start() client.run_until_disconnected()
from telethon import TelegramClient, events api_id = 'your_api_id' api_hash = 'your_api_hash' group_username = 'group_username' client = TelegramClient('session_name', api_id, api_hash) @client.on(events.NewMessage(chats=group_username, from_users=group_username)) async def join_group(event): await event.respond('/join') await client.get_dialogs() print('Joined the group!') client.start() client.run_until_disconnected()This code uses Telethon to join a group using your API id and hash. When the bot receives a message from the group, it responds with the "/join" command and then gets the list of current chats to confirm that it has successfully joined the group. Overall, the Telethon package is a great package library for building Python userbots. It's powerful, easy to use, and offers many features for interacting with messaging platforms such as Telegram.