2017-10-24 22:32:31 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# A simple script to print all updates received
|
2018-09-04 12:52:18 +03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
2018-02-16 23:02:47 +03:00
|
|
|
|
2017-10-26 19:03:24 +03:00
|
|
|
from telethon import TelegramClient
|
2018-02-16 23:02:47 +03:00
|
|
|
|
2017-10-24 22:32:31 +03:00
|
|
|
|
2018-09-04 12:52:18 +03:00
|
|
|
def get_env(name, message, cast=str):
|
|
|
|
if name in os.environ:
|
|
|
|
return os.environ[name]
|
|
|
|
while True:
|
|
|
|
value = input(message)
|
|
|
|
try:
|
|
|
|
return cast(value)
|
|
|
|
except ValueError as e:
|
|
|
|
print(e, file=sys.stderr)
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
2018-06-27 11:03:26 +03:00
|
|
|
client = TelegramClient(
|
2018-09-04 12:52:18 +03:00
|
|
|
os.environ.get('TG_SESSION', 'printer'),
|
|
|
|
get_env('TG_API_ID', 'Enter your API ID: ', int),
|
|
|
|
get_env('TG_API_HASH', 'Enter your API hash: '),
|
2018-06-27 11:03:26 +03:00
|
|
|
proxy=None
|
|
|
|
)
|
2018-02-16 23:02:47 +03:00
|
|
|
|
2017-10-24 22:32:31 +03:00
|
|
|
|
2018-06-27 11:03:26 +03:00
|
|
|
async def update_handler(update):
|
|
|
|
print(update)
|
2018-02-16 23:02:47 +03:00
|
|
|
|
2017-10-24 22:32:31 +03:00
|
|
|
|
2018-06-27 11:03:26 +03:00
|
|
|
client.add_event_handler(update_handler)
|
|
|
|
|
|
|
|
'''You could also have used the @client.on(...) syntax:
|
|
|
|
from telethon import events
|
|
|
|
|
|
|
|
@client.on(events.Raw)
|
2018-06-24 13:04:23 +03:00
|
|
|
async def update_handler(update):
|
2017-10-26 19:03:24 +03:00
|
|
|
print(update)
|
2018-06-27 11:03:26 +03:00
|
|
|
'''
|
2018-02-16 23:02:47 +03:00
|
|
|
|
2018-06-27 11:03:26 +03:00
|
|
|
with client.start():
|
|
|
|
print('(Press Ctrl+C to stop this)')
|
|
|
|
client.run_until_disconnected()
|