2016-09-04 11:07:18 +02:00
|
|
|
import tl_generator
|
|
|
|
from tl.telegram_client import TelegramClient
|
|
|
|
from utils.helpers import load_settings
|
2016-08-26 12:58:53 +02:00
|
|
|
|
2016-09-03 10:54:58 +02:00
|
|
|
|
2016-08-26 12:58:53 +02:00
|
|
|
if __name__ == '__main__':
|
2016-09-04 11:07:18 +02:00
|
|
|
if not tl_generator.tlobjects_exist():
|
|
|
|
print('Please run `python3 tl_generator.py` first!')
|
2016-08-28 13:43:00 +02:00
|
|
|
|
2016-09-04 11:07:18 +02:00
|
|
|
else:
|
2016-09-07 11:36:34 +02:00
|
|
|
print('Loading interactive example...')
|
|
|
|
|
|
|
|
# First, initialize our TelegramClient and connect
|
2016-09-04 11:07:18 +02:00
|
|
|
settings = load_settings()
|
|
|
|
client = TelegramClient(session_user_id=settings.get('session_name', 'anonymous'),
|
2016-09-06 18:54:49 +02:00
|
|
|
layer=55,
|
2016-09-04 11:07:18 +02:00
|
|
|
api_id=settings['api_id'],
|
|
|
|
api_hash=settings['api_hash'])
|
|
|
|
|
|
|
|
client.connect()
|
2016-09-06 18:54:49 +02:00
|
|
|
input('You should now be connected. Press enter when you are ready to continue.')
|
2016-09-07 11:36:34 +02:00
|
|
|
|
|
|
|
# Then, ensure we're authorized and have access
|
2016-09-04 11:07:18 +02:00
|
|
|
if not client.is_user_authorized():
|
2016-09-06 18:54:49 +02:00
|
|
|
client.send_code_request(str(settings['user_phone']))
|
2016-09-05 18:35:12 +02:00
|
|
|
|
2016-09-04 11:07:18 +02:00
|
|
|
code = input('Enter the code you just received: ')
|
2016-09-06 18:54:49 +02:00
|
|
|
client.make_auth(settings['user_phone'], code)
|
|
|
|
|
2016-09-07 11:36:34 +02:00
|
|
|
# After that, load the top dialogs and show a list
|
|
|
|
# We use zip(*list_of_tuples) to pair all the elements together,
|
|
|
|
# hence being able to return a new list of each triple pair!
|
|
|
|
# See http://stackoverflow.com/a/12974504/4759433 for a better explanation
|
|
|
|
dialogs, displays, inputs = zip(*client.get_dialogs(8))
|
|
|
|
|
|
|
|
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 = int(input('Who do you want to send messages to?: ')) - 1
|
|
|
|
dialog = dialogs[i]
|
|
|
|
display = displays[i]
|
|
|
|
input_peer = inputs[i]
|
|
|
|
|
|
|
|
# And start a while loop!
|
|
|
|
print('You are now sending messages to "{}". Type "!q" when you want to exit.'.format(display))
|
|
|
|
while True:
|
|
|
|
msg = input('Enter a message: ')
|
|
|
|
if msg == '!q':
|
|
|
|
break
|
|
|
|
client.send_message(input_peer, msg)
|
|
|
|
|
|
|
|
print('Thanks for trying the interactive example! Exiting.')
|