2017-05-20 16:58:44 +03:00
|
|
|
#!/usr/bin/env python3
|
2016-09-17 21:42:34 +03:00
|
|
|
import traceback
|
2016-11-30 00:29:42 +03:00
|
|
|
|
2017-06-08 15:00:27 +03:00
|
|
|
from telethon_examples.interactive_telegram_client \
|
|
|
|
import InteractiveTelegramClient
|
2016-09-17 21:42:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
def load_settings(path='api/settings'):
|
|
|
|
"""Loads the user settings located under `api/`"""
|
2017-05-21 14:59:16 +03:00
|
|
|
result = {}
|
2016-09-17 21:42:34 +03:00
|
|
|
with open(path, 'r', encoding='utf-8') as file:
|
|
|
|
for line in file:
|
|
|
|
value_pair = line.split('=')
|
|
|
|
left = value_pair[0].strip()
|
|
|
|
right = value_pair[1].strip()
|
|
|
|
if right.isnumeric():
|
2017-05-21 14:59:16 +03:00
|
|
|
result[left] = int(right)
|
2016-09-17 21:42:34 +03:00
|
|
|
else:
|
2017-05-21 14:59:16 +03:00
|
|
|
result[left] = right
|
2016-09-17 21:42:34 +03:00
|
|
|
|
2017-05-21 14:59:16 +03:00
|
|
|
return result
|
2016-09-17 21:42:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# Load the settings and initialize the client
|
|
|
|
settings = load_settings()
|
2017-06-17 01:17:51 +03:00
|
|
|
kwargs = {}
|
|
|
|
if settings.get('socks_proxy'):
|
|
|
|
import socks # $ pip install pysocks
|
|
|
|
host, port = settings['socks_proxy'].split(':')
|
|
|
|
kwargs = dict(proxy=(socks.SOCKS5, host, int(port)))
|
|
|
|
|
2016-09-17 21:42:34 +03:00
|
|
|
client = InteractiveTelegramClient(
|
2017-06-03 14:08:25 +03:00
|
|
|
session_user_id=str(settings.get('session_name', 'anonymous')),
|
2016-09-17 21:42:34 +03:00
|
|
|
user_phone=str(settings['user_phone']),
|
|
|
|
api_id=settings['api_id'],
|
2017-06-17 01:17:51 +03:00
|
|
|
api_hash=str(settings['api_hash']),
|
|
|
|
**kwargs)
|
2016-09-17 21:42:34 +03:00
|
|
|
|
|
|
|
print('Initialization done!')
|
|
|
|
|
|
|
|
try:
|
|
|
|
client.run()
|
|
|
|
|
|
|
|
except Exception as e:
|
2016-11-30 00:29:42 +03:00
|
|
|
print('Unexpected error ({}): {} at\n{}'.format(
|
|
|
|
type(e), e, traceback.format_exc()))
|
2016-09-17 21:42:34 +03:00
|
|
|
|
|
|
|
finally:
|
|
|
|
client.disconnect()
|
2017-06-08 15:00:27 +03:00
|
|
|
print('Thanks for trying the interactive example! Exiting...')
|