mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 12:59:46 +00:00
Tidy examples and update licenses
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
# A simple script to print all updates received
|
||||
# A simple script to print all updates received.
|
||||
# Import modules to access environment, sleep, write to stderr
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Import the client
|
||||
from telethon import TelegramClient
|
||||
|
||||
|
||||
# This is a helper method to access environment variables or
|
||||
# prompt the user to type them in the terminal if missing.
|
||||
def get_env(name, message, cast=str):
|
||||
if name in os.environ:
|
||||
return os.environ[name]
|
||||
@@ -19,28 +23,23 @@ def get_env(name, message, cast=str):
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
client = TelegramClient(
|
||||
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: '),
|
||||
proxy=None
|
||||
)
|
||||
# Define some variables so the code reads easier
|
||||
session = os.environ.get('TG_SESSION', 'printer')
|
||||
api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
|
||||
api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
|
||||
proxy = None # https://github.com/Anorov/PySocks
|
||||
|
||||
|
||||
async def update_handler(update):
|
||||
# This is our update handler. It is called when a new update arrives.
|
||||
async def handler(update):
|
||||
print(update)
|
||||
|
||||
|
||||
client.add_event_handler(update_handler)
|
||||
# Use the client in a `with` block. It calls `start/disconnect` automatically.
|
||||
with TelegramClient(session, api_id, api_hash, proxy=proxy) as client:
|
||||
# Register the update handler so that it gets called
|
||||
client.add_event_handler(handler)
|
||||
|
||||
'''You could also have used the @client.on(...) syntax:
|
||||
from telethon import events
|
||||
|
||||
@client.on(events.Raw)
|
||||
async def update_handler(update):
|
||||
print(update)
|
||||
'''
|
||||
|
||||
with client.start():
|
||||
# Run the client until Ctrl+C is pressed, or the client disconnects
|
||||
print('(Press Ctrl+C to stop this)')
|
||||
client.run_until_disconnected()
|
||||
|
Reference in New Issue
Block a user