2017-08-23 01:27:33 +03:00
|
|
|
import os
|
2017-08-23 01:48:00 +03:00
|
|
|
from datetime import datetime, timedelta
|
2016-10-09 13:57:38 +03:00
|
|
|
from mimetypes import guess_type
|
2017-09-25 12:04:09 +03:00
|
|
|
|
2017-09-20 14:22:56 +03:00
|
|
|
try:
|
|
|
|
import socks
|
|
|
|
except ImportError:
|
|
|
|
socks = None
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
from . import TelegramBareClient
|
2017-10-01 17:57:07 +03:00
|
|
|
from . import helpers, utils
|
2017-09-04 18:18:33 +03:00
|
|
|
from .errors import (
|
|
|
|
RPCError, UnauthorizedError, InvalidParameterError, PhoneCodeEmptyError,
|
|
|
|
PhoneCodeExpiredError, PhoneCodeHashEmptyError, PhoneCodeInvalidError
|
|
|
|
)
|
2017-09-29 21:50:27 +03:00
|
|
|
from .network import ConnectionMode
|
|
|
|
from .tl import TLObject
|
2017-10-05 14:01:00 +03:00
|
|
|
from .tl.entity_database import EntityDatabase
|
2017-09-04 18:18:33 +03:00
|
|
|
from .tl.functions.account import (
|
|
|
|
GetPasswordRequest
|
|
|
|
)
|
|
|
|
from .tl.functions.auth import (
|
|
|
|
CheckPasswordRequest, LogOutRequest, SendCodeRequest, SignInRequest,
|
|
|
|
SignUpRequest, ImportBotAuthorizationRequest
|
|
|
|
)
|
|
|
|
from .tl.functions.contacts import (
|
|
|
|
GetContactsRequest, ResolveUsernameRequest
|
|
|
|
)
|
2017-05-21 14:02:54 +03:00
|
|
|
from .tl.functions.messages import (
|
2016-11-30 00:29:42 +03:00
|
|
|
GetDialogsRequest, GetHistoryRequest, ReadHistoryRequest, SendMediaRequest,
|
2017-10-01 15:19:04 +03:00
|
|
|
SendMessageRequest, GetChatsRequest
|
2017-09-04 18:18:33 +03:00
|
|
|
)
|
2017-10-02 19:59:29 +03:00
|
|
|
|
|
|
|
from .tl.functions import channels
|
|
|
|
from .tl.functions import messages
|
|
|
|
|
2017-09-04 18:18:33 +03:00
|
|
|
from .tl.functions.users import (
|
|
|
|
GetUsersRequest
|
|
|
|
)
|
2017-10-01 15:19:04 +03:00
|
|
|
from .tl.functions.channels import (
|
|
|
|
GetChannelsRequest
|
|
|
|
)
|
2017-05-21 14:02:54 +03:00
|
|
|
from .tl.types import (
|
2017-08-23 02:35:12 +03:00
|
|
|
DocumentAttributeAudio, DocumentAttributeFilename,
|
2017-06-08 14:12:57 +03:00
|
|
|
InputDocumentFileLocation, InputFileLocation,
|
2016-11-30 00:29:42 +03:00
|
|
|
InputMediaUploadedDocument, InputMediaUploadedPhoto, InputPeerEmpty,
|
2017-08-23 01:48:00 +03:00
|
|
|
Message, MessageMediaContact, MessageMediaDocument, MessageMediaPhoto,
|
2017-09-13 13:00:27 +03:00
|
|
|
InputUserSelf, UserProfilePhoto, ChatPhoto, UpdateMessageID,
|
2017-10-01 15:19:04 +03:00
|
|
|
UpdateNewMessage, UpdateShortSentMessage,
|
|
|
|
PeerUser, InputPeerUser, InputPeerChat, InputPeerChannel)
|
2017-10-01 11:50:37 +03:00
|
|
|
from .tl.types.messages import DialogsSlice
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2016-09-04 12:07:18 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
class TelegramClient(TelegramBareClient):
|
|
|
|
"""Full featured TelegramClient meant to extend the basic functionality -
|
2016-09-04 12:07:18 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
As opposed to the TelegramBareClient, this one features downloading
|
|
|
|
media from different data centers, starting a second thread to
|
|
|
|
handle updates, and some very common functionality.
|
|
|
|
"""
|
2016-09-18 12:59:12 +03:00
|
|
|
|
2017-05-08 17:01:53 +03:00
|
|
|
# region Initialization
|
2016-09-07 12:36:34 +03:00
|
|
|
|
2017-09-04 12:24:10 +03:00
|
|
|
def __init__(self, session, api_id, api_hash,
|
|
|
|
connection_mode=ConnectionMode.TCP_FULL,
|
|
|
|
proxy=None,
|
2017-09-30 12:17:31 +03:00
|
|
|
update_workers=None,
|
2017-08-29 17:06:14 +03:00
|
|
|
timeout=timedelta(seconds=5),
|
2017-09-30 12:28:15 +03:00
|
|
|
spawn_read_thread=True,
|
2017-08-29 17:06:14 +03:00
|
|
|
**kwargs):
|
2016-11-30 17:36:59 +03:00
|
|
|
"""Initializes the Telegram client with the specified API ID and Hash.
|
|
|
|
|
2017-06-08 17:23:05 +03:00
|
|
|
Session can either be a `str` object (filename for the .session)
|
|
|
|
or it can be a `Session` instance (in which case list_sessions()
|
|
|
|
would probably not work). Pass 'None' for it to be a temporary
|
|
|
|
session - remember to '.log_out()'!
|
2016-11-30 17:36:59 +03:00
|
|
|
|
2017-09-04 12:24:10 +03:00
|
|
|
The 'connection_mode' should be any value under ConnectionMode.
|
|
|
|
This will only affect how messages are sent over the network
|
|
|
|
and how much processing is required before sending them.
|
|
|
|
|
2017-09-30 12:17:31 +03:00
|
|
|
The integer 'update_workers' represents depending on its value:
|
|
|
|
is None: Updates will *not* be stored in memory.
|
|
|
|
= 0: Another thread is responsible for calling self.updates.poll()
|
|
|
|
> 0: 'update_workers' background threads will be spawned, any
|
|
|
|
any of them will invoke all the self.updates.handlers.
|
2017-09-07 21:29:51 +03:00
|
|
|
|
2017-09-30 12:28:15 +03:00
|
|
|
If 'spawn_read_thread', a background thread will be started once
|
|
|
|
an authorized user has been logged in to Telegram to read items
|
|
|
|
(such as updates and responses) from the network as soon as they
|
|
|
|
occur, which will speed things up.
|
|
|
|
|
|
|
|
If you don't want to spawn any additional threads, pending updates
|
|
|
|
will be read and processed accordingly after invoking a request
|
|
|
|
and not immediately. This is useful if you don't care about updates
|
|
|
|
at all and have set 'update_workers=None'.
|
2017-09-07 19:49:08 +03:00
|
|
|
|
2017-08-29 17:06:14 +03:00
|
|
|
If more named arguments are provided as **kwargs, they will be
|
|
|
|
used to update the Session instance. Most common settings are:
|
2017-06-30 12:48:45 +03:00
|
|
|
device_model = platform.node()
|
|
|
|
system_version = platform.system()
|
|
|
|
app_version = TelegramClient.__version__
|
|
|
|
lang_code = 'en'
|
|
|
|
system_lang_code = lang_code
|
2017-08-29 17:06:14 +03:00
|
|
|
report_errors = True
|
2017-06-08 17:23:05 +03:00
|
|
|
"""
|
2017-09-04 12:24:10 +03:00
|
|
|
super().__init__(
|
|
|
|
session, api_id, api_hash,
|
2017-09-07 19:49:08 +03:00
|
|
|
connection_mode=connection_mode,
|
|
|
|
proxy=proxy,
|
2017-09-30 12:17:31 +03:00
|
|
|
update_workers=update_workers,
|
2017-09-30 12:28:15 +03:00
|
|
|
spawn_read_thread=spawn_read_thread,
|
2017-09-30 19:02:08 +03:00
|
|
|
timeout=timeout,
|
|
|
|
**kwargs
|
2017-09-04 12:24:10 +03:00
|
|
|
)
|
2017-06-07 21:08:16 +03:00
|
|
|
|
2017-09-29 21:50:27 +03:00
|
|
|
# Some fields to easy signing in
|
2017-08-31 11:34:09 +03:00
|
|
|
self._phone_code_hash = None
|
|
|
|
self._phone = None
|
2016-09-07 12:36:34 +03:00
|
|
|
|
2017-05-30 13:14:29 +03:00
|
|
|
# endregion
|
|
|
|
|
2016-09-07 12:36:34 +03:00
|
|
|
# region Telegram requests functions
|
2016-09-04 12:07:18 +03:00
|
|
|
|
2016-09-11 17:24:03 +03:00
|
|
|
# region Authorization requests
|
|
|
|
|
2017-08-31 11:34:09 +03:00
|
|
|
def send_code_request(self, phone):
|
2016-09-07 12:36:34 +03:00
|
|
|
"""Sends a code request to the specified phone number"""
|
2017-10-05 14:01:00 +03:00
|
|
|
phone = EntityDatabase.parse_phone(phone) or self._phone
|
2017-09-20 13:47:19 +03:00
|
|
|
result = self(SendCodeRequest(phone, self.api_id, self.api_hash))
|
2017-08-31 11:34:09 +03:00
|
|
|
self._phone = phone
|
|
|
|
self._phone_code_hash = result.phone_code_hash
|
|
|
|
return result
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-08-31 11:34:09 +03:00
|
|
|
def sign_in(self, phone=None, code=None,
|
2017-10-01 12:37:18 +03:00
|
|
|
password=None, bot_token=None, phone_code_hash=None):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Completes the sign in process with the phone number + code pair.
|
2016-09-05 19:35:12 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
If no phone or code is provided, then the sole password will be used.
|
|
|
|
The password should be used after a normal authorization attempt
|
2017-09-18 10:37:20 +03:00
|
|
|
has happened and an SessionPasswordNeededError was raised.
|
2016-09-04 12:07:18 +03:00
|
|
|
|
2017-10-01 12:37:18 +03:00
|
|
|
If you're calling .sign_in() on two completely different clients
|
|
|
|
(for example, through an API that creates a new client per phone),
|
|
|
|
you must first call .sign_in(phone) to receive the code, and then
|
|
|
|
with the result such method results, call
|
|
|
|
.sign_in(phone, code, phone_code_hash=result.phone_code_hash).
|
|
|
|
|
|
|
|
If this is done on the same client, the client will fill said values
|
|
|
|
for you.
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
To login as a bot, only `bot_token` should be provided.
|
|
|
|
This should equal to the bot access hash provided by
|
|
|
|
https://t.me/BotFather during your bot creation.
|
2017-03-20 14:31:13 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
If the login succeeds, the logged in user is returned.
|
|
|
|
"""
|
2017-08-31 11:34:09 +03:00
|
|
|
|
2017-08-31 11:38:53 +03:00
|
|
|
if phone and not code:
|
2017-08-31 11:34:09 +03:00
|
|
|
return self.send_code_request(phone)
|
|
|
|
elif code:
|
2017-10-05 14:01:00 +03:00
|
|
|
phone = EntityDatabase.parse_phone(phone) or self._phone
|
2017-10-01 12:37:18 +03:00
|
|
|
phone_code_hash = phone_code_hash or self._phone_code_hash
|
|
|
|
if not phone:
|
2016-11-30 00:29:42 +03:00
|
|
|
raise ValueError(
|
2017-10-01 12:31:26 +03:00
|
|
|
'Please make sure to call send_code_request first.'
|
|
|
|
)
|
2017-10-01 12:37:18 +03:00
|
|
|
if not phone_code_hash:
|
|
|
|
raise ValueError('You also need to provide a phone_code_hash.')
|
2016-09-16 14:35:14 +03:00
|
|
|
|
2016-11-26 14:04:02 +03:00
|
|
|
try:
|
2017-08-24 19:05:32 +03:00
|
|
|
if isinstance(code, int):
|
|
|
|
code = str(code)
|
2017-10-01 20:02:53 +03:00
|
|
|
|
|
|
|
result = self(SignInRequest(phone, phone_code_hash, code))
|
2016-11-26 14:04:02 +03:00
|
|
|
|
2017-06-10 12:47:51 +03:00
|
|
|
except (PhoneCodeEmptyError, PhoneCodeExpiredError,
|
|
|
|
PhoneCodeHashEmptyError, PhoneCodeInvalidError):
|
|
|
|
return None
|
2016-11-26 14:04:02 +03:00
|
|
|
elif password:
|
2017-07-02 12:56:40 +03:00
|
|
|
salt = self(GetPasswordRequest()).current_salt
|
2017-09-21 16:36:20 +03:00
|
|
|
result = self(CheckPasswordRequest(
|
2017-10-01 17:57:07 +03:00
|
|
|
helpers.get_password_hash(password, salt)
|
2017-09-21 16:36:20 +03:00
|
|
|
))
|
2017-03-20 14:31:13 +03:00
|
|
|
elif bot_token:
|
2017-07-02 12:56:40 +03:00
|
|
|
result = self(ImportBotAuthorizationRequest(
|
2017-06-08 14:12:57 +03:00
|
|
|
flags=0, bot_auth_token=bot_token,
|
2017-09-21 16:36:20 +03:00
|
|
|
api_id=self.api_id, api_hash=self.api_hash
|
|
|
|
))
|
2016-11-26 14:04:02 +03:00
|
|
|
else:
|
2016-11-30 00:29:42 +03:00
|
|
|
raise ValueError(
|
2017-08-31 11:34:09 +03:00
|
|
|
'You must provide a phone and a code the first time, '
|
2017-09-21 16:36:20 +03:00
|
|
|
'and a password only if an RPCError was raised before.'
|
|
|
|
)
|
2016-09-04 12:07:18 +03:00
|
|
|
|
2017-09-21 16:36:20 +03:00
|
|
|
self._set_connected_and_authorized()
|
2017-06-08 14:12:57 +03:00
|
|
|
return result.user
|
2016-09-04 12:07:18 +03:00
|
|
|
|
2017-08-31 11:34:09 +03:00
|
|
|
def sign_up(self, code, first_name, last_name=''):
|
2016-09-16 14:35:14 +03:00
|
|
|
"""Signs up to Telegram. Make sure you sent a code request first!"""
|
2017-09-21 16:36:20 +03:00
|
|
|
result = self(SignUpRequest(
|
2017-09-04 19:07:20 +03:00
|
|
|
phone_number=self._phone,
|
|
|
|
phone_code_hash=self._phone_code_hash,
|
|
|
|
phone_code=code,
|
|
|
|
first_name=first_name,
|
|
|
|
last_name=last_name
|
2017-09-21 16:36:20 +03:00
|
|
|
))
|
|
|
|
|
|
|
|
self._set_connected_and_authorized()
|
|
|
|
return result.user
|
2016-09-16 14:35:14 +03:00
|
|
|
|
|
|
|
def log_out(self):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Logs out and deletes the current session.
|
|
|
|
Returns True if everything went okay."""
|
2016-09-16 14:35:14 +03:00
|
|
|
try:
|
2017-07-02 12:56:40 +03:00
|
|
|
self(LogOutRequest())
|
2017-09-29 14:58:15 +03:00
|
|
|
except RPCError:
|
2016-09-16 14:35:14 +03:00
|
|
|
return False
|
|
|
|
|
2017-09-29 14:58:15 +03:00
|
|
|
self.disconnect()
|
2017-08-21 10:00:23 +03:00
|
|
|
self.session.delete()
|
|
|
|
self.session = None
|
|
|
|
return True
|
|
|
|
|
2017-06-04 18:24:08 +03:00
|
|
|
def get_me(self):
|
|
|
|
"""Gets "me" (the self user) which is currently authenticated,
|
|
|
|
or None if the request fails (hence, not authenticated)."""
|
|
|
|
try:
|
2017-07-02 12:56:40 +03:00
|
|
|
return self(GetUsersRequest([InputUserSelf()]))[0]
|
2017-06-10 12:47:51 +03:00
|
|
|
except UnauthorizedError:
|
|
|
|
return None
|
2017-06-04 18:24:08 +03:00
|
|
|
|
2016-09-11 17:24:03 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Dialogs ("chats") requests
|
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
def get_dialogs(self,
|
2017-05-05 16:11:48 +03:00
|
|
|
limit=10,
|
2016-11-30 00:29:42 +03:00
|
|
|
offset_date=None,
|
|
|
|
offset_id=0,
|
|
|
|
offset_peer=InputPeerEmpty()):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Returns a tuple of lists ([dialogs], [entities])
|
2017-10-01 11:50:37 +03:00
|
|
|
with at least 'limit' items each unless all dialogs were consumed.
|
|
|
|
|
|
|
|
If `limit` is None, all dialogs will be retrieved (from the given
|
|
|
|
offset) will be retrieved.
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
The `entities` represent the user, chat or channel
|
2017-10-01 11:50:37 +03:00
|
|
|
corresponding to that dialog. If it's an integer, not
|
|
|
|
all dialogs may be retrieved at once.
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
2017-10-01 11:50:37 +03:00
|
|
|
if limit is None:
|
|
|
|
limit = float('inf')
|
|
|
|
|
|
|
|
dialogs = {} # Use Dialog.top_message as identifier to avoid dupes
|
|
|
|
messages = {} # Used later for sorting TODO also return these?
|
|
|
|
entities = {}
|
|
|
|
while len(dialogs) < limit:
|
2017-10-05 14:55:58 +03:00
|
|
|
need = limit - len(dialogs)
|
2017-10-01 11:50:37 +03:00
|
|
|
r = self(GetDialogsRequest(
|
2016-11-30 00:29:42 +03:00
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
offset_peer=offset_peer,
|
2017-10-05 14:55:58 +03:00
|
|
|
limit=need if need < float('inf') else 0
|
2017-10-01 11:50:37 +03:00
|
|
|
))
|
|
|
|
if not r.dialogs:
|
|
|
|
break
|
|
|
|
|
|
|
|
for d in r.dialogs:
|
|
|
|
dialogs[d.top_message] = d
|
|
|
|
for m in r.messages:
|
|
|
|
messages[m.id] = m
|
|
|
|
|
|
|
|
# We assume users can't have the same ID as a chat
|
|
|
|
for u in r.users:
|
|
|
|
entities[u.id] = u
|
|
|
|
for c in r.chats:
|
|
|
|
entities[c.id] = c
|
|
|
|
|
2017-10-01 20:45:42 +03:00
|
|
|
if not isinstance(r, DialogsSlice):
|
2017-10-01 11:50:37 +03:00
|
|
|
# Don't enter next iteration if we already got all
|
|
|
|
break
|
|
|
|
|
|
|
|
offset_date = r.messages[-1].date
|
2017-10-01 17:57:07 +03:00
|
|
|
offset_peer = utils.find_user_or_chat(
|
|
|
|
r.dialogs[-1].peer, entities, entities
|
|
|
|
)
|
2017-10-01 11:50:37 +03:00
|
|
|
offset_id = r.messages[-1].id & 4294967296 # Telegram/danog magic
|
|
|
|
|
2017-10-01 22:02:41 +03:00
|
|
|
# Sort by message date. Windows will raise if timestamp is 0,
|
|
|
|
# so we need to set at least one day ahead while still being
|
|
|
|
# the smallest date possible.
|
|
|
|
no_date = datetime.fromtimestamp(86400)
|
2017-10-05 14:55:58 +03:00
|
|
|
ds = list(sorted(
|
|
|
|
dialogs.values(),
|
2017-10-01 11:50:37 +03:00
|
|
|
key=lambda d: getattr(messages[d.top_message], 'date', no_date)
|
2017-10-05 14:55:58 +03:00
|
|
|
))
|
|
|
|
if limit < float('inf'):
|
|
|
|
ds = ds[:limit]
|
2016-11-30 00:29:42 +03:00
|
|
|
return (
|
2017-10-01 17:57:07 +03:00
|
|
|
ds,
|
|
|
|
[utils.find_user_or_chat(d.peer, entities, entities) for d in ds]
|
2017-10-01 11:50:37 +03:00
|
|
|
)
|
2016-09-06 19:54:49 +03:00
|
|
|
|
2016-09-11 17:24:03 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Message requests
|
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
def send_message(self,
|
2017-01-17 22:22:47 +03:00
|
|
|
entity,
|
2016-11-30 00:29:42 +03:00
|
|
|
message,
|
2017-09-13 12:51:23 +03:00
|
|
|
reply_to=None,
|
2017-07-07 11:37:19 +03:00
|
|
|
link_preview=True):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Sends a message to the given entity (or input peer)
|
2017-09-13 13:00:27 +03:00
|
|
|
and returns the sent message as a Telegram object.
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-09-13 12:51:23 +03:00
|
|
|
If 'reply_to' is set to either a message or a message ID,
|
|
|
|
the sent message will be replying to such message.
|
2017-08-23 01:01:10 +03:00
|
|
|
"""
|
2017-10-01 17:54:11 +03:00
|
|
|
entity = self.get_input_entity(entity)
|
2017-07-04 17:53:07 +03:00
|
|
|
request = SendMessageRequest(
|
2017-09-18 12:59:55 +03:00
|
|
|
peer=entity,
|
2017-06-11 20:16:59 +03:00
|
|
|
message=message,
|
|
|
|
entities=[],
|
2017-09-13 12:51:23 +03:00
|
|
|
no_webpage=not link_preview,
|
|
|
|
reply_to_msg_id=self._get_reply_to(reply_to)
|
2017-07-04 17:53:07 +03:00
|
|
|
)
|
|
|
|
result = self(request)
|
2017-09-19 17:27:10 +03:00
|
|
|
if isinstance(result, UpdateShortSentMessage):
|
2017-09-18 12:59:55 +03:00
|
|
|
return Message(
|
|
|
|
id=result.id,
|
|
|
|
to_id=entity,
|
|
|
|
message=message,
|
|
|
|
date=result.date,
|
|
|
|
out=result.out,
|
|
|
|
media=result.media,
|
|
|
|
entities=result.entities
|
|
|
|
)
|
|
|
|
|
2017-09-13 13:00:27 +03:00
|
|
|
# Telegram seems to send updateMessageID first, then updateNewMessage,
|
|
|
|
# however let's not rely on that just in case.
|
|
|
|
msg_id = None
|
|
|
|
for update in result.updates:
|
|
|
|
if isinstance(update, UpdateMessageID):
|
|
|
|
if update.random_id == request.random_id:
|
|
|
|
msg_id = update.id
|
|
|
|
break
|
|
|
|
|
|
|
|
for update in result.updates:
|
|
|
|
if isinstance(update, UpdateNewMessage):
|
|
|
|
if update.message.id == msg_id:
|
|
|
|
return update.message
|
|
|
|
|
|
|
|
return None # Should not happen
|
2016-09-06 19:54:49 +03:00
|
|
|
|
2017-10-02 19:59:29 +03:00
|
|
|
def delete_messages(self, entity, message_ids, revoke=True):
|
|
|
|
"""
|
|
|
|
Deletes a message from a chat, optionally "for everyone" with argument
|
|
|
|
`revoke` set to `True`.
|
|
|
|
|
|
|
|
The `revoke` argument has no effect for Channels and Supergroups,
|
|
|
|
where it inherently behaves as being `True`.
|
|
|
|
|
|
|
|
Note: The `entity` argument can be `None` for normal chats, but it's
|
|
|
|
mandatory to delete messages from Channels and Supergroups. It is also
|
|
|
|
possible to supply a chat_id which will be automatically resolved to
|
|
|
|
the right type of InputPeer.
|
|
|
|
|
|
|
|
:param entity: ID or Entity of the chat
|
|
|
|
:param list message_ids: ID(s) or `Message` object(s) of the message(s) to delete
|
|
|
|
:param revoke: Delete the message for everyone or just this client
|
|
|
|
:returns .messages.AffectedMessages: Messages affected by deletion.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not isinstance(message_ids, list):
|
|
|
|
message_ids = [message_ids]
|
|
|
|
message_ids = [m.id if isinstance(m, Message) else int(m) for m in message_ids]
|
|
|
|
|
|
|
|
if entity is None:
|
|
|
|
return self(messages.DeleteMessagesRequest(message_ids, revoke=revoke))
|
|
|
|
|
|
|
|
entity = self.get_input_entity(entity)
|
|
|
|
|
|
|
|
if isinstance(entity, InputPeerChannel):
|
|
|
|
return self(channels.DeleteMessagesRequest(entity, message_ids))
|
|
|
|
else:
|
|
|
|
return self(messages.DeleteMessagesRequest(message_ids, revoke=revoke))
|
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
def get_message_history(self,
|
2017-01-17 22:22:47 +03:00
|
|
|
entity,
|
2016-11-30 00:29:42 +03:00
|
|
|
limit=20,
|
|
|
|
offset_date=None,
|
|
|
|
offset_id=0,
|
|
|
|
max_id=0,
|
|
|
|
min_id=0,
|
|
|
|
add_offset=0):
|
2016-09-08 13:13:31 +03:00
|
|
|
"""
|
2017-01-17 22:22:47 +03:00
|
|
|
Gets the message history for the specified entity
|
2016-09-08 13:13:31 +03:00
|
|
|
|
2017-08-23 01:01:10 +03:00
|
|
|
:param entity: The entity from whom to retrieve the message history
|
2016-09-08 13:13:31 +03:00
|
|
|
:param limit: Number of messages to be retrieved
|
|
|
|
:param offset_date: Offset date (messages *previous* to this date will be retrieved)
|
|
|
|
:param offset_id: Offset message ID (only messages *previous* to the given ID will be retrieved)
|
|
|
|
:param max_id: All the messages with a higher (newer) ID or equal to this will be excluded
|
|
|
|
:param min_id: All the messages with a lower (older) ID or equal to this will be excluded
|
|
|
|
:param add_offset: Additional message offset (all of the specified offsets + this offset = older messages)
|
|
|
|
|
|
|
|
:return: A tuple containing total message count and two more lists ([messages], [senders]).
|
|
|
|
Note that the sender can be null if it was not found!
|
2017-08-23 01:01:10 +03:00
|
|
|
|
|
|
|
The entity may be a phone or an username at the expense of
|
|
|
|
some performance loss.
|
2016-09-08 13:13:31 +03:00
|
|
|
"""
|
2017-07-02 12:56:40 +03:00
|
|
|
result = self(GetHistoryRequest(
|
2017-10-01 17:54:11 +03:00
|
|
|
peer=self.get_input_entity(entity),
|
2017-07-02 12:56:40 +03:00
|
|
|
limit=limit,
|
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
max_id=max_id,
|
|
|
|
min_id=min_id,
|
|
|
|
add_offset=add_offset
|
|
|
|
))
|
2016-09-08 13:13:31 +03:00
|
|
|
|
2017-06-14 15:06:35 +03:00
|
|
|
# The result may be a messages slice (not all messages were retrieved)
|
|
|
|
# or simply a messages TLObject. In the later case, no "count"
|
|
|
|
# attribute is specified, so the total messages count is simply
|
|
|
|
# the count of retrieved messages
|
2016-09-08 13:13:31 +03:00
|
|
|
total_messages = getattr(result, 'count', len(result.messages))
|
2016-09-11 11:35:02 +03:00
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
# Iterate over all the messages and find the sender User
|
2017-10-01 17:57:07 +03:00
|
|
|
entities = [
|
|
|
|
utils.find_user_or_chat(m.from_id, result.users, result.chats)
|
|
|
|
if m.from_id is not None else
|
|
|
|
utils.find_user_or_chat(m.to_id, result.users, result.chats)
|
|
|
|
|
|
|
|
for m in result.messages
|
|
|
|
]
|
2017-06-14 15:06:35 +03:00
|
|
|
|
|
|
|
return total_messages, result.messages, entities
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-01-17 22:22:47 +03:00
|
|
|
def send_read_acknowledge(self, entity, messages=None, max_id=None):
|
2016-10-02 14:57:03 +03:00
|
|
|
"""Sends a "read acknowledge" (i.e., notifying the given peer that we've
|
2017-06-10 14:39:37 +03:00
|
|
|
read their messages, also known as the "double check").
|
2016-10-02 14:57:03 +03:00
|
|
|
|
|
|
|
Either a list of messages (or a single message) can be given,
|
|
|
|
or the maximum message ID (until which message we want to send the read acknowledge).
|
|
|
|
|
2017-08-23 01:01:10 +03:00
|
|
|
Returns an AffectedMessages TLObject
|
|
|
|
|
|
|
|
The entity may be a phone or an username at the expense of
|
|
|
|
some performance loss.
|
|
|
|
"""
|
2016-10-02 14:57:03 +03:00
|
|
|
if max_id is None:
|
|
|
|
if not messages:
|
2016-11-30 00:29:42 +03:00
|
|
|
raise InvalidParameterError(
|
|
|
|
'Either a message list or a max_id must be provided.')
|
2016-10-02 14:57:03 +03:00
|
|
|
|
|
|
|
if isinstance(messages, list):
|
|
|
|
max_id = max(msg.id for msg in messages)
|
|
|
|
else:
|
|
|
|
max_id = messages.id
|
|
|
|
|
2017-07-02 12:56:40 +03:00
|
|
|
return self(ReadHistoryRequest(
|
2017-10-01 17:54:11 +03:00
|
|
|
peer=self.get_input_entity(entity),
|
2017-07-02 12:56:40 +03:00
|
|
|
max_id=max_id
|
|
|
|
))
|
2016-10-02 14:57:03 +03:00
|
|
|
|
2017-09-13 12:51:23 +03:00
|
|
|
@staticmethod
|
|
|
|
def _get_reply_to(reply_to):
|
|
|
|
"""Sanitizes the 'reply_to' parameter a user may send"""
|
|
|
|
if reply_to is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if isinstance(reply_to, int):
|
|
|
|
return reply_to
|
|
|
|
|
|
|
|
if isinstance(reply_to, TLObject) and \
|
2017-10-02 19:59:29 +03:00
|
|
|
type(reply_to).SUBCLASS_OF_ID == 0x790009e3:
|
2017-09-13 12:51:23 +03:00
|
|
|
# hex(crc32(b'Message')) = 0x790009e3
|
|
|
|
return reply_to.id
|
|
|
|
|
|
|
|
raise ValueError('Invalid reply_to type: ', type(reply_to))
|
|
|
|
|
2016-09-11 17:24:03 +03:00
|
|
|
# endregion
|
|
|
|
|
2017-06-15 16:50:44 +03:00
|
|
|
# region Uploading files
|
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
def send_file(self, entity, file, caption='',
|
2017-09-13 12:30:20 +03:00
|
|
|
force_document=False, progress_callback=None,
|
2017-09-13 12:51:23 +03:00
|
|
|
reply_to=None,
|
2017-09-13 12:30:20 +03:00
|
|
|
**kwargs):
|
2017-08-23 01:27:33 +03:00
|
|
|
"""Sends a file to the specified entity.
|
|
|
|
The file may either be a path, a byte array, or a stream.
|
|
|
|
|
|
|
|
An optional caption can also be specified for said file.
|
|
|
|
|
|
|
|
If "force_document" is False, the file will be sent as a photo
|
|
|
|
if it's recognised to have a common image format (e.g. .png, .jpg).
|
2016-09-11 14:10:27 +03:00
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
Otherwise, the file will always be sent as an uncompressed document.
|
|
|
|
|
|
|
|
Subsequent calls with the very same file will result in
|
|
|
|
immediate uploads, unless .clear_file_cache() is called.
|
|
|
|
|
|
|
|
If "progress_callback" is not None, it should be a function that
|
|
|
|
takes two parameters, (bytes_uploaded, total_bytes).
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-09-13 12:51:23 +03:00
|
|
|
The "reply_to" parameter works exactly as the one on .send_message.
|
|
|
|
|
|
|
|
If "is_voice_note" in kwargs, despite its value, and the file is
|
2017-09-13 12:30:20 +03:00
|
|
|
sent as a document, it will be sent as a voice note.
|
|
|
|
|
2017-08-23 01:01:10 +03:00
|
|
|
The entity may be a phone or an username at the expense of
|
|
|
|
some performance loss.
|
|
|
|
"""
|
2017-08-23 01:27:33 +03:00
|
|
|
as_photo = False
|
|
|
|
if isinstance(file, str):
|
|
|
|
lowercase_file = file.lower()
|
|
|
|
as_photo = any(
|
|
|
|
lowercase_file.endswith(ext)
|
|
|
|
for ext in ('.png', '.jpg', '.gif', '.jpeg')
|
|
|
|
)
|
|
|
|
|
|
|
|
file_hash = hash(file)
|
|
|
|
if file_hash in self._upload_cache:
|
|
|
|
file_handle = self._upload_cache[file_hash]
|
|
|
|
else:
|
2017-08-23 01:55:34 +03:00
|
|
|
self._upload_cache[file_hash] = file_handle = self.upload_file(
|
|
|
|
file, progress_callback=progress_callback
|
|
|
|
)
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
if as_photo and not force_document:
|
|
|
|
media = InputMediaUploadedPhoto(file_handle, caption)
|
|
|
|
else:
|
|
|
|
mime_type = None
|
|
|
|
if isinstance(file, str):
|
|
|
|
# Determine mime-type and attributes
|
|
|
|
# Take the first element by using [0] since it returns a tuple
|
|
|
|
mime_type = guess_type(file)[0]
|
|
|
|
attributes = [
|
2017-10-06 22:48:53 +03:00
|
|
|
DocumentAttributeFilename(os.path.basename(file))
|
2017-08-23 01:27:33 +03:00
|
|
|
# TODO If the input file is an audio, find out:
|
|
|
|
# Performer and song title and add DocumentAttributeAudio
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
attributes = [DocumentAttributeFilename('unnamed')]
|
|
|
|
|
2017-09-13 12:30:20 +03:00
|
|
|
if 'is_voice_note' in kwargs:
|
|
|
|
attributes.append(DocumentAttributeAudio(0, voice=True))
|
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
# Ensure we have a mime type, any; but it cannot be None
|
|
|
|
# 'The "octet-stream" subtype is used to indicate that a body
|
|
|
|
# contains arbitrary binary data.'
|
|
|
|
if not mime_type:
|
|
|
|
mime_type = 'application/octet-stream'
|
|
|
|
|
|
|
|
media = InputMediaUploadedDocument(
|
|
|
|
file=file_handle,
|
2016-11-30 00:29:42 +03:00
|
|
|
mime_type=mime_type,
|
|
|
|
attributes=attributes,
|
2017-08-23 01:27:33 +03:00
|
|
|
caption=caption
|
|
|
|
)
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
# Once the media type is properly specified and the file uploaded,
|
|
|
|
# send the media message to the desired entity.
|
2017-07-02 12:56:40 +03:00
|
|
|
self(SendMediaRequest(
|
2017-10-01 17:54:11 +03:00
|
|
|
peer=self.get_input_entity(entity),
|
2017-09-13 12:51:23 +03:00
|
|
|
media=media,
|
|
|
|
reply_to_msg_id=self._get_reply_to(reply_to)
|
2017-06-11 20:16:59 +03:00
|
|
|
))
|
2016-09-11 14:10:27 +03:00
|
|
|
|
2017-09-13 12:51:23 +03:00
|
|
|
def send_voice_note(self, entity, file, caption='', upload_progress=None,
|
|
|
|
reply_to=None):
|
2017-09-13 12:30:20 +03:00
|
|
|
"""Wrapper method around .send_file() with is_voice_note=()"""
|
|
|
|
return self.send_file(entity, file, caption,
|
|
|
|
upload_progress=upload_progress,
|
2017-09-13 12:51:23 +03:00
|
|
|
reply_to=reply_to,
|
2017-09-13 12:30:20 +03:00
|
|
|
is_voice_note=()) # empty tuple is enough
|
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
def clear_file_cache(self):
|
|
|
|
"""Calls to .send_file() will cache the remote location of the
|
|
|
|
uploaded files so that subsequent files can be immediate, so
|
|
|
|
uploading the same file path will result in using the cached
|
|
|
|
version. To avoid this a call to this method should be made.
|
|
|
|
"""
|
|
|
|
self._upload_cache.clear()
|
|
|
|
|
2016-09-12 20:32:16 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Downloading media requests
|
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
def download_profile_photo(self, entity, file=None, download_big=True):
|
2017-08-23 02:35:12 +03:00
|
|
|
"""Downloads the profile photo for an user or a chat (channels too).
|
|
|
|
Returns None if no photo was provided, or if it was Empty.
|
2016-10-03 20:44:01 +03:00
|
|
|
|
2017-08-23 02:35:12 +03:00
|
|
|
If an entity itself (an user, chat or channel) is given, the photo
|
|
|
|
to be downloaded will be downloaded automatically.
|
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
On success, the file path is returned since it may differ from
|
|
|
|
the one provided.
|
|
|
|
|
|
|
|
The specified output file can either be a file path, a directory,
|
|
|
|
or a stream-like object. If the path exists and is a file, it will
|
|
|
|
be overwritten.
|
2017-08-23 02:35:12 +03:00
|
|
|
|
|
|
|
The entity may be a phone or an username at the expense of
|
|
|
|
some performance loss.
|
|
|
|
"""
|
2017-08-24 18:44:38 +03:00
|
|
|
possible_names = []
|
2017-10-02 14:26:55 +03:00
|
|
|
if not isinstance(entity, TLObject) or type(entity).SUBCLASS_OF_ID in (
|
2017-10-02 19:59:29 +03:00
|
|
|
0x2da17977, 0xc5af5d94, 0x1f4661b9, 0xd49a2697
|
|
|
|
):
|
2017-08-23 02:35:12 +03:00
|
|
|
# Maybe it is an user or a chat? Or their full versions?
|
|
|
|
#
|
|
|
|
# The hexadecimal numbers above are simply:
|
|
|
|
# hex(crc32(x.encode('ascii'))) for x in
|
|
|
|
# ('User', 'Chat', 'UserFull', 'ChatFull')
|
2017-09-11 12:54:32 +03:00
|
|
|
entity = self.get_entity(entity)
|
2017-08-23 02:35:12 +03:00
|
|
|
if not hasattr(entity, 'photo'):
|
|
|
|
# Special case: may be a ChatFull with photo:Photo
|
|
|
|
# This is different from a normal UserProfilePhoto and Chat
|
|
|
|
if hasattr(entity, 'chat_photo'):
|
|
|
|
return self._download_photo(
|
2017-08-24 18:44:38 +03:00
|
|
|
entity.chat_photo, file,
|
|
|
|
date=None, progress_callback=None
|
2017-08-23 02:35:12 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Give up
|
|
|
|
return None
|
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
for attr in ('username', 'first_name', 'title'):
|
|
|
|
possible_names.append(getattr(entity, attr, None))
|
2017-08-23 02:35:12 +03:00
|
|
|
|
|
|
|
entity = entity.photo
|
|
|
|
|
|
|
|
if not isinstance(entity, UserProfilePhoto) and \
|
|
|
|
not isinstance(entity, ChatPhoto):
|
|
|
|
return None
|
|
|
|
|
2016-10-03 20:44:01 +03:00
|
|
|
if download_big:
|
2017-08-23 02:35:12 +03:00
|
|
|
photo_location = entity.photo_big
|
2016-10-03 20:44:01 +03:00
|
|
|
else:
|
2017-08-23 02:35:12 +03:00
|
|
|
photo_location = entity.photo_small
|
2016-10-03 20:44:01 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
file = self._get_proper_filename(
|
|
|
|
file, 'profile_photo', '.jpg',
|
|
|
|
possible_names=possible_names
|
|
|
|
)
|
|
|
|
|
2016-10-03 20:44:01 +03:00
|
|
|
# Download the media with the largest size input file location
|
2017-06-08 14:12:57 +03:00
|
|
|
self.download_file(
|
2016-11-30 00:29:42 +03:00
|
|
|
InputFileLocation(
|
|
|
|
volume_id=photo_location.volume_id,
|
|
|
|
local_id=photo_location.local_id,
|
2017-06-08 14:12:57 +03:00
|
|
|
secret=photo_location.secret
|
|
|
|
),
|
2017-08-24 18:44:38 +03:00
|
|
|
file
|
2017-06-08 14:12:57 +03:00
|
|
|
)
|
2017-08-24 18:44:38 +03:00
|
|
|
return file
|
2016-10-03 20:44:01 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
def download_media(self, message, file=None, progress_callback=None):
|
2017-08-23 01:48:00 +03:00
|
|
|
"""Downloads the media from a specified Message (it can also be
|
|
|
|
the message.media) into the desired file (a stream or str),
|
|
|
|
optionally finding its extension automatically.
|
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
The specified output file can either be a file path, a directory,
|
|
|
|
or a stream-like object. If the path exists and is a file, it will
|
|
|
|
be overwritten.
|
2017-08-23 01:48:00 +03:00
|
|
|
|
|
|
|
If the operation succeeds, the path will be returned (since
|
|
|
|
the extension may have been added automatically). Otherwise,
|
|
|
|
None is returned.
|
2017-07-23 19:38:27 +03:00
|
|
|
|
|
|
|
The progress_callback should be a callback function which takes
|
|
|
|
two parameters, uploaded size and total file size (both in bytes).
|
|
|
|
This will be called every time a part is downloaded
|
|
|
|
"""
|
2017-08-23 01:48:00 +03:00
|
|
|
# TODO This won't work for messageService
|
|
|
|
if isinstance(message, Message):
|
|
|
|
date = message.date
|
2017-08-24 18:44:38 +03:00
|
|
|
media = message.media
|
2017-08-23 01:48:00 +03:00
|
|
|
else:
|
|
|
|
date = datetime.now()
|
2017-08-24 18:44:38 +03:00
|
|
|
media = message
|
2017-08-23 01:48:00 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
if isinstance(media, MessageMediaPhoto):
|
2017-08-23 01:48:00 +03:00
|
|
|
return self._download_photo(
|
2017-08-24 18:44:38 +03:00
|
|
|
media, file, date, progress_callback
|
2017-08-23 01:48:00 +03:00
|
|
|
)
|
2017-08-24 18:44:38 +03:00
|
|
|
elif isinstance(media, MessageMediaDocument):
|
2017-08-23 01:48:00 +03:00
|
|
|
return self._download_document(
|
2017-08-24 18:44:38 +03:00
|
|
|
media, file, date, progress_callback
|
2017-08-23 01:48:00 +03:00
|
|
|
)
|
2017-08-24 18:44:38 +03:00
|
|
|
elif isinstance(media, MessageMediaContact):
|
2017-08-23 01:48:00 +03:00
|
|
|
return self._download_contact(
|
2017-08-24 18:44:38 +03:00
|
|
|
media, file
|
2017-08-23 01:48:00 +03:00
|
|
|
)
|
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
def _download_photo(self, mm_photo, file, date, progress_callback):
|
2017-08-23 01:48:00 +03:00
|
|
|
"""Specialized version of .download_media() for photos"""
|
2016-09-17 18:04:30 +03:00
|
|
|
|
2016-09-11 17:24:03 +03:00
|
|
|
# Determine the photo and its largest size
|
2017-08-24 18:44:38 +03:00
|
|
|
photo = mm_photo.photo
|
2016-09-17 18:04:30 +03:00
|
|
|
largest_size = photo.sizes[-1]
|
|
|
|
file_size = largest_size.size
|
|
|
|
largest_size = largest_size.location
|
2016-09-10 19:05:20 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
file = self._get_proper_filename(file, 'photo', '.jpg', date=date)
|
2016-09-10 19:05:20 +03:00
|
|
|
|
2016-09-12 20:32:16 +03:00
|
|
|
# Download the media with the largest size input file location
|
2017-06-08 14:12:57 +03:00
|
|
|
self.download_file(
|
2016-11-30 00:29:42 +03:00
|
|
|
InputFileLocation(
|
|
|
|
volume_id=largest_size.volume_id,
|
|
|
|
local_id=largest_size.local_id,
|
2017-06-08 14:12:57 +03:00
|
|
|
secret=largest_size.secret
|
|
|
|
),
|
2017-07-20 10:37:19 +03:00
|
|
|
file,
|
2016-11-30 00:29:42 +03:00
|
|
|
file_size=file_size,
|
2017-06-08 14:12:57 +03:00
|
|
|
progress_callback=progress_callback
|
|
|
|
)
|
2017-07-20 10:37:19 +03:00
|
|
|
return file
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
def _download_document(self, mm_doc, file, date, progress_callback):
|
2017-08-23 01:48:00 +03:00
|
|
|
"""Specialized version of .download_media() for documents"""
|
2017-08-24 18:44:38 +03:00
|
|
|
document = mm_doc.document
|
2016-09-17 18:04:30 +03:00
|
|
|
file_size = document.size
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
possible_names = []
|
|
|
|
for attr in document.attributes:
|
|
|
|
if isinstance(attr, DocumentAttributeFilename):
|
|
|
|
possible_names.insert(0, attr.file_name)
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
elif isinstance(attr, DocumentAttributeAudio):
|
|
|
|
possible_names.append('{} - {}'.format(
|
|
|
|
attr.performer, attr.title
|
|
|
|
))
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
file = self._get_proper_filename(
|
2017-10-01 17:57:07 +03:00
|
|
|
file, 'document', utils.get_extension(mm_doc),
|
2017-08-24 18:44:38 +03:00
|
|
|
date=date, possible_names=possible_names
|
|
|
|
)
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
self.download_file(
|
2016-11-30 00:29:42 +03:00
|
|
|
InputDocumentFileLocation(
|
|
|
|
id=document.id,
|
|
|
|
access_hash=document.access_hash,
|
2017-06-08 14:12:57 +03:00
|
|
|
version=document.version
|
|
|
|
),
|
2017-07-20 10:37:19 +03:00
|
|
|
file,
|
2016-11-30 00:29:42 +03:00
|
|
|
file_size=file_size,
|
2017-06-08 14:12:57 +03:00
|
|
|
progress_callback=progress_callback
|
|
|
|
)
|
2017-07-20 10:37:19 +03:00
|
|
|
return file
|
2016-09-12 20:32:16 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2017-08-24 18:44:38 +03:00
|
|
|
def _download_contact(mm_contact, file):
|
2017-08-23 01:48:00 +03:00
|
|
|
"""Specialized version of .download_media() for contacts.
|
|
|
|
Will make use of the vCard 4.0 format
|
|
|
|
"""
|
2017-08-24 18:44:38 +03:00
|
|
|
first_name = mm_contact.first_name
|
|
|
|
last_name = mm_contact.last_name
|
|
|
|
phone_number = mm_contact.phone_number
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-07-20 10:37:19 +03:00
|
|
|
if isinstance(file, str):
|
2017-08-24 18:44:38 +03:00
|
|
|
file = TelegramClient._get_proper_filename(
|
|
|
|
file, 'contact', '.vcard',
|
|
|
|
possible_names=[first_name, phone_number, last_name]
|
|
|
|
)
|
2017-07-23 18:08:04 +03:00
|
|
|
f = open(file, 'w', encoding='utf-8')
|
2017-07-20 10:37:19 +03:00
|
|
|
else:
|
2017-07-23 18:08:04 +03:00
|
|
|
f = file
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.write('BEGIN:VCARD\n')
|
|
|
|
f.write('VERSION:4.0\n')
|
|
|
|
f.write('N:{};{};;;\n'.format(
|
|
|
|
first_name, last_name if last_name else '')
|
|
|
|
)
|
|
|
|
f.write('FN:{}\n'.format(' '.join((first_name, last_name))))
|
|
|
|
f.write('TEL;TYPE=cell;VALUE=uri:tel:+{}\n'.format(
|
2016-11-30 00:29:42 +03:00
|
|
|
phone_number))
|
2017-07-23 18:08:04 +03:00
|
|
|
f.write('END:VCARD\n')
|
|
|
|
finally:
|
|
|
|
# Only close the stream if we opened it
|
|
|
|
if isinstance(file, str):
|
|
|
|
f.close()
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-07-20 10:37:19 +03:00
|
|
|
return file
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2017-08-24 18:44:38 +03:00
|
|
|
@staticmethod
|
|
|
|
def _get_proper_filename(file, kind, extension,
|
|
|
|
date=None, possible_names=None):
|
|
|
|
"""Gets a proper filename for 'file', if this is a path.
|
|
|
|
|
|
|
|
'kind' should be the kind of the output file (photo, document...)
|
|
|
|
'extension' should be the extension to be added to the file if
|
|
|
|
the filename doesn't have any yet
|
|
|
|
'date' should be when this file was originally sent, if known
|
|
|
|
'possible_names' should be an ordered list of possible names
|
|
|
|
|
|
|
|
If no modification is made to the path, any existing file
|
|
|
|
will be overwritten.
|
|
|
|
If any modification is made to the path, this method will
|
|
|
|
ensure that no existing file will be overwritten.
|
|
|
|
"""
|
|
|
|
if file is not None and not isinstance(file, str):
|
|
|
|
# Probably a stream-like object, we cannot set a filename here
|
|
|
|
return file
|
|
|
|
|
|
|
|
if file is None:
|
|
|
|
file = ''
|
|
|
|
elif os.path.isfile(file):
|
|
|
|
# Make no modifications to valid existing paths
|
|
|
|
return file
|
|
|
|
|
|
|
|
if os.path.isdir(file) or not file:
|
|
|
|
try:
|
|
|
|
name = None if possible_names is None else next(
|
|
|
|
x for x in possible_names if x
|
|
|
|
)
|
|
|
|
except StopIteration:
|
|
|
|
name = None
|
|
|
|
|
|
|
|
if not name:
|
|
|
|
name = '{}_{}-{:02}-{:02}_{:02}-{:02}-{:02}'.format(
|
|
|
|
kind,
|
|
|
|
date.year, date.month, date.day,
|
|
|
|
date.hour, date.minute, date.second,
|
|
|
|
)
|
|
|
|
file = os.path.join(file, name)
|
|
|
|
|
|
|
|
directory, name = os.path.split(file)
|
|
|
|
name, ext = os.path.splitext(name)
|
|
|
|
if not ext:
|
|
|
|
ext = extension
|
|
|
|
|
|
|
|
result = os.path.join(directory, name + ext)
|
|
|
|
if not os.path.isfile(result):
|
|
|
|
return result
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
while True:
|
|
|
|
result = os.path.join(directory, '{} ({}){}'.format(name, i, ext))
|
|
|
|
if not os.path.isfile(result):
|
|
|
|
return result
|
|
|
|
i += 1
|
|
|
|
|
2016-09-11 17:24:03 +03:00
|
|
|
# endregion
|
2016-09-10 19:05:20 +03:00
|
|
|
|
2016-09-07 12:36:34 +03:00
|
|
|
# endregion
|
|
|
|
|
2017-08-23 01:01:10 +03:00
|
|
|
# region Small utilities to make users' life easier
|
|
|
|
|
2017-09-11 12:54:32 +03:00
|
|
|
def get_entity(self, entity):
|
2017-08-23 01:01:10 +03:00
|
|
|
"""Turns an entity into a valid Telegram user or chat.
|
2017-10-01 15:19:04 +03:00
|
|
|
If "entity" is a string which can be converted to an integer,
|
|
|
|
or if it starts with '+' it will be resolved as if it
|
2017-08-23 01:01:10 +03:00
|
|
|
were a phone number.
|
|
|
|
|
|
|
|
If "entity" is a string and doesn't start with '+', or
|
|
|
|
it starts with '@', it will be resolved from the username.
|
|
|
|
If no exact match is returned, an error will be raised.
|
|
|
|
|
2017-10-01 15:19:04 +03:00
|
|
|
If "entity" is an integer or a "Peer", its information will
|
|
|
|
be returned through a call to self.get_input_peer(entity).
|
|
|
|
|
2017-08-23 01:01:10 +03:00
|
|
|
If the entity is neither, and it's not a TLObject, an
|
|
|
|
error will be raised.
|
|
|
|
"""
|
2017-10-05 13:27:05 +03:00
|
|
|
try:
|
|
|
|
return self.session.entities[entity]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-10-05 13:27:05 +03:00
|
|
|
if isinstance(entity, int) or (
|
|
|
|
isinstance(entity, TLObject) and
|
|
|
|
# crc32(b'InputPeer') and crc32(b'Peer')
|
|
|
|
type(entity).SUBCLASS_OF_ID in (0xc91c90b6, 0x2d45687)):
|
|
|
|
ie = self.get_input_entity(entity)
|
2017-10-06 22:05:02 +03:00
|
|
|
result = None
|
2017-10-05 13:27:05 +03:00
|
|
|
if isinstance(ie, InputPeerUser):
|
2017-10-06 22:05:02 +03:00
|
|
|
result = self(GetUsersRequest([ie]))
|
2017-10-05 13:27:05 +03:00
|
|
|
elif isinstance(ie, InputPeerChat):
|
2017-10-06 22:05:02 +03:00
|
|
|
result = self(GetChatsRequest([ie.chat_id]))
|
2017-10-05 13:27:05 +03:00
|
|
|
elif isinstance(ie, InputPeerChannel):
|
2017-10-06 22:05:02 +03:00
|
|
|
result = self(GetChannelsRequest([ie]))
|
|
|
|
|
|
|
|
if result:
|
|
|
|
self.session.process_entities(result)
|
|
|
|
try:
|
|
|
|
return self.session.entities[ie]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-08-23 01:01:10 +03:00
|
|
|
|
|
|
|
if isinstance(entity, str):
|
2017-10-05 13:27:05 +03:00
|
|
|
return self._get_entity_from_string(entity)
|
2017-08-23 01:01:10 +03:00
|
|
|
|
|
|
|
raise ValueError(
|
|
|
|
'Cannot turn "{}" into any entity (user or chat)'.format(entity)
|
|
|
|
)
|
|
|
|
|
2017-10-05 13:27:05 +03:00
|
|
|
def _get_entity_from_string(self, string):
|
|
|
|
"""Gets an entity from the given string, which may be a phone or
|
|
|
|
an username, and processes all the found entities on the session.
|
|
|
|
"""
|
2017-10-05 14:01:00 +03:00
|
|
|
phone = EntityDatabase.parse_phone(string)
|
|
|
|
if phone:
|
|
|
|
entity = phone
|
|
|
|
self.session.process_entities(self(GetContactsRequest(0)))
|
2017-10-05 13:27:05 +03:00
|
|
|
else:
|
|
|
|
entity = string.strip('@').lower()
|
|
|
|
self.session.process_entities(self(ResolveUsernameRequest(entity)))
|
|
|
|
|
2017-10-05 14:01:00 +03:00
|
|
|
try:
|
|
|
|
return self.session.entities[entity]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError(
|
|
|
|
'Could not find user with username {}'.format(entity)
|
|
|
|
)
|
2017-10-01 20:02:53 +03:00
|
|
|
|
2017-10-01 15:19:04 +03:00
|
|
|
def get_input_entity(self, peer):
|
|
|
|
"""Gets the input entity given its PeerUser, PeerChat, PeerChannel.
|
|
|
|
If no Peer class is used, peer is assumed to be the integer ID
|
|
|
|
of an User.
|
|
|
|
|
|
|
|
If this Peer hasn't been seen before by the library, all dialogs
|
|
|
|
will loaded, and their entities saved to the session file.
|
|
|
|
|
2017-10-05 14:01:00 +03:00
|
|
|
If even after it's not found, a ValueError is raised.
|
2017-10-01 15:19:04 +03:00
|
|
|
"""
|
2017-10-05 13:27:05 +03:00
|
|
|
try:
|
|
|
|
# First try to get the entity from cache, otherwise figure it out
|
2017-10-06 22:43:04 +03:00
|
|
|
return self.session.entities.get_input_entity(peer)
|
2017-10-05 13:27:05 +03:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2017-10-01 17:57:07 +03:00
|
|
|
if isinstance(peer, str):
|
2017-10-05 13:27:05 +03:00
|
|
|
return utils.get_input_peer(self._get_entity_from_string(peer))
|
2017-10-01 17:57:07 +03:00
|
|
|
|
2017-10-01 15:19:04 +03:00
|
|
|
is_peer = False
|
|
|
|
if isinstance(peer, int):
|
|
|
|
peer = PeerUser(peer)
|
|
|
|
is_peer = True
|
|
|
|
|
|
|
|
elif isinstance(peer, TLObject):
|
|
|
|
is_peer = type(peer).SUBCLASS_OF_ID == 0x2d45687 # crc32(b'Peer')
|
2017-10-01 22:15:49 +03:00
|
|
|
if not is_peer:
|
|
|
|
try:
|
|
|
|
return utils.get_input_peer(peer)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2017-10-01 15:19:04 +03:00
|
|
|
|
|
|
|
if not is_peer:
|
|
|
|
raise ValueError(
|
|
|
|
'Cannot turn "{}" into an input entity.'.format(peer)
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.session.save_entities:
|
|
|
|
# Not found, look in the dialogs (this will save the users)
|
|
|
|
self.get_dialogs(limit=None)
|
2017-10-05 13:27:05 +03:00
|
|
|
|
2017-10-01 15:19:04 +03:00
|
|
|
try:
|
2017-10-06 22:43:04 +03:00
|
|
|
return self.session.entities.get_input_entity(peer)
|
2017-10-01 15:19:04 +03:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
raise ValueError(
|
|
|
|
'Could not find the input entity corresponding to "{}".'
|
|
|
|
'Make sure you have encountered this peer before.'.format(peer)
|
|
|
|
)
|
|
|
|
|
2017-10-02 19:59:29 +03:00
|
|
|
# endregion
|