2018-09-04 12:52:18 +03:00
|
|
|
import asyncio
|
2017-08-23 01:55:49 +03:00
|
|
|
import os
|
2018-06-24 13:04:23 +03:00
|
|
|
import sys
|
2018-09-04 12:52:18 +03:00
|
|
|
import time
|
2016-11-26 14:04:02 +03:00
|
|
|
from getpass import getpass
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2018-05-24 11:58:42 +03:00
|
|
|
from telethon import TelegramClient, events
|
2017-06-10 12:47:51 +03:00
|
|
|
from telethon.errors import SessionPasswordNeededError
|
2018-09-04 12:52:18 +03:00
|
|
|
from telethon.network import ConnectionTcpAbridged
|
|
|
|
from telethon.utils import get_display_name
|
2018-06-24 13:04:23 +03:00
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-05-29 18:07:09 +03:00
|
|
|
def sprint(string, *args, **kwargs):
|
|
|
|
"""Safe Print (handle UnicodeEncodeErrors on some terminals)"""
|
|
|
|
try:
|
|
|
|
print(string, *args, **kwargs)
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
string = string.encode('utf-8', errors='ignore')\
|
|
|
|
.decode('ascii', errors='ignore')
|
|
|
|
print(string, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
def print_title(title):
|
2017-10-21 17:21:58 +03:00
|
|
|
"""Helper function to print titles to the console more nicely"""
|
|
|
|
sprint('\n')
|
|
|
|
sprint('=={}=='.format('=' * len(title)))
|
2017-05-29 18:07:09 +03:00
|
|
|
sprint('= {} ='.format(title))
|
2017-10-21 17:21:58 +03:00
|
|
|
sprint('=={}=='.format('=' * len(title)))
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
|
2016-09-17 18:04:30 +03:00
|
|
|
def bytes_to_string(byte_count):
|
|
|
|
"""Converts a byte count to a string (in KB, MB...)"""
|
|
|
|
suffix_index = 0
|
|
|
|
while byte_count >= 1024:
|
|
|
|
byte_count /= 1024
|
|
|
|
suffix_index += 1
|
|
|
|
|
2017-10-21 17:21:58 +03:00
|
|
|
return '{:.2f}{}'.format(
|
|
|
|
byte_count, [' bytes', 'KB', 'MB', 'GB', 'TB'][suffix_index]
|
|
|
|
)
|
2016-09-17 18:04:30 +03:00
|
|
|
|
|
|
|
|
2018-06-24 13:04:23 +03:00
|
|
|
async def async_input(prompt):
|
|
|
|
"""
|
|
|
|
Python's ``input()`` is blocking, which means the event loop we set
|
|
|
|
above can't be running while we're blocking there. This method will
|
|
|
|
let the loop run while we wait for input.
|
|
|
|
"""
|
|
|
|
print(prompt, end='', flush=True)
|
2023-01-11 23:02:29 +03:00
|
|
|
return (await asyncio.get_running_loop().run_in_executor(None, sys.stdin.readline)).rstrip()
|
2018-06-24 13:04:23 +03:00
|
|
|
|
|
|
|
|
2018-09-04 12:52:18 +03:00
|
|
|
def get_env(name, message, cast=str):
|
|
|
|
"""Helper to get environment variables interactively"""
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
class InteractiveTelegramClient(TelegramClient):
|
2017-06-08 15:00:27 +03:00
|
|
|
"""Full featured Telegram client, meant to be used on an interactive
|
|
|
|
session to see what Telethon is capable off -
|
|
|
|
|
|
|
|
This client allows the user to perform some basic interaction with
|
|
|
|
Telegram through Telethon, such as listing dialogs (open chats),
|
|
|
|
talking to people, downloading media, and receiving updates.
|
|
|
|
"""
|
2018-02-23 23:34:15 +03:00
|
|
|
|
2018-09-04 12:52:18 +03:00
|
|
|
def __init__(self, session_user_id, api_id, api_hash,
|
2017-06-16 10:34:09 +03:00
|
|
|
proxy=None):
|
2017-10-21 17:21:58 +03:00
|
|
|
"""
|
|
|
|
Initializes the InteractiveTelegramClient.
|
|
|
|
:param session_user_id: Name of the *.session file.
|
|
|
|
:param api_id: Telegram's api_id acquired through my.telegram.org.
|
|
|
|
:param api_hash: Telegram's api_hash.
|
|
|
|
:param proxy: Optional proxy tuple/dictionary.
|
|
|
|
"""
|
2016-09-12 15:07:45 +03:00
|
|
|
print_title('Initialization')
|
|
|
|
|
|
|
|
print('Initializing interactive example...')
|
2017-10-21 17:21:58 +03:00
|
|
|
|
|
|
|
# The first step is to initialize the TelegramClient, as we are
|
|
|
|
# subclassing it, we need to call super().__init__(). On a more
|
|
|
|
# normal case you would want 'client = TelegramClient(...)'
|
2017-09-04 12:24:10 +03:00
|
|
|
super().__init__(
|
2017-10-21 17:21:58 +03:00
|
|
|
# These parameters should be passed always, session name and API
|
2017-09-04 12:24:10 +03:00
|
|
|
session_user_id, api_id, api_hash,
|
2017-10-21 17:21:58 +03:00
|
|
|
|
2018-05-24 11:58:42 +03:00
|
|
|
# You can optionally change the connection mode by passing a
|
|
|
|
# type or an instance of it. This changes how the sent packets
|
|
|
|
# look (low-level concept you normally shouldn't worry about).
|
|
|
|
# Default is ConnectionTcpFull, smallest is ConnectionTcpAbridged.
|
|
|
|
connection=ConnectionTcpAbridged,
|
2017-10-21 17:21:58 +03:00
|
|
|
|
|
|
|
# If you're using a proxy, set it here.
|
2018-06-24 13:04:23 +03:00
|
|
|
proxy=proxy
|
2017-09-04 12:24:10 +03:00
|
|
|
)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2018-01-16 20:36:50 +03:00
|
|
|
# Store {message.id: message} map here so that we can download
|
|
|
|
# media known the message ID, for every message having media.
|
|
|
|
self.found_media = {}
|
2016-09-12 16:39:23 +03:00
|
|
|
|
2023-01-11 23:02:29 +03:00
|
|
|
async def init(self):
|
2018-06-24 13:04:23 +03:00
|
|
|
# Calling .connect() may raise a connection error False, so you need
|
|
|
|
# to except those before continuing. Otherwise you may want to retry
|
|
|
|
# as done here.
|
2016-09-12 15:07:45 +03:00
|
|
|
print('Connecting to Telegram servers...')
|
2018-06-24 13:04:23 +03:00
|
|
|
try:
|
2023-01-11 23:02:29 +03:00
|
|
|
await self.connect()
|
2019-03-28 11:52:46 +03:00
|
|
|
except IOError:
|
|
|
|
# We handle IOError and not ConnectionError because
|
|
|
|
# PySocks' errors do not subclass ConnectionError
|
|
|
|
# (so this will work with and without proxies).
|
2017-05-30 14:27:23 +03:00
|
|
|
print('Initial connection failed. Retrying...')
|
2023-01-11 23:02:29 +03:00
|
|
|
await self.connect()
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2023-02-26 12:39:10 +03:00
|
|
|
# If the user hasn't called .sign_in() yet, they won't
|
2017-10-21 17:21:58 +03:00
|
|
|
# be authorized. The first thing you must do is authorize. Calling
|
|
|
|
# .sign_in() should only be done once as the information is saved on
|
|
|
|
# the *.session file so you don't need to enter the code every time.
|
2023-01-11 23:02:29 +03:00
|
|
|
if not await self.is_user_authorized():
|
2016-09-12 15:07:45 +03:00
|
|
|
print('First run. Sending code request...')
|
2018-09-04 12:52:18 +03:00
|
|
|
user_phone = input('Enter your phone: ')
|
2023-01-11 23:02:29 +03:00
|
|
|
await self.sign_in(user_phone)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
self_user = None
|
|
|
|
while self_user is None:
|
2016-09-12 20:32:16 +03:00
|
|
|
code = input('Enter the code you just received: ')
|
2016-11-26 14:04:02 +03:00
|
|
|
try:
|
2023-01-11 23:02:29 +03:00
|
|
|
self_user = await self.sign_in(code=code)
|
2016-11-26 14:04:02 +03:00
|
|
|
|
2017-10-21 17:21:58 +03:00
|
|
|
# Two-step verification may be enabled, and .sign_in will
|
|
|
|
# raise this error. If that's the case ask for the password.
|
|
|
|
# Note that getpass() may not work on PyCharm due to a bug,
|
|
|
|
# if that's the case simply change it for input().
|
2017-06-16 10:34:09 +03:00
|
|
|
except SessionPasswordNeededError:
|
2017-06-10 12:47:51 +03:00
|
|
|
pw = getpass('Two step verification is enabled. '
|
|
|
|
'Please enter your password: ')
|
|
|
|
|
2023-01-11 23:02:29 +03:00
|
|
|
self_user = await self.sign_in(password=pw)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2018-06-24 13:04:23 +03:00
|
|
|
async def run(self):
|
2017-10-21 17:21:58 +03:00
|
|
|
"""Main loop of the TelegramClient, will wait for user action"""
|
|
|
|
|
2018-05-24 11:58:42 +03:00
|
|
|
# Once everything is ready, we can add an event handler.
|
|
|
|
#
|
|
|
|
# Events are an abstraction over Telegram's "Updates" and
|
|
|
|
# are much easier to use.
|
|
|
|
self.add_event_handler(self.message_handler, events.NewMessage)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# Enter a while loop to chat as long as the user wants
|
|
|
|
while True:
|
2017-10-21 17:21:58 +03:00
|
|
|
# Retrieve the top dialogs. You can set the limit to None to
|
|
|
|
# retrieve all of them if you wish, but beware that may take
|
|
|
|
# a long time if you have hundreds of them.
|
2017-10-02 19:59:29 +03:00
|
|
|
dialog_count = 15
|
2016-10-03 20:44:01 +03:00
|
|
|
|
|
|
|
# Entities represent the user, chat or channel
|
2017-10-21 17:21:58 +03:00
|
|
|
# corresponding to the dialog on the same index.
|
2018-06-24 13:04:23 +03:00
|
|
|
dialogs = await self.get_dialogs(limit=dialog_count)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
i = None
|
|
|
|
while i is None:
|
2017-05-07 14:33:35 +03:00
|
|
|
print_title('Dialogs window')
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-05-07 14:33:35 +03:00
|
|
|
# Display them so the user can choose
|
2018-01-02 11:56:37 +03:00
|
|
|
for i, dialog in enumerate(dialogs, start=1):
|
|
|
|
sprint('{}. {}'.format(i, get_display_name(dialog.entity)))
|
2017-05-07 14:33:35 +03:00
|
|
|
|
|
|
|
# Let the user decide who they want to talk to
|
|
|
|
print()
|
|
|
|
print('> Who do you want to send messages to?')
|
|
|
|
print('> Available commands:')
|
|
|
|
print(' !q: Quits the dialogs window and exits.')
|
|
|
|
print(' !l: Logs out, terminating this session.')
|
|
|
|
print()
|
2018-06-24 13:04:23 +03:00
|
|
|
i = await async_input('Enter dialog ID or a command: ')
|
2017-05-07 14:33:35 +03:00
|
|
|
if i == '!q':
|
|
|
|
return
|
|
|
|
if i == '!l':
|
2017-10-21 17:21:58 +03:00
|
|
|
# Logging out will cause the user to need to reenter the
|
|
|
|
# code next time they want to use the library, and will
|
|
|
|
# also delete the *.session file off the filesystem.
|
|
|
|
#
|
|
|
|
# This is not the same as simply calling .disconnect(),
|
|
|
|
# which simply shuts down everything gracefully.
|
2018-06-24 13:04:23 +03:00
|
|
|
await self.log_out()
|
2017-05-07 14:33:35 +03:00
|
|
|
return
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-05-07 14:33:35 +03:00
|
|
|
try:
|
2016-09-12 15:07:45 +03:00
|
|
|
i = int(i if i else 0) - 1
|
2017-06-16 10:34:09 +03:00
|
|
|
# Ensure it is inside the bounds, otherwise retry
|
2016-09-12 15:07:45 +03:00
|
|
|
if not 0 <= i < dialog_count:
|
|
|
|
i = None
|
|
|
|
except ValueError:
|
2017-01-17 22:22:47 +03:00
|
|
|
i = None
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2016-10-03 20:44:01 +03:00
|
|
|
# Retrieve the selected user (or chat, or channel)
|
2018-01-02 11:56:37 +03:00
|
|
|
entity = dialogs[i].entity
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# Show some information
|
2016-10-09 13:57:38 +03:00
|
|
|
print_title('Chat with "{}"'.format(get_display_name(entity)))
|
2016-09-16 14:35:14 +03:00
|
|
|
print('Available commands:')
|
2018-02-23 23:34:15 +03:00
|
|
|
print(' !q: Quits the current chat.')
|
|
|
|
print(' !Q: Quits the current chat and exits.')
|
|
|
|
print(' !h: prints the latest messages (message History).')
|
|
|
|
print(' !up <path>: Uploads and sends the Photo from path.')
|
|
|
|
print(' !uf <path>: Uploads and sends the File from path.')
|
|
|
|
print(' !d <msg-id>: Deletes a message by its id')
|
|
|
|
print(' !dm <msg-id>: Downloads the given message Media (if any).')
|
2016-10-03 20:44:01 +03:00
|
|
|
print(' !dp: Downloads the current dialog Profile picture.')
|
2018-02-23 23:34:15 +03:00
|
|
|
print(' !i: Prints information about this chat..')
|
2016-09-16 14:35:14 +03:00
|
|
|
print()
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# And start a while loop to chat
|
|
|
|
while True:
|
2018-06-24 13:04:23 +03:00
|
|
|
msg = await async_input('Enter a message: ')
|
2016-09-12 15:07:45 +03:00
|
|
|
# Quit
|
|
|
|
if msg == '!q':
|
|
|
|
break
|
2016-09-12 20:32:16 +03:00
|
|
|
elif msg == '!Q':
|
|
|
|
return
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# History
|
|
|
|
elif msg == '!h':
|
|
|
|
# First retrieve the messages and some information
|
2018-06-24 13:04:23 +03:00
|
|
|
messages = await self.get_messages(entity, limit=10)
|
2017-06-08 15:00:27 +03:00
|
|
|
|
|
|
|
# Iterate over all (in reverse order so the latest appear
|
|
|
|
# the last in the console) and print them with format:
|
|
|
|
# "[hh:mm] Sender: Message"
|
2018-01-16 20:36:50 +03:00
|
|
|
for msg in reversed(messages):
|
2018-06-24 13:04:23 +03:00
|
|
|
# Note how we access .sender here. Since we made an
|
|
|
|
# API call using the self client, it will always have
|
|
|
|
# information about the sender. This is different to
|
|
|
|
# events, where Telegram may not always send the user.
|
2018-01-16 20:36:50 +03:00
|
|
|
name = get_display_name(msg.sender)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# Format the message content
|
2017-04-17 10:36:24 +03:00
|
|
|
if getattr(msg, 'media', None):
|
2018-01-16 20:36:50 +03:00
|
|
|
self.found_media[msg.id] = msg
|
2017-06-08 15:00:27 +03:00
|
|
|
content = '<{}> {}'.format(
|
2018-03-02 23:28:33 +03:00
|
|
|
type(msg.media).__name__, msg.message)
|
2017-06-08 15:00:27 +03:00
|
|
|
|
2017-04-17 10:36:24 +03:00
|
|
|
elif hasattr(msg, 'message'):
|
2016-09-12 15:07:45 +03:00
|
|
|
content = msg.message
|
2017-04-17 10:36:24 +03:00
|
|
|
elif hasattr(msg, 'action'):
|
|
|
|
content = str(msg.action)
|
|
|
|
else:
|
|
|
|
# Unknown message, simply print its class name
|
2017-06-08 15:00:27 +03:00
|
|
|
content = type(msg).__name__
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# And print it to the user
|
2017-05-29 18:07:09 +03:00
|
|
|
sprint('[{}:{}] (ID={}) {}: {}'.format(
|
2018-02-23 23:34:15 +03:00
|
|
|
msg.date.hour, msg.date.minute, msg.id, name, content))
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# Send photo
|
2016-10-03 20:44:01 +03:00
|
|
|
elif msg.startswith('!up '):
|
2016-09-12 20:32:16 +03:00
|
|
|
# Slice the message to get the path
|
2018-06-24 13:04:23 +03:00
|
|
|
path = msg[len('!up '):]
|
|
|
|
await self.send_photo(path=path, entity=entity)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2016-09-12 20:32:16 +03:00
|
|
|
# Send file (document)
|
2016-10-03 20:44:01 +03:00
|
|
|
elif msg.startswith('!uf '):
|
2016-09-12 20:32:16 +03:00
|
|
|
# Slice the message to get the path
|
2018-06-24 13:04:23 +03:00
|
|
|
path = msg[len('!uf '):]
|
|
|
|
await self.send_document(path=path, entity=entity)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-10-02 19:59:29 +03:00
|
|
|
# Delete messages
|
|
|
|
elif msg.startswith('!d '):
|
|
|
|
# Slice the message to get message ID
|
2018-06-24 13:04:23 +03:00
|
|
|
msg = msg[len('!d '):]
|
|
|
|
deleted_msg = await self.delete_messages(entity, msg)
|
2018-01-16 20:36:50 +03:00
|
|
|
print('Deleted {}'.format(deleted_msg))
|
2017-10-02 19:59:29 +03:00
|
|
|
|
2016-09-12 16:39:23 +03:00
|
|
|
# Download media
|
2016-10-03 20:44:01 +03:00
|
|
|
elif msg.startswith('!dm '):
|
2016-09-12 20:32:16 +03:00
|
|
|
# Slice the message to get message ID
|
2018-06-24 13:04:23 +03:00
|
|
|
await self.download_media_by_id(msg[len('!dm '):])
|
2016-09-12 16:39:23 +03:00
|
|
|
|
2016-10-03 20:44:01 +03:00
|
|
|
# Download profile photo
|
|
|
|
elif msg == '!dp':
|
2017-08-23 02:35:12 +03:00
|
|
|
print('Downloading profile picture to usermedia/...')
|
|
|
|
os.makedirs('usermedia', exist_ok=True)
|
2018-06-24 13:04:23 +03:00
|
|
|
output = await self.download_profile_photo(entity,
|
|
|
|
'usermedia')
|
2017-08-23 02:35:12 +03:00
|
|
|
if output:
|
2018-02-23 23:34:15 +03:00
|
|
|
print('Profile picture downloaded to', output)
|
2016-10-03 20:44:01 +03:00
|
|
|
else:
|
2018-01-16 20:36:50 +03:00
|
|
|
print('No profile picture found for this user!')
|
2016-10-03 20:44:01 +03:00
|
|
|
|
2018-02-23 23:34:15 +03:00
|
|
|
elif msg == '!i':
|
|
|
|
attributes = list(entity.to_dict().items())
|
|
|
|
pad = max(len(x) for x, _ in attributes)
|
|
|
|
for name, val in attributes:
|
|
|
|
print("{:<{width}} : {}".format(name, val, width=pad))
|
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
# Send chat message (if any)
|
|
|
|
elif msg:
|
2018-06-24 13:04:23 +03:00
|
|
|
await self.send_message(entity, msg, link_preview=False)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2018-06-24 13:04:23 +03:00
|
|
|
async def send_photo(self, path, entity):
|
2017-10-21 17:21:58 +03:00
|
|
|
"""Sends the file located at path to the desired entity as a photo"""
|
2018-06-24 13:04:23 +03:00
|
|
|
await self.send_file(
|
2017-08-23 01:55:49 +03:00
|
|
|
entity, path,
|
|
|
|
progress_callback=self.upload_progress_callback
|
|
|
|
)
|
2016-09-12 20:32:16 +03:00
|
|
|
print('Photo sent!')
|
|
|
|
|
2018-06-24 13:04:23 +03:00
|
|
|
async def send_document(self, path, entity):
|
2017-10-21 17:21:58 +03:00
|
|
|
"""Sends the file located at path to the desired entity as a document"""
|
2018-06-24 13:04:23 +03:00
|
|
|
await self.send_file(
|
2017-08-23 01:55:49 +03:00
|
|
|
entity, path,
|
|
|
|
force_document=True,
|
|
|
|
progress_callback=self.upload_progress_callback
|
|
|
|
)
|
2016-09-12 20:32:16 +03:00
|
|
|
print('Document sent!')
|
|
|
|
|
2018-06-24 13:04:23 +03:00
|
|
|
async def download_media_by_id(self, media_id):
|
2017-10-21 17:21:58 +03:00
|
|
|
"""Given a message ID, finds the media this message contained and
|
|
|
|
downloads it.
|
|
|
|
"""
|
2016-09-12 20:32:16 +03:00
|
|
|
try:
|
2018-01-16 20:36:50 +03:00
|
|
|
msg = self.found_media[int(media_id)]
|
|
|
|
except (ValueError, KeyError):
|
|
|
|
# ValueError when parsing, KeyError when accessing dictionary
|
|
|
|
print('Invalid media ID given or message not found!')
|
|
|
|
return
|
|
|
|
|
|
|
|
print('Downloading media to usermedia/...')
|
|
|
|
os.makedirs('usermedia', exist_ok=True)
|
2018-06-24 13:04:23 +03:00
|
|
|
output = await self.download_media(
|
2018-01-16 20:36:50 +03:00
|
|
|
msg.media,
|
|
|
|
file='usermedia/',
|
|
|
|
progress_callback=self.download_progress_callback
|
|
|
|
)
|
|
|
|
print('Media downloaded to {}!'.format(output))
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2016-09-17 18:04:30 +03:00
|
|
|
@staticmethod
|
|
|
|
def download_progress_callback(downloaded_bytes, total_bytes):
|
2017-10-01 21:12:27 +03:00
|
|
|
InteractiveTelegramClient.print_progress(
|
|
|
|
'Downloaded', downloaded_bytes, total_bytes
|
|
|
|
)
|
2016-09-17 18:04:30 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def upload_progress_callback(uploaded_bytes, total_bytes):
|
2017-10-01 21:12:27 +03:00
|
|
|
InteractiveTelegramClient.print_progress(
|
|
|
|
'Uploaded', uploaded_bytes, total_bytes
|
|
|
|
)
|
2016-09-17 18:04:30 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def print_progress(progress_type, downloaded_bytes, total_bytes):
|
2017-10-01 21:12:27 +03:00
|
|
|
print('{} {} out of {} ({:.2%})'.format(
|
|
|
|
progress_type, bytes_to_string(downloaded_bytes),
|
|
|
|
bytes_to_string(total_bytes), downloaded_bytes / total_bytes)
|
|
|
|
)
|
2016-09-17 18:04:30 +03:00
|
|
|
|
2018-06-24 13:04:23 +03:00
|
|
|
async def message_handler(self, event):
|
2018-05-24 11:58:42 +03:00
|
|
|
"""Callback method for received events.NewMessage"""
|
2017-10-21 17:21:58 +03:00
|
|
|
|
2018-06-24 13:04:23 +03:00
|
|
|
# Note that message_handler is called when a Telegram update occurs
|
|
|
|
# and an event is created. Telegram may not always send information
|
|
|
|
# about the ``.sender`` or the ``.chat``, so if you *really* want it
|
|
|
|
# you should use ``get_chat()`` and ``get_sender()`` while working
|
|
|
|
# with events. Since they are methods, you know they may make an API
|
|
|
|
# call, which can be expensive.
|
|
|
|
chat = await event.get_chat()
|
2018-05-24 11:58:42 +03:00
|
|
|
if event.is_group:
|
|
|
|
if event.out:
|
|
|
|
sprint('>> sent "{}" to chat {}'.format(
|
2018-06-24 13:04:23 +03:00
|
|
|
event.text, get_display_name(chat)
|
2017-10-01 21:12:27 +03:00
|
|
|
))
|
2017-05-22 00:57:13 +03:00
|
|
|
else:
|
2018-05-24 11:58:42 +03:00
|
|
|
sprint('<< {} @ {} sent "{}"'.format(
|
2018-06-24 13:04:23 +03:00
|
|
|
get_display_name(await event.get_sender()),
|
|
|
|
get_display_name(chat),
|
2018-05-24 11:58:42 +03:00
|
|
|
event.text
|
2017-10-01 21:12:27 +03:00
|
|
|
))
|
2018-05-24 11:58:42 +03:00
|
|
|
else:
|
|
|
|
if event.out:
|
|
|
|
sprint('>> "{}" to user {}'.format(
|
2018-06-24 13:04:23 +03:00
|
|
|
event.text, get_display_name(chat)
|
2017-10-01 21:12:27 +03:00
|
|
|
))
|
2017-05-22 00:57:13 +03:00
|
|
|
else:
|
2018-05-24 11:58:42 +03:00
|
|
|
sprint('<< {} sent "{}"'.format(
|
2018-06-24 13:04:23 +03:00
|
|
|
get_display_name(chat), event.text
|
2017-10-01 21:12:27 +03:00
|
|
|
))
|
2018-09-04 12:52:18 +03:00
|
|
|
|
|
|
|
|
2023-01-11 23:02:29 +03:00
|
|
|
async def main():
|
2018-09-04 12:52:18 +03:00
|
|
|
SESSION = os.environ.get('TG_SESSION', 'interactive')
|
|
|
|
API_ID = get_env('TG_API_ID', 'Enter your API ID: ', int)
|
|
|
|
API_HASH = get_env('TG_API_HASH', 'Enter your API hash: ')
|
|
|
|
client = InteractiveTelegramClient(SESSION, API_ID, API_HASH)
|
2023-01-11 23:02:29 +03:00
|
|
|
await client.init()
|
|
|
|
await client.run()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
asyncio.run()
|