2017-12-24 18:18:09 +03:00
|
|
|
import itertools
|
2017-08-23 01:27:33 +03:00
|
|
|
import os
|
2017-10-31 14:48:55 +03:00
|
|
|
import time
|
2017-12-28 13:49:35 +03:00
|
|
|
from collections import OrderedDict, UserList
|
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 (
|
2017-12-28 02:22:28 +03:00
|
|
|
RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError,
|
|
|
|
PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError
|
2017-09-04 18:18:33 +03:00
|
|
|
)
|
2017-09-29 21:50:27 +03:00
|
|
|
from .network import ConnectionMode
|
|
|
|
from .tl import TLObject
|
2017-12-24 18:18:09 +03:00
|
|
|
from .tl.custom import Draft, Dialog
|
2017-09-04 18:18:33 +03:00
|
|
|
from .tl.functions.account import (
|
|
|
|
GetPasswordRequest
|
|
|
|
)
|
|
|
|
from .tl.functions.auth import (
|
|
|
|
CheckPasswordRequest, LogOutRequest, SendCodeRequest, SignInRequest,
|
2017-11-04 22:46:02 +03:00
|
|
|
SignUpRequest, ResendCodeRequest, ImportBotAuthorizationRequest
|
2017-09-04 18:18:33 +03:00
|
|
|
)
|
|
|
|
from .tl.functions.contacts import (
|
|
|
|
GetContactsRequest, ResolveUsernameRequest
|
|
|
|
)
|
2017-05-21 14:02:54 +03:00
|
|
|
from .tl.functions.messages import (
|
2017-12-28 13:55:05 +03:00
|
|
|
GetDialogsRequest, GetHistoryRequest, SendMediaRequest,
|
2017-11-10 15:27:51 +03:00
|
|
|
SendMessageRequest, GetChatsRequest, GetAllDraftsRequest,
|
|
|
|
CheckChatInviteRequest
|
|
|
|
)
|
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 (
|
2017-12-28 01:54:31 +03:00
|
|
|
GetChannelsRequest, GetFullChannelRequest
|
2017-10-01 15:19:04 +03:00
|
|
|
)
|
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-13 11:53:36 +03:00
|
|
|
UpdateNewChannelMessage, UpdateNewMessage, UpdateShortSentMessage,
|
2017-11-10 15:27:51 +03:00
|
|
|
PeerUser, InputPeerUser, InputPeerChat, InputPeerChannel, MessageEmpty,
|
2018-01-05 21:51:44 +03:00
|
|
|
ChatInvite, ChatInviteAlready, PeerChannel, Photo, InputPeerSelf
|
2017-10-31 14:48:55 +03:00
|
|
|
)
|
2017-10-01 11:50:37 +03:00
|
|
|
from .tl.types.messages import DialogsSlice
|
2017-10-30 13:17:22 +03:00
|
|
|
from .extensions import markdown
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
|
|
|
|
class TelegramClient(TelegramBareClient):
|
2018-01-05 15:30:21 +03:00
|
|
|
"""
|
|
|
|
Initializes the Telegram client with the specified API ID and Hash.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (:obj:`str` | :obj:`Session` | :obj:`None`):
|
|
|
|
The file name of the session file to be used if a string is
|
|
|
|
given (it may be a full path), or the Session instance to be
|
|
|
|
used otherwise. If it's ``None``, the session will not be saved,
|
|
|
|
and you should call :meth:`.log_out()` when you're done.
|
|
|
|
|
|
|
|
api_id (:obj:`int` | :obj:`str`):
|
|
|
|
The API ID you obtained from https://my.telegram.org.
|
|
|
|
|
|
|
|
api_hash (:obj:`str`):
|
|
|
|
The API ID you obtained from https://my.telegram.org.
|
|
|
|
|
|
|
|
connection_mode (:obj:`ConnectionMode`, optional):
|
|
|
|
The connection mode to be used when creating a new connection
|
|
|
|
to the servers. Defaults to the ``TCP_FULL`` mode.
|
|
|
|
This will only affect how messages are sent over the network
|
|
|
|
and how much processing is required before sending them.
|
|
|
|
|
|
|
|
use_ipv6 (:obj:`bool`, optional):
|
|
|
|
Whether to connect to the servers through IPv6 or not.
|
|
|
|
By default this is ``False`` as IPv6 support is not
|
|
|
|
too widespread yet.
|
|
|
|
|
|
|
|
proxy (:obj:`tuple` | :obj:`dict`, optional):
|
|
|
|
A tuple consisting of ``(socks.SOCKS5, 'host', port)``.
|
|
|
|
See https://github.com/Anorov/PySocks#usage-1 for more.
|
|
|
|
|
|
|
|
update_workers (:obj:`int`, optional):
|
|
|
|
If specified, represents how many extra threads should
|
|
|
|
be spawned to handle incoming updates, and updates will
|
|
|
|
be kept in memory until they are processed. Note that
|
|
|
|
you must set this to at least ``0`` if you want to be
|
|
|
|
able to process updates through :meth:`updates.poll()`.
|
|
|
|
|
|
|
|
timeout (:obj:`int` | :obj:`float` | :obj:`timedelta`, optional):
|
|
|
|
The timeout to be used when receiving responses from
|
|
|
|
the network. Defaults to 5 seconds.
|
|
|
|
|
|
|
|
spawn_read_thread (:obj:`bool`, optional):
|
|
|
|
Whether to use an extra background thread or not. Defaults
|
|
|
|
to ``True`` so receiving items from the network happens
|
|
|
|
instantly, as soon as they arrive. Can still be disabled
|
|
|
|
if you want to run the library without any additional thread.
|
|
|
|
|
|
|
|
Kwargs:
|
|
|
|
Extra parameters will be forwarded to the ``Session`` file.
|
|
|
|
Most relevant parameters are:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
device_model = platform.node()
|
|
|
|
system_version = platform.system()
|
|
|
|
app_version = TelegramClient.__version__
|
|
|
|
lang_code = 'en'
|
|
|
|
system_lang_code = lang_code
|
|
|
|
report_errors = True
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
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,
|
2017-11-16 15:30:18 +03:00
|
|
|
use_ipv6=False,
|
2017-09-04 12:24:10 +03:00
|
|
|
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):
|
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,
|
2017-11-16 15:30:18 +03:00
|
|
|
use_ipv6=use_ipv6,
|
2017-09-07 19:49:08 +03:00
|
|
|
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-11-04 22:46:02 +03:00
|
|
|
def send_code_request(self, phone, force_sms=False):
|
2018-01-05 15:30:21 +03:00
|
|
|
"""
|
|
|
|
Sends a code request to the specified phone number.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
phone (:obj:`str` | :obj:`int`):
|
|
|
|
The phone to which the code will be sent.
|
|
|
|
|
|
|
|
force_sms (:obj:`bool`, optional):
|
|
|
|
Whether to force sending as SMS.
|
2017-10-21 17:59:20 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Returns:
|
2017-11-04 22:46:02 +03:00
|
|
|
Information about the result of the request.
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
2017-12-27 02:50:09 +03:00
|
|
|
phone = utils.parse_phone(phone) or self._phone
|
TelegramClient.send_code_request(): Change logic of methods invocation
Before:
First call, force_sms=False: SendCodeRequest
Next call, force_sms=False: SendCodeRequest
First call, force_sms=True: raise ValueError
Next call, force_sms=True: ResendCodeRequest
That's inconvenient because the user must remember whether the code requested at all and whether the request was successful.
In addition, the repeated invocation of SendCodeRequest does nothing.
This commit changes logic to this:
First call, force_sms=False: SendCodeRequest
Next call, force_sms=False: ResendCodeRequest
First call, force_sms=True: SendCodeRequest, ResendCodeRequest
Next call, force_sms=True: ResendCodeRequest
2017-12-24 14:21:14 +03:00
|
|
|
|
|
|
|
if not self._phone_code_hash:
|
2017-11-04 22:46:02 +03:00
|
|
|
result = self(SendCodeRequest(phone, self.api_id, self.api_hash))
|
|
|
|
self._phone_code_hash = result.phone_code_hash
|
TelegramClient.send_code_request(): Change logic of methods invocation
Before:
First call, force_sms=False: SendCodeRequest
Next call, force_sms=False: SendCodeRequest
First call, force_sms=True: raise ValueError
Next call, force_sms=True: ResendCodeRequest
That's inconvenient because the user must remember whether the code requested at all and whether the request was successful.
In addition, the repeated invocation of SendCodeRequest does nothing.
This commit changes logic to this:
First call, force_sms=False: SendCodeRequest
Next call, force_sms=False: ResendCodeRequest
First call, force_sms=True: SendCodeRequest, ResendCodeRequest
Next call, force_sms=True: ResendCodeRequest
2017-12-24 14:21:14 +03:00
|
|
|
else:
|
|
|
|
force_sms = True
|
2017-11-04 22:46:02 +03:00
|
|
|
|
2017-08-31 11:34:09 +03:00
|
|
|
self._phone = phone
|
TelegramClient.send_code_request(): Change logic of methods invocation
Before:
First call, force_sms=False: SendCodeRequest
Next call, force_sms=False: SendCodeRequest
First call, force_sms=True: raise ValueError
Next call, force_sms=True: ResendCodeRequest
That's inconvenient because the user must remember whether the code requested at all and whether the request was successful.
In addition, the repeated invocation of SendCodeRequest does nothing.
This commit changes logic to this:
First call, force_sms=False: SendCodeRequest
Next call, force_sms=False: ResendCodeRequest
First call, force_sms=True: SendCodeRequest, ResendCodeRequest
Next call, force_sms=True: ResendCodeRequest
2017-12-24 14:21:14 +03:00
|
|
|
|
|
|
|
if force_sms:
|
|
|
|
result = self(ResendCodeRequest(phone, self._phone_code_hash))
|
|
|
|
self._phone_code_hash = result.phone_code_hash
|
|
|
|
|
2017-08-31 11:34:09 +03:00
|
|
|
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-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Starts or completes the sign in process with the given phone number
|
|
|
|
or code that Telegram sent.
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
phone (:obj:`str` | :obj:`int`):
|
|
|
|
The phone to send the code to if no code was provided,
|
|
|
|
or to override the phone that was previously used with
|
|
|
|
these requests.
|
|
|
|
|
|
|
|
code (:obj:`str` | :obj:`int`):
|
|
|
|
The code that Telegram sent.
|
|
|
|
|
|
|
|
password (:obj:`str`):
|
|
|
|
2FA password, should be used if a previous call raised
|
|
|
|
SessionPasswordNeededError.
|
|
|
|
|
|
|
|
bot_token (:obj:`str`):
|
|
|
|
Used to sign in as a bot. Not all requests will be available.
|
|
|
|
This should be the hash the @BotFather gave you.
|
|
|
|
|
|
|
|
phone_code_hash (:obj:`str`):
|
|
|
|
The hash returned by .send_code_request. This can be set to None
|
|
|
|
to use the last hash known.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The signed in user, or the information about
|
|
|
|
:meth:`.send_code_request()`.
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
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-12-27 02:50:09 +03:00
|
|
|
phone = utils.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=''):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Signs up to Telegram if you don't have an account yet.
|
|
|
|
You must call .send_code_request(phone) first.
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
code (:obj:`str` | :obj:`int`):
|
|
|
|
The code sent by Telegram
|
|
|
|
|
|
|
|
first_name (:obj:`str`):
|
|
|
|
The first name to be used by the new account.
|
|
|
|
|
|
|
|
last_name (:obj:`str`, optional)
|
|
|
|
Optional last name.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The new created user.
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
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):
|
2018-01-05 15:30:21 +03:00
|
|
|
"""
|
|
|
|
Logs out Telegram and deletes the current *.session file.
|
2017-10-21 17:59:20 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Returns:
|
|
|
|
True if the operation was successful.
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
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):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Gets "me" (the self user) which is currently authenticated,
|
|
|
|
or None if the request fails (hence, not authenticated).
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Returns:
|
|
|
|
Your own user.
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
2017-06-04 18:24:08 +03:00
|
|
|
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
|
|
|
|
|
2018-01-05 20:31:48 +03:00
|
|
|
def get_dialogs(self, limit=10, offset_date=None, offset_id=0,
|
2016-11-30 00:29:42 +03:00
|
|
|
offset_peer=InputPeerEmpty()):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Gets N "dialogs" (open "chats" or conversations with other people).
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
limit (:obj:`int` | :obj:`None`):
|
|
|
|
How many dialogs to be retrieved as maximum. Can be set to
|
|
|
|
``None`` to retrieve all dialogs. Note that this may take
|
|
|
|
whole minutes if you have hundreds of dialogs, as Telegram
|
|
|
|
will tell the library to slow down through a
|
|
|
|
``FloodWaitError``.
|
|
|
|
|
|
|
|
offset_date (:obj:`datetime`, optional):
|
|
|
|
The offset date to be used.
|
|
|
|
|
|
|
|
offset_id (:obj:`int`, optional):
|
|
|
|
The message ID to be used as an offset.
|
|
|
|
|
|
|
|
offset_peer (:obj:`InputPeer`, optional):
|
|
|
|
The peer to be used as an offset.
|
|
|
|
|
|
|
|
Returns:
|
2017-12-28 13:49:35 +03:00
|
|
|
A list dialogs, with an additional .total attribute on the list.
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
2017-11-04 14:34:44 +03:00
|
|
|
limit = float('inf') if limit is None else int(limit)
|
|
|
|
if limit == 0:
|
2017-12-28 13:49:35 +03:00
|
|
|
# Special case, get a single dialog and determine count
|
|
|
|
dialogs = self(GetDialogsRequest(
|
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
offset_peer=offset_peer,
|
|
|
|
limit=1
|
|
|
|
))
|
|
|
|
result = UserList()
|
|
|
|
result.total = getattr(dialogs, 'count', len(dialogs.dialogs))
|
|
|
|
return result
|
2017-10-01 11:50:37 +03:00
|
|
|
|
2017-12-28 13:49:35 +03:00
|
|
|
total_count = 0
|
2017-12-24 18:18:09 +03:00
|
|
|
dialogs = OrderedDict() # Use peer id as identifier to avoid dupes
|
2017-10-01 11:50:37 +03:00
|
|
|
while len(dialogs) < limit:
|
2017-11-04 14:34:44 +03:00
|
|
|
real_limit = min(limit - len(dialogs), 100)
|
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-11-04 14:34:44 +03:00
|
|
|
limit=real_limit
|
2017-10-01 11:50:37 +03:00
|
|
|
))
|
|
|
|
|
2017-12-28 13:49:35 +03:00
|
|
|
total_count = getattr(r, 'count', len(r.dialogs))
|
2017-12-24 18:18:09 +03:00
|
|
|
messages = {m.id: m for m in r.messages}
|
2017-12-28 15:31:43 +03:00
|
|
|
entities = {utils.get_peer_id(x): x
|
2017-12-24 18:18:09 +03:00
|
|
|
for x in itertools.chain(r.users, r.chats)}
|
2017-10-01 11:50:37 +03:00
|
|
|
|
2017-12-24 18:18:09 +03:00
|
|
|
for d in r.dialogs:
|
2017-12-28 15:31:43 +03:00
|
|
|
dialogs[utils.get_peer_id(d.peer)] = \
|
2017-12-24 18:18:09 +03:00
|
|
|
Dialog(self, d, entities, messages)
|
2017-10-01 11:50:37 +03:00
|
|
|
|
2017-11-04 14:34:44 +03:00
|
|
|
if len(r.dialogs) < real_limit or not isinstance(r, DialogsSlice):
|
|
|
|
# Less than we requested means we reached the end, or
|
|
|
|
# we didn't get a DialogsSlice which means we got all.
|
2017-10-01 11:50:37 +03:00
|
|
|
break
|
|
|
|
|
|
|
|
offset_date = r.messages[-1].date
|
2017-12-28 15:31:43 +03:00
|
|
|
offset_peer = entities[utils.get_peer_id(r.dialogs[-1].peer)]
|
2017-10-01 11:50:37 +03:00
|
|
|
offset_id = r.messages[-1].id & 4294967296 # Telegram/danog magic
|
|
|
|
|
2017-12-28 16:55:02 +03:00
|
|
|
dialogs = UserList(
|
|
|
|
itertools.islice(dialogs.values(), min(limit, len(dialogs)))
|
|
|
|
)
|
2017-12-28 13:49:35 +03:00
|
|
|
dialogs.total = total_count
|
2017-12-28 16:55:02 +03:00
|
|
|
return dialogs
|
2016-09-06 19:54:49 +03:00
|
|
|
|
2017-10-09 10:54:48 +03:00
|
|
|
def get_drafts(self): # TODO: Ability to provide a `filter`
|
|
|
|
"""
|
|
|
|
Gets all open draft messages.
|
2016-09-11 17:24:03 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Returns:
|
|
|
|
A list of custom ``Draft`` objects that are easy to work with:
|
|
|
|
You can call :meth:`draft.set_message('text')` to change the message,
|
|
|
|
or delete it through :meth:`draft.delete()`.
|
2017-10-09 10:54:48 +03:00
|
|
|
"""
|
|
|
|
response = self(GetAllDraftsRequest())
|
|
|
|
self.session.process_entities(response)
|
|
|
|
self.session.generate_sequence(response.seq)
|
|
|
|
drafts = [Draft._from_update(self, u) for u in response.updates]
|
|
|
|
return drafts
|
2016-09-11 17:24:03 +03:00
|
|
|
|
2018-01-02 15:30:29 +03:00
|
|
|
@staticmethod
|
|
|
|
def _get_response_message(request, result):
|
2018-01-05 15:30:21 +03:00
|
|
|
"""Extracts the response message known a request and Update result"""
|
2018-01-02 15:30:29 +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, (UpdateNewChannelMessage, UpdateNewMessage)):
|
|
|
|
if update.message.id == msg_id:
|
|
|
|
return update.message
|
|
|
|
|
2018-01-05 20:31:48 +03:00
|
|
|
def send_message(self, entity, message, reply_to=None, parse_mode=None,
|
2017-07-07 11:37:19 +03:00
|
|
|
link_preview=True):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Sends the given message to the specified entity (user/chat/channel).
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
entity (:obj:`entity`):
|
|
|
|
To who will it be sent.
|
|
|
|
|
|
|
|
message (:obj:`str`):
|
|
|
|
The message to be sent.
|
|
|
|
|
|
|
|
reply_to (:obj:`int` | :obj:`Message`, optional):
|
|
|
|
Whether to reply to a message or not. If an integer is provided,
|
|
|
|
it should be the ID of the message that it should reply to.
|
|
|
|
|
|
|
|
parse_mode (:obj:`str`, optional):
|
|
|
|
Can be 'md' or 'markdown' for markdown-like parsing, in a similar
|
|
|
|
fashion how official clients work.
|
|
|
|
|
|
|
|
link_preview (:obj:`bool`, optional):
|
|
|
|
Should the link preview be shown?
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
the sent message
|
2017-08-23 01:01:10 +03:00
|
|
|
"""
|
2017-10-01 17:54:11 +03:00
|
|
|
entity = self.get_input_entity(entity)
|
2017-10-30 13:17:22 +03:00
|
|
|
if parse_mode:
|
|
|
|
parse_mode = parse_mode.lower()
|
|
|
|
if parse_mode in {'md', 'markdown'}:
|
2017-11-10 13:01:02 +03:00
|
|
|
message, msg_entities = markdown.parse(message)
|
2017-10-30 13:17:22 +03:00
|
|
|
else:
|
2017-12-28 02:22:28 +03:00
|
|
|
raise ValueError('Unknown parsing mode: {}'.format(parse_mode))
|
2017-10-30 13:17:22 +03:00
|
|
|
else:
|
|
|
|
msg_entities = []
|
|
|
|
|
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,
|
2017-10-30 13:17:22 +03:00
|
|
|
entities=msg_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
|
|
|
|
)
|
|
|
|
|
2018-01-02 15:30:29 +03:00
|
|
|
return self._get_response_message(request, result)
|
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):
|
|
|
|
"""
|
2018-01-05 15:30:21 +03:00
|
|
|
Deletes a message from a chat, optionally "for everyone".
|
|
|
|
|
|
|
|
Args:
|
|
|
|
entity (:obj:`entity`):
|
|
|
|
From who the message will be deleted. This can actually
|
|
|
|
be ``None`` for normal chats, but **must** be present
|
|
|
|
for channels and megagroups.
|
2017-10-02 19:59:29 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
message_ids (:obj:`list` | :obj:`int` | :obj:`Message`):
|
|
|
|
The IDs (or ID) or messages to be deleted.
|
2017-10-02 19:59:29 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
revoke (:obj:`bool`, optional):
|
|
|
|
Whether the message should be deleted for everyone or not.
|
|
|
|
By default it has the opposite behaviour of official clients,
|
|
|
|
and it will delete the message for everyone.
|
|
|
|
This has no effect on channels or megagroups.
|
2017-10-02 19:59:29 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Returns:
|
|
|
|
The affected messages.
|
2017-10-02 19:59:29 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
2018-01-05 20:31:48 +03:00
|
|
|
def get_message_history(self, entity, 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
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
entity (:obj:`entity`):
|
|
|
|
The entity from whom to retrieve the message history.
|
|
|
|
|
|
|
|
limit (:obj:`int` | :obj:`None`, optional):
|
|
|
|
Number of messages to be retrieved. Due to limitations with
|
|
|
|
the API retrieving more than 3000 messages will take longer
|
|
|
|
than half a minute (or even more based on previous calls).
|
|
|
|
The limit may also be ``None``, which would eventually return
|
|
|
|
the whole history.
|
|
|
|
|
|
|
|
offset_date (:obj:`datetime`):
|
|
|
|
Offset date (messages *previous* to this date will be
|
|
|
|
retrieved). Exclusive.
|
|
|
|
|
|
|
|
offset_id (:obj:`int`):
|
|
|
|
Offset message ID (only messages *previous* to the given
|
|
|
|
ID will be retrieved). Exclusive.
|
|
|
|
|
|
|
|
max_id (:obj:`int`):
|
|
|
|
All the messages with a higher (newer) ID or equal to this will
|
|
|
|
be excluded
|
|
|
|
|
|
|
|
min_id (:obj:`int`):
|
|
|
|
All the messages with a lower (older) ID or equal to this will
|
|
|
|
be excluded.
|
|
|
|
|
|
|
|
add_offset (:obj:`int`):
|
|
|
|
Additional message offset (all of the specified offsets +
|
|
|
|
this offset = older messages).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of messages with extra attributes:
|
|
|
|
|
|
|
|
* ``.total`` = (on the list) total amount of messages sent.
|
|
|
|
* ``.sender`` = entity of the sender.
|
|
|
|
* ``.fwd_from.sender`` = if fwd_from, who sent it originally.
|
|
|
|
* ``.fwd_from.channel`` = if fwd_from, original channel.
|
|
|
|
* ``.to`` = entity to which the message was sent.
|
2016-09-08 13:13:31 +03:00
|
|
|
"""
|
2017-10-31 15:52:43 +03:00
|
|
|
entity = self.get_input_entity(entity)
|
2017-10-31 14:48:55 +03:00
|
|
|
limit = float('inf') if limit is None else int(limit)
|
2017-10-31 15:52:43 +03:00
|
|
|
if limit == 0:
|
|
|
|
# No messages, but we still need to know the total message count
|
|
|
|
result = self(GetHistoryRequest(
|
2017-11-16 15:24:32 +03:00
|
|
|
peer=entity, limit=1,
|
2017-10-31 15:52:43 +03:00
|
|
|
offset_date=None, offset_id=0, max_id=0, min_id=0, add_offset=0
|
|
|
|
))
|
|
|
|
return getattr(result, 'count', len(result.messages)), [], []
|
|
|
|
|
2017-10-31 14:48:55 +03:00
|
|
|
total_messages = 0
|
2017-12-28 13:49:35 +03:00
|
|
|
messages = UserList()
|
2017-10-31 14:48:55 +03:00
|
|
|
entities = {}
|
|
|
|
while len(messages) < limit:
|
|
|
|
# Telegram has a hard limit of 100
|
|
|
|
real_limit = min(limit - len(messages), 100)
|
|
|
|
result = self(GetHistoryRequest(
|
2017-10-31 15:52:43 +03:00
|
|
|
peer=entity,
|
2017-10-31 14:48:55 +03:00
|
|
|
limit=real_limit,
|
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
max_id=max_id,
|
|
|
|
min_id=min_id,
|
2018-01-03 21:18:24 +03:00
|
|
|
add_offset=add_offset,
|
|
|
|
hash=0
|
2017-10-31 14:48:55 +03:00
|
|
|
))
|
|
|
|
messages.extend(
|
|
|
|
m for m in result.messages if not isinstance(m, MessageEmpty)
|
|
|
|
)
|
|
|
|
total_messages = getattr(result, 'count', len(result.messages))
|
2016-09-08 13:13:31 +03:00
|
|
|
|
2017-10-31 14:48:55 +03:00
|
|
|
# TODO We can potentially use self.session.database, but since
|
|
|
|
# it might be disabled, use a local dictionary.
|
|
|
|
for u in result.users:
|
2017-12-28 15:31:43 +03:00
|
|
|
entities[utils.get_peer_id(u)] = u
|
2017-10-31 14:48:55 +03:00
|
|
|
for c in result.chats:
|
2017-12-28 15:31:43 +03:00
|
|
|
entities[utils.get_peer_id(c)] = c
|
2016-09-11 11:35:02 +03:00
|
|
|
|
2017-10-31 14:48:55 +03:00
|
|
|
if len(result.messages) < real_limit:
|
|
|
|
break
|
2017-10-01 17:57:07 +03:00
|
|
|
|
2017-10-31 14:48:55 +03:00
|
|
|
offset_id = result.messages[-1].id
|
|
|
|
offset_date = result.messages[-1].date
|
|
|
|
|
|
|
|
# Telegram limit seems to be 3000 messages within 30 seconds in
|
|
|
|
# batches of 100 messages each request (since the FloodWait was
|
|
|
|
# of 30 seconds). If the limit is greater than that, we will
|
|
|
|
# sleep 1s between each request.
|
|
|
|
if limit > 3000:
|
|
|
|
time.sleep(1)
|
|
|
|
|
2017-12-28 04:01:22 +03:00
|
|
|
# Add a few extra attributes to the Message to make it friendlier.
|
2017-12-28 13:49:35 +03:00
|
|
|
messages.total = total_messages
|
2017-10-31 14:48:55 +03:00
|
|
|
for m in messages:
|
2017-12-28 04:01:22 +03:00
|
|
|
# TODO Better way to return a total without tuples?
|
|
|
|
m.sender = (None if not m.from_id else
|
2017-12-28 15:31:43 +03:00
|
|
|
entities[utils.get_peer_id(m.from_id)])
|
2017-12-28 04:01:22 +03:00
|
|
|
|
|
|
|
if getattr(m, 'fwd_from', None):
|
|
|
|
m.fwd_from.sender = (
|
|
|
|
None if not m.fwd_from.from_id else
|
2017-12-28 15:31:43 +03:00
|
|
|
entities[utils.get_peer_id(m.fwd_from.from_id)]
|
2017-12-28 04:01:22 +03:00
|
|
|
)
|
|
|
|
m.fwd_from.channel = (
|
|
|
|
None if not m.fwd_from.channel_id else
|
|
|
|
entities[utils.get_peer_id(
|
2017-12-28 15:31:43 +03:00
|
|
|
PeerChannel(m.fwd_from.channel_id)
|
2017-12-28 04:01:22 +03:00
|
|
|
)]
|
|
|
|
)
|
|
|
|
|
2017-12-28 15:31:43 +03:00
|
|
|
m.to = entities[utils.get_peer_id(m.to_id)]
|
2017-06-14 15:06:35 +03:00
|
|
|
|
2017-12-28 04:01:22 +03:00
|
|
|
return messages
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-12-27 17:26:23 +03:00
|
|
|
def send_read_acknowledge(self, entity, message=None, max_id=None):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Sends a "read acknowledge" (i.e., notifying the given peer that we've
|
|
|
|
read their messages, also known as the "double check").
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
entity (:obj:`entity`):
|
|
|
|
The chat where these messages are located.
|
|
|
|
|
|
|
|
message (:obj:`list` | :obj:`Message`):
|
|
|
|
Either a list of messages or a single message.
|
|
|
|
|
|
|
|
max_id (:obj:`int`):
|
|
|
|
Overrides messages, until which message should the
|
|
|
|
acknowledge should be sent.
|
2017-08-23 01:01:10 +03:00
|
|
|
"""
|
2016-10-02 14:57:03 +03:00
|
|
|
if max_id is None:
|
|
|
|
if not messages:
|
2017-12-28 02:22:28 +03:00
|
|
|
raise ValueError(
|
2016-11-30 00:29:42 +03:00
|
|
|
'Either a message list or a max_id must be provided.')
|
2016-10-02 14:57:03 +03:00
|
|
|
|
2017-12-27 17:26:23 +03:00
|
|
|
if hasattr(message, '__iter__'):
|
|
|
|
max_id = max(msg.id for msg in message)
|
2016-10-02 14:57:03 +03:00
|
|
|
else:
|
2017-12-27 17:26:23 +03:00
|
|
|
max_id = message.id
|
2016-10-02 14:57:03 +03:00
|
|
|
|
2017-12-27 17:26:23 +03:00
|
|
|
entity = self.get_input_entity(entity)
|
|
|
|
if entity == InputPeerChannel:
|
|
|
|
return self(channels.ReadHistoryRequest(entity, max_id=max_id))
|
|
|
|
else:
|
|
|
|
return self(messages.ReadHistoryRequest(entity, 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
|
|
|
|
|
2017-12-28 02:22:28 +03:00
|
|
|
raise TypeError('Invalid reply_to type: {}'.format(type(reply_to)))
|
2017-09-13 12:51:23 +03:00
|
|
|
|
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-10-09 12:20:09 +03:00
|
|
|
attributes=None,
|
2018-01-03 14:47:38 +03:00
|
|
|
thumb=None,
|
2017-09-13 12:30:20 +03:00
|
|
|
**kwargs):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Sends a file to the specified entity.
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
entity (:obj:`entity`):
|
|
|
|
Who will receive the file.
|
|
|
|
|
|
|
|
file (:obj:`str` | :obj:`bytes` | :obj:`file`):
|
|
|
|
The path of the file, byte array, or stream that will be sent.
|
|
|
|
Note that if a byte array or a stream is given, a filename
|
|
|
|
or its type won't be inferred, and it will be sent as an
|
|
|
|
"unnamed application/octet-stream".
|
|
|
|
|
|
|
|
Subsequent calls with the very same file will result in
|
|
|
|
immediate uploads, unless ``.clear_file_cache()`` is called.
|
|
|
|
|
|
|
|
caption (:obj:`str`, optional):
|
|
|
|
Optional caption for the sent media message.
|
|
|
|
|
|
|
|
force_document (:obj:`bool`, optional):
|
|
|
|
If left to ``False`` and the file is a path that ends with
|
|
|
|
``.png``, ``.jpg`` and such, the file will be sent as a photo.
|
|
|
|
Otherwise always as a document.
|
|
|
|
|
|
|
|
progress_callback (:obj:`callable`, optional):
|
|
|
|
A callback function accepting two parameters:
|
|
|
|
``(sent bytes, total)``.
|
|
|
|
|
|
|
|
reply_to (:obj:`int` | :obj:`Message`):
|
|
|
|
Same as reply_to from .send_message().
|
|
|
|
|
|
|
|
attributes (:obj:`list`, optional):
|
|
|
|
Optional attributes that override the inferred ones, like
|
|
|
|
``DocumentAttributeFilename`` and so on.
|
|
|
|
|
|
|
|
thumb (:obj:`str` | :obj:`bytes` | :obj:`file`):
|
|
|
|
Optional thumbnail (for videos).
|
|
|
|
|
|
|
|
Kwargs:
|
2017-09-13 12:51:23 +03:00
|
|
|
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.
|
2018-01-05 15:30:21 +03:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
The message containing the sent file.
|
2017-08-23 01:01:10 +03:00
|
|
|
"""
|
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')
|
|
|
|
)
|
|
|
|
|
2018-01-05 17:33:25 +03:00
|
|
|
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]
|
2017-10-09 12:20:09 +03:00
|
|
|
attr_dict = {
|
|
|
|
DocumentAttributeFilename:
|
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
|
2017-10-09 12:20:09 +03:00
|
|
|
}
|
2017-08-23 01:27:33 +03:00
|
|
|
else:
|
2017-10-09 12:20:09 +03:00
|
|
|
attr_dict = {
|
|
|
|
DocumentAttributeFilename:
|
|
|
|
DocumentAttributeFilename('unnamed')
|
|
|
|
}
|
2017-08-23 01:27:33 +03:00
|
|
|
|
2017-09-13 12:30:20 +03:00
|
|
|
if 'is_voice_note' in kwargs:
|
2017-10-09 12:20:09 +03:00
|
|
|
attr_dict[DocumentAttributeAudio] = \
|
|
|
|
DocumentAttributeAudio(0, voice=True)
|
|
|
|
|
|
|
|
# Now override the attributes if any. As we have a dict of
|
|
|
|
# {cls: instance}, we can override any class with the list
|
|
|
|
# of attributes provided by the user easily.
|
|
|
|
if attributes:
|
|
|
|
for a in attributes:
|
|
|
|
attr_dict[type(a)] = a
|
2017-09-13 12:30:20 +03:00
|
|
|
|
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'
|
|
|
|
|
2018-01-03 14:47:38 +03:00
|
|
|
input_kw = {}
|
|
|
|
if thumb:
|
|
|
|
input_kw['thumb'] = self.upload_file(thumb)
|
|
|
|
|
2017-08-23 01:27:33 +03:00
|
|
|
media = InputMediaUploadedDocument(
|
|
|
|
file=file_handle,
|
2016-11-30 00:29:42 +03:00
|
|
|
mime_type=mime_type,
|
2017-10-09 12:20:09 +03:00
|
|
|
attributes=list(attr_dict.values()),
|
2018-01-03 14:47:38 +03:00
|
|
|
caption=caption,
|
|
|
|
**input_kw
|
2017-08-23 01:27:33 +03:00
|
|
|
)
|
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.
|
2018-01-02 15:30:29 +03:00
|
|
|
request = 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)
|
2018-01-02 15:30:29 +03:00
|
|
|
)
|
|
|
|
result = self(request)
|
|
|
|
|
|
|
|
return self._get_response_message(request, result)
|
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
|
|
|
|
|
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-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Downloads the profile photo of the given entity (user/chat/channel).
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Args:
|
|
|
|
entity (:obj:`entity`):
|
|
|
|
From who the photo will be downloaded.
|
|
|
|
|
|
|
|
file (:obj:`str` | :obj:`file`, optional):
|
|
|
|
The output file path, directory, or stream-like object.
|
|
|
|
If the path exists and is a file, it will be overwritten.
|
|
|
|
|
|
|
|
download_big (:obj:`bool`, optional):
|
|
|
|
Whether to use the big version of the available photos.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
``None`` if no photo was provided, or if it was Empty. On success
|
2017-10-21 17:59:20 +03:00
|
|
|
the file path is returned since it may differ from the one given.
|
2017-08-23 02:35:12 +03:00
|
|
|
"""
|
2017-12-28 01:54:31 +03:00
|
|
|
photo = entity
|
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
|
|
|
|
2017-12-28 01:54:31 +03:00
|
|
|
photo = entity.photo
|
2017-08-23 02:35:12 +03:00
|
|
|
|
2017-12-28 01:54:31 +03:00
|
|
|
if not isinstance(photo, UserProfilePhoto) and \
|
|
|
|
not isinstance(photo, ChatPhoto):
|
2017-08-23 02:35:12 +03:00
|
|
|
return None
|
|
|
|
|
2017-12-28 01:54:31 +03:00
|
|
|
photo_location = photo.photo_big if download_big else photo.photo_small
|
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-12-28 01:54:31 +03:00
|
|
|
try:
|
|
|
|
self.download_file(
|
|
|
|
InputFileLocation(
|
|
|
|
volume_id=photo_location.volume_id,
|
|
|
|
local_id=photo_location.local_id,
|
|
|
|
secret=photo_location.secret
|
|
|
|
),
|
|
|
|
file
|
|
|
|
)
|
|
|
|
except LocationInvalidError:
|
|
|
|
# See issue #500, Android app fails as of v4.6.0 (1155).
|
|
|
|
# The fix seems to be using the full channel chat photo.
|
|
|
|
ie = self.get_input_entity(entity)
|
|
|
|
if isinstance(ie, InputPeerChannel):
|
|
|
|
full = self(GetFullChannelRequest(ie))
|
|
|
|
return self._download_photo(
|
|
|
|
full.full_chat.chat_photo, file,
|
|
|
|
date=None, progress_callback=None
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Until there's a report for chats, no need to.
|
|
|
|
return None
|
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-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Downloads the given media, or the media from a specified Message.
|
2018-01-05 15:30:21 +03:00
|
|
|
|
|
|
|
message (:obj:`Message` | :obj:`Media`):
|
2017-10-21 17:59:20 +03:00
|
|
|
The media or message containing the media that will be downloaded.
|
2018-01-05 15:30:21 +03:00
|
|
|
|
|
|
|
file (:obj:`str` | :obj:`file`, optional):
|
2017-10-21 17:59:20 +03:00
|
|
|
The output file path, directory, or stream-like object.
|
|
|
|
If the path exists and is a file, it will be overwritten.
|
2018-01-05 15:30:21 +03:00
|
|
|
|
|
|
|
progress_callback (:obj:`callable`, optional):
|
|
|
|
A callback function accepting two parameters:
|
|
|
|
``(recv bytes, total)``.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
``None`` if no media was provided, or if it was Empty. On success
|
|
|
|
the file path is returned since it may differ from the one given.
|
2017-07-23 19:38:27 +03:00
|
|
|
"""
|
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-12-28 14:11:31 +03:00
|
|
|
if isinstance(media, (MessageMediaPhoto, Photo)):
|
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-12-28 14:11:31 +03:00
|
|
|
def _download_photo(self, 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-12-28 14:11:31 +03:00
|
|
|
if isinstance(photo, MessageMediaPhoto):
|
|
|
|
photo = photo.photo
|
|
|
|
if not isinstance(photo, Photo):
|
|
|
|
return
|
|
|
|
|
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:
|
2017-10-28 12:09:46 +03:00
|
|
|
# Remove these pesky characters
|
2017-10-28 12:11:51 +03:00
|
|
|
first_name = first_name.replace(';', '')
|
|
|
|
last_name = (last_name or '').replace(';', '')
|
2017-07-23 18:08:04 +03:00
|
|
|
f.write('BEGIN:VCARD\n')
|
|
|
|
f.write('VERSION:4.0\n')
|
2017-10-29 22:10:29 +03:00
|
|
|
f.write('N:{};{};;;\n'.format(first_name, last_name))
|
2017-10-28 12:11:51 +03:00
|
|
|
f.write('FN:{} {}\n'.format(first_name, last_name))
|
2017-10-29 22:10:29 +03:00
|
|
|
f.write('TEL;TYPE=cell;VALUE=uri:tel:+{}\n'.format(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:
|
2017-12-27 16:52:33 +03:00
|
|
|
if not date:
|
|
|
|
date = datetime.now()
|
2017-08-24 18:44:38 +03:00
|
|
|
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-12-27 13:56:05 +03:00
|
|
|
def get_entity(self, entity):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Turns the given entity into a valid Telegram user or chat.
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
entity (:obj:`str` | :obj:`int` | :obj:`Peer` | :obj:`InputPeer`):
|
2017-12-27 14:36:38 +03:00
|
|
|
The entity (or iterable of entities) to be transformed.
|
2017-10-21 17:59:20 +03:00
|
|
|
If it's a string which can be converted to an integer or starts
|
|
|
|
with '+' it will be resolved as if it were a phone number.
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-10-21 17:59:20 +03:00
|
|
|
If it doesn't start with '+' or starts with a '@' it will be
|
|
|
|
be resolved from the username. If no exact match is returned,
|
|
|
|
an error will be raised.
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-10-21 17:59:20 +03:00
|
|
|
If the entity is an integer or a Peer, its information will be
|
|
|
|
returned through a call to self.get_input_peer(entity).
|
2017-10-01 15:19:04 +03:00
|
|
|
|
2017-10-21 17:59:20 +03:00
|
|
|
If the entity is neither, and it's not a TLObject, an
|
|
|
|
error will be raised.
|
2017-11-20 12:58:11 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Returns:
|
|
|
|
``User``, ``Chat`` or ``Channel`` corresponding to the input
|
|
|
|
entity.
|
2017-08-23 01:01:10 +03:00
|
|
|
"""
|
2017-12-27 14:36:38 +03:00
|
|
|
if not isinstance(entity, str) and hasattr(entity, '__iter__'):
|
|
|
|
single = False
|
|
|
|
else:
|
|
|
|
single = True
|
|
|
|
entity = (entity,)
|
|
|
|
|
|
|
|
# Group input entities by string (resolve username),
|
|
|
|
# input users (get users), input chat (get chats) and
|
|
|
|
# input channels (get channels) to get the most entities
|
|
|
|
# in the less amount of calls possible.
|
|
|
|
inputs = [
|
|
|
|
x if isinstance(x, str) else self.get_input_entity(x)
|
|
|
|
for x in entity
|
|
|
|
]
|
|
|
|
users = [x for x in inputs if isinstance(x, InputPeerUser)]
|
|
|
|
chats = [x.chat_id for x in inputs if isinstance(x, InputPeerChat)]
|
|
|
|
channels = [x for x in inputs if isinstance(x, InputPeerChannel)]
|
|
|
|
if users:
|
2017-12-27 17:08:29 +03:00
|
|
|
# GetUsersRequest has a limit of 200 per call
|
|
|
|
tmp = []
|
|
|
|
while users:
|
|
|
|
curr, users = users[:200], users[200:]
|
|
|
|
tmp.extend(self(GetUsersRequest(curr)))
|
|
|
|
users = tmp
|
2017-12-27 14:36:38 +03:00
|
|
|
if chats: # TODO Handle chats slice?
|
|
|
|
chats = self(GetChatsRequest(chats)).chats
|
|
|
|
if channels:
|
|
|
|
channels = self(GetChannelsRequest(channels)).chats
|
|
|
|
|
|
|
|
# Merge users, chats and channels into a single dictionary
|
|
|
|
id_entity = {
|
2017-12-28 15:31:43 +03:00
|
|
|
utils.get_peer_id(x): x
|
2017-12-27 14:36:38 +03:00
|
|
|
for x in itertools.chain(users, chats, channels)
|
|
|
|
}
|
|
|
|
|
|
|
|
# We could check saved usernames and put them into the users,
|
|
|
|
# chats and channels list from before. While this would reduce
|
|
|
|
# the amount of ResolveUsername calls, it would fail to catch
|
|
|
|
# username changes.
|
|
|
|
result = [
|
|
|
|
self._get_entity_from_string(x) if isinstance(x, str)
|
2017-12-28 15:31:43 +03:00
|
|
|
else id_entity[utils.get_peer_id(x)]
|
2017-12-27 14:36:38 +03:00
|
|
|
for x in inputs
|
|
|
|
]
|
|
|
|
return result[0] if single else result
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-12-27 13:56:05 +03:00
|
|
|
def _get_entity_from_string(self, string):
|
2017-10-05 13:27:05 +03:00
|
|
|
"""
|
2017-12-27 13:56:05 +03:00
|
|
|
Gets a full entity from the given string, which may be a phone or
|
2017-12-27 02:50:09 +03:00
|
|
|
an username, and processes all the found entities on the session.
|
2017-12-27 13:56:05 +03:00
|
|
|
The string may also be a user link, or a channel/chat invite link.
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-12-27 13:56:05 +03:00
|
|
|
This method has the side effect of adding the found users to the
|
|
|
|
session database, so it can be queried later without API calls,
|
|
|
|
if this option is enabled on the session.
|
2017-08-23 01:01:10 +03:00
|
|
|
|
2017-12-28 13:55:05 +03:00
|
|
|
Returns the found entity, or raises TypeError if not found.
|
2017-10-05 13:27:05 +03:00
|
|
|
"""
|
2017-12-27 02:50:09 +03:00
|
|
|
phone = utils.parse_phone(string)
|
2017-10-05 14:01:00 +03:00
|
|
|
if phone:
|
2017-12-27 13:56:05 +03:00
|
|
|
for user in self(GetContactsRequest(0)).users:
|
|
|
|
if user.phone == phone:
|
|
|
|
return user
|
2017-10-05 13:27:05 +03:00
|
|
|
else:
|
2017-12-27 13:56:05 +03:00
|
|
|
string, is_join_chat = utils.parse_username(string)
|
2017-11-10 15:27:51 +03:00
|
|
|
if is_join_chat:
|
2017-12-27 13:56:05 +03:00
|
|
|
invite = self(CheckChatInviteRequest(string))
|
2017-11-10 15:27:51 +03:00
|
|
|
if isinstance(invite, ChatInvite):
|
|
|
|
# If it's an invite to a chat, the user must join before
|
|
|
|
# for the link to be resolved and work, otherwise raise.
|
|
|
|
if invite.channel:
|
|
|
|
return invite.channel
|
|
|
|
elif isinstance(invite, ChatInviteAlready):
|
|
|
|
return invite.chat
|
|
|
|
else:
|
2018-01-05 21:51:44 +03:00
|
|
|
if string in ('me', 'self'):
|
|
|
|
return self.get_me()
|
2017-12-27 13:56:05 +03:00
|
|
|
result = self(ResolveUsernameRequest(string))
|
|
|
|
for entity in itertools.chain(result.users, result.chats):
|
|
|
|
if entity.username.lower() == string:
|
|
|
|
return entity
|
2017-10-01 20:02:53 +03:00
|
|
|
|
2017-12-28 13:55:05 +03:00
|
|
|
raise TypeError(
|
|
|
|
'Cannot turn "{}" into any entity (user or chat)'.format(string)
|
|
|
|
)
|
|
|
|
|
2017-10-01 15:19:04 +03:00
|
|
|
def get_input_entity(self, peer):
|
2017-10-21 17:59:20 +03:00
|
|
|
"""
|
|
|
|
Turns the given peer into its input entity version. Most requests
|
|
|
|
use this kind of InputUser, InputChat and so on, so this is the
|
|
|
|
most suitable call to make for those cases.
|
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
entity (:obj:`str` | :obj:`int` | :obj:`Peer` | :obj:`InputPeer`):
|
2017-10-21 17:59:20 +03:00
|
|
|
The integer ID of an user or otherwise either of a
|
2018-01-05 15:30:21 +03:00
|
|
|
``PeerUser``, ``PeerChat`` or ``PeerChannel``, for
|
|
|
|
which to get its ``Input*`` version.
|
2017-10-21 17:59:20 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
If this ``Peer`` hasn't been seen before by the library, the top
|
2017-10-21 17:59:20 +03:00
|
|
|
dialogs will be loaded and their entities saved to the session
|
|
|
|
file (unless this feature was disabled explicitly).
|
|
|
|
|
|
|
|
If in the end the access hash required for the peer was not found,
|
|
|
|
a ValueError will be raised.
|
2017-12-27 13:56:05 +03:00
|
|
|
|
2018-01-05 15:30:21 +03:00
|
|
|
Returns:
|
|
|
|
``InputPeerUser``, ``InputPeerChat`` or ``InputPeerChannel``.
|
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-12-27 02:50:09 +03:00
|
|
|
return self.session.get_input_entity(peer)
|
2017-12-27 14:36:14 +03:00
|
|
|
except ValueError:
|
2017-10-05 13:27:05 +03:00
|
|
|
pass
|
|
|
|
|
2017-10-01 17:57:07 +03:00
|
|
|
if isinstance(peer, str):
|
2018-01-05 21:51:44 +03:00
|
|
|
if peer in ('me', 'self'):
|
|
|
|
return InputPeerSelf()
|
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)
|
2017-12-28 13:55:05 +03:00
|
|
|
except TypeError:
|
2017-10-01 22:15:49 +03:00
|
|
|
pass
|
2017-10-01 15:19:04 +03:00
|
|
|
|
|
|
|
if not is_peer:
|
2017-12-28 02:22:28 +03:00
|
|
|
raise TypeError(
|
2017-10-01 15:19:04 +03:00
|
|
|
'Cannot turn "{}" into an input entity.'.format(peer)
|
|
|
|
)
|
|
|
|
|
2017-12-27 13:56:05 +03:00
|
|
|
# Not found, look in the latest dialogs.
|
|
|
|
# This is useful if for instance someone just sent a message but
|
|
|
|
# the updates didn't specify who, as this person or chat should
|
|
|
|
# be in the latest dialogs.
|
|
|
|
dialogs = self(GetDialogsRequest(
|
|
|
|
offset_date=None,
|
|
|
|
offset_id=0,
|
|
|
|
offset_peer=InputPeerEmpty(),
|
|
|
|
limit=0,
|
|
|
|
exclude_pinned=True
|
|
|
|
))
|
|
|
|
|
2017-12-28 15:31:43 +03:00
|
|
|
target = utils.get_peer_id(peer)
|
2017-12-27 13:56:05 +03:00
|
|
|
for entity in itertools.chain(dialogs.users, dialogs.chats):
|
2017-12-28 15:31:43 +03:00
|
|
|
if utils.get_peer_id(entity) == target:
|
2017-12-27 13:56:05 +03:00
|
|
|
return utils.get_input_peer(entity)
|
2017-10-01 15:19:04 +03:00
|
|
|
|
2017-12-28 13:55:05 +03:00
|
|
|
raise TypeError(
|
2017-10-01 15:19:04 +03:00
|
|
|
'Could not find the input entity corresponding to "{}".'
|
|
|
|
'Make sure you have encountered this peer before.'.format(peer)
|
|
|
|
)
|
|
|
|
|
2017-10-29 22:10:29 +03:00
|
|
|
# endregion
|