2016-09-04 12:07:18 +03:00
|
|
|
import tl_generator
|
2016-09-08 17:55:46 +03:00
|
|
|
if not tl_generator.tlobjects_exist():
|
|
|
|
import errors
|
|
|
|
raise errors.TLGeneratorNotRan()
|
|
|
|
else:
|
|
|
|
del tl_generator
|
|
|
|
|
2016-09-04 12:07:18 +03:00
|
|
|
from tl.telegram_client import TelegramClient
|
|
|
|
from utils.helpers import load_settings
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-09-08 13:13:31 +03:00
|
|
|
from datetime import datetime
|
|
|
|
|
2016-09-03 11:54:58 +03:00
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
if __name__ == '__main__':
|
2016-09-08 17:55:46 +03:00
|
|
|
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
|
2016-09-10 19:05:20 +03:00
|
|
|
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
|
|
|
|
|
2016-09-08 17:55:46 +03:00
|
|
|
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.')
|
|
|
|
|
|
|
|
# And start a while loop to chat
|
2016-09-08 13:13:31 +03:00
|
|
|
while True:
|
2016-09-08 17:55:46 +03:00
|
|
|
msg = input('Enter a message: ')
|
|
|
|
# Quit
|
|
|
|
if msg == '!q':
|
2016-09-07 12:36:34 +03:00
|
|
|
break
|
2016-09-08 13:13:31 +03:00
|
|
|
|
2016-09-08 17:55:46 +03:00
|
|
|
# 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))
|
|
|
|
|
2016-09-10 12:01:03 +03:00
|
|
|
# Send chat message (if any)
|
|
|
|
elif msg:
|
2016-09-08 17:55:46 +03:00
|
|
|
client.send_message(input_peer, msg, markdown=True, no_web_page=True)
|
|
|
|
|
2016-09-09 12:47:37 +03:00
|
|
|
print('Thanks for trying the interactive example! Exiting...')
|
|
|
|
client.disconnect()
|