36 lines
1019 B
Python
36 lines
1019 B
Python
|
|
from telethon.sync import TelegramClient
|
||
|
|
|
||
|
|
# get your api_id, api_hash, token
|
||
|
|
# from telegram as described above
|
||
|
|
api_id = '2452309'
|
||
|
|
api_hash = '4cc6a58946508e59547d15d530a421ae'
|
||
|
|
|
||
|
|
# your phone number
|
||
|
|
phone = '+13616553044'
|
||
|
|
|
||
|
|
# creating a telegram session and assigning
|
||
|
|
# it to a variable client
|
||
|
|
client = TelegramClient('session', api_id, api_hash)
|
||
|
|
|
||
|
|
# connecting and building the session
|
||
|
|
client.connect()
|
||
|
|
|
||
|
|
# in case of script ran first time it will
|
||
|
|
# ask either to input token or otp sent to
|
||
|
|
# number or sent or your telegram id
|
||
|
|
if not client.is_user_authorized():
|
||
|
|
client.send_code_request(phone)
|
||
|
|
|
||
|
|
# signing in the client
|
||
|
|
client.sign_in(phone, input('Enter the code: '))
|
||
|
|
try:
|
||
|
|
schlingelId=5070349790
|
||
|
|
client.send_message(schlingelId, "test message", parse_mode='html')
|
||
|
|
except Exception as e:
|
||
|
|
# there may be many error coming in while like peer
|
||
|
|
# error, wwrong access_hash, flood_error, etc
|
||
|
|
print(e);
|
||
|
|
|
||
|
|
# disconnecting the telegram session
|
||
|
|
client.disconnect()
|