API ID, hash, and phone number
api_id = YOUR_API_ID
api_hash = 'YOUR_API_HASH'
phone_number = 'YOUR_PHONE_NUMBER'
# Create a TelegramClient instance
client = TelegramClient('session_name', api_id, api_hash)
@client.on(events.NewMessage)
async def my_event_handler(event):
# This function will be called whenever a new message arrives
sender = await event.get_sender()
print(f"New message from: {sender.username} - Message: {event.text}")
# Start the client and begin listening for events
with client:
client.run_until_disconnected()
This code ask me to enter phone number, so the session file not works. Right?
from telethon import TelegramClient from telethon.sessions import StringSession STRING_SESSION = " " client = TelegramClient(StringSession(STRING_SESSION), API_ID, API_HASH)
client = TelegramClient(StringSession(STRING_SESSION), api_id, api_hash) File "/home/admin/.local/lib/python3.9/site-packages/telethon/sessions/string.py", line 33, in init raise ValueError('Not a valid string') ValueError: Not a valid string
You have filled valid session?
from telethon import TelegramClient from telethon.sessions import StringSession STRING_SESSION = "+918xxxxx55.session" # Replace these with your actual API ID, hash, and phone number api_id = xxxxxx api_hash = 'xxxxxxxxx' phone_number = '+9183xxxxxx55' client = TelegramClient(StringSession(STRING_SESSION), api_id, api_hash) # Create a TelegramClient instance @client.on(events.NewMessage) async def my_event_handler(event): # This function will be called whenever a new message arrives sender = await event.get_sender() print(f"New message from: {sender.username} - Message: {event.text}") # Start the client and begin listening for even with client: client.run_until_disconnected() ls command gives .... ... +91833xxxxx.session ....... .....
from telethon.sessions import StringSession from telethon.sync import TelegramClient APP_ID = int(input("Enter APP ID here: \n")) API_HASH = input("Enter API HASH here: \n") with TelegramClient(StringSession(), APP_ID, API_HASH) as client: try: session = client.session.save() client.send_message("me", f"String Session \nTap To Copy. \n{session}") print(f"{session} & a Copy String Generated Sucessfully Check Your Saved Message.") except Exception as sed: print(f"Something Went Wrong While Generating String \nError : {sed}") Run this to generate string session first
It's started maybe run any task;
Обсуждают сегодня