Telethon/telethon_examples/print_updates.py

41 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# A simple script to print all updates received
2018-05-24 11:58:42 +03:00
#
# NOTE: To run this script you MUST have 'TG_API_ID' and 'TG_API_HASH' in
# your environment variables. This is a good way to use these private
# values. See https://superuser.com/q/284342.
2018-06-24 13:04:23 +03:00
import asyncio
from os import environ
2018-02-16 23:02:47 +03:00
# environ is used to get API information from environment variables
# You could also use a config file, pass them as arguments,
# or even hardcode them (not recommended)
from telethon import TelegramClient
2018-02-16 23:02:47 +03:00
2018-06-24 13:04:23 +03:00
async def main():
session_name = environ.get('TG_SESSION', 'session')
client = TelegramClient(session_name,
int(environ['TG_API_ID']),
environ['TG_API_HASH'],
2018-06-24 13:04:23 +03:00
proxy=None)
2018-02-16 23:02:47 +03:00
if 'TG_PHONE' in environ:
2018-06-24 13:04:23 +03:00
await client.start(phone=environ['TG_PHONE'])
2018-02-16 23:02:47 +03:00
else:
2018-06-24 13:04:23 +03:00
await client.start()
2018-05-24 11:58:42 +03:00
client.add_event_handler(update_handler)
2018-02-16 23:02:47 +03:00
print('(Press Ctrl+C to stop this)')
2018-06-24 13:04:23 +03:00
await client.disconnected
2018-02-16 23:02:47 +03:00
2018-06-24 13:04:23 +03:00
async def update_handler(update):
print(update)
2018-02-16 23:02:47 +03:00
if __name__ == '__main__':
2018-06-24 13:04:23 +03:00
loop = asyncio.get_event_loop()
loop.run_until_complete(main())