mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-10 19:46:36 +03:00
7e78b1b6dc
Uploading and sending media are different things. Once you have uploaded a media file, you can send it to many users without uploading it again, since you have a handle to the uploaded file. Other fixes include not showing additional data on error messages and not generating correct code for sending bytes
101 lines
3.6 KiB
Python
Executable File
101 lines
3.6 KiB
Python
Executable File
import tl_generator
|
|
if not tl_generator.tlobjects_exist():
|
|
import errors
|
|
raise errors.TLGeneratorNotRan()
|
|
else:
|
|
del tl_generator
|
|
|
|
from tl.telegram_client import TelegramClient
|
|
from utils.helpers import load_settings
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print('Loading interactive example...')
|
|
|
|
# First, initialize our TelegramClient and connect
|
|
settings = load_settings()
|
|
client = TelegramClient(session_user_id=settings.get('session_name', 'anonymous'),
|
|
layer=55,
|
|
api_id=settings['api_id'],
|
|
api_hash=settings['api_hash'])
|
|
|
|
client.connect()
|
|
input('You should now be connected. Press enter when you are ready to continue.')
|
|
|
|
# Then, ensure we're authorized and have access
|
|
if not client.is_user_authorized():
|
|
client.send_code_request(str(settings['user_phone']))
|
|
|
|
code = input('Enter the code you just received: ')
|
|
client.make_auth(settings['user_phone'], code)
|
|
|
|
# Enter a while loop to chat as long as the user wants
|
|
while True:
|
|
# Retrieve the top dialogs
|
|
dialogs, displays, inputs = client.get_dialogs(8)
|
|
|
|
# Display them so the user can choose
|
|
for i, display in enumerate(displays):
|
|
i += 1 # 1-based index for normies
|
|
print('{}. {}'.format(i, display))
|
|
|
|
# Let the user decide who they want to talk to
|
|
i = None
|
|
while i is None:
|
|
try:
|
|
i = int(input('Who do you want to send messages to (0 to exit)?: ')) - 1
|
|
except ValueError:
|
|
pass
|
|
|
|
if i == -1:
|
|
break
|
|
|
|
# Retrieve the selected user
|
|
dialog = dialogs[i]
|
|
display = displays[i]
|
|
input_peer = inputs[i]
|
|
|
|
# Show some information
|
|
print('You are now sending messages to "{}". Available commands:'.format(display))
|
|
print(' !q: Quits the current chat.')
|
|
print(' !h: prints the latest messages (message History) of the chat.')
|
|
print(' !p <path>: sends a Photo located at the given path')
|
|
|
|
# And start a while loop to chat
|
|
while True:
|
|
msg = input('Enter a message: ')
|
|
# Quit
|
|
if msg == '!q':
|
|
break
|
|
|
|
# History
|
|
elif msg == '!h':
|
|
# First retrieve the messages and some information
|
|
total_count, messages, senders = client.get_message_history(input_peer, limit=10)
|
|
# Iterate over all (in reverse order so the latest appears the last in the console)
|
|
# and print them in "[hh:mm] Sender: Message" text format
|
|
for msg, sender in zip(reversed(messages), reversed(senders)):
|
|
name = sender.first_name if sender else '???'
|
|
date = datetime.fromtimestamp(msg.date)
|
|
print('[{}:{}] {}: {}'.format(date.hour, date.minute, name, msg.message))
|
|
|
|
# Send photo
|
|
elif msg.startswith('!p '):
|
|
file_path = msg[len('!p '):] # Slice the message to get the path
|
|
|
|
print('Uploading {}...'.format(file_path))
|
|
input_file = client.upload_file(file_path)
|
|
|
|
# After we have the handle to the uploaded file, send it to our peer
|
|
client.send_photo_file(input_file, input_peer)
|
|
print('Media sent!')
|
|
|
|
# Send chat message (if any)
|
|
elif msg:
|
|
client.send_message(input_peer, msg, markdown=True, no_web_page=True)
|
|
|
|
print('Thanks for trying the interactive example! Exiting...')
|
|
client.disconnect()
|