mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 13:29:47 +00:00
Document the magic sync module
This commit is contained in:
@@ -19,9 +19,9 @@ not *interact* with a voting message), by making use of the
|
||||
|
||||
from telethon.tl.functions.messages import GetInlineBotResultsRequest
|
||||
|
||||
bot_results = loop.run_until_complete(client(GetInlineBotResultsRequest(
|
||||
bot_results = client(GetInlineBotResultsRequest(
|
||||
bot, user_or_chat, 'query', ''
|
||||
)))
|
||||
))
|
||||
|
||||
And you can select any of their results by using
|
||||
:tl:`SendInlineBotResultRequest`:
|
||||
@@ -30,11 +30,11 @@ And you can select any of their results by using
|
||||
|
||||
from telethon.tl.functions.messages import SendInlineBotResultRequest
|
||||
|
||||
loop.run_until_complete(client(SendInlineBotResultRequest(
|
||||
client(SendInlineBotResultRequest(
|
||||
get_input_peer(user_or_chat),
|
||||
obtained_query_id,
|
||||
obtained_str_id
|
||||
)))
|
||||
))
|
||||
|
||||
|
||||
Talking to Bots with special reply markup
|
||||
@@ -45,9 +45,8 @@ Generally, you just use the `message.click()
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def main():
|
||||
messages = await client.get_messages('somebot')
|
||||
await messages[0].click(0)
|
||||
messages = client.get_messages('somebot')
|
||||
messages[0].click(0)
|
||||
|
||||
You can also do it manually.
|
||||
|
||||
@@ -58,11 +57,11 @@ To interact with a message that has a special reply markup, such as
|
||||
|
||||
from telethon.tl.functions.messages import GetBotCallbackAnswerRequest
|
||||
|
||||
loop.run_until_complete(client(GetBotCallbackAnswerRequest(
|
||||
client(GetBotCallbackAnswerRequest(
|
||||
user_or_chat,
|
||||
msg.id,
|
||||
data=msg.reply_markup.rows[wanted_row].buttons[wanted_button].data
|
||||
)))
|
||||
))
|
||||
|
||||
It's a bit verbose, but it has all the information you would need to
|
||||
show it visually (button rows, and buttons within each row, each with
|
||||
|
@@ -25,11 +25,11 @@ to, you can make use of the :tl:`JoinChannelRequest` to join such channel:
|
||||
.. code-block:: python
|
||||
|
||||
from telethon.tl.functions.channels import JoinChannelRequest
|
||||
loop.run_until_complete(client(JoinChannelRequest(channel)))
|
||||
client(JoinChannelRequest(channel))
|
||||
|
||||
# In the same way, you can also leave such channel
|
||||
from telethon.tl.functions.channels import LeaveChannelRequest
|
||||
loop.run_until_complete(client(LeaveChannelRequest(input_channel)))
|
||||
client(LeaveChannelRequest(input_channel))
|
||||
|
||||
|
||||
For more on channels, check the `channels namespace`__.
|
||||
@@ -51,9 +51,7 @@ example, is the ``hash`` of the chat or channel. Now you can use
|
||||
.. code-block:: python
|
||||
|
||||
from telethon.tl.functions.messages import ImportChatInviteRequest
|
||||
updates = loop.run_until_complete(
|
||||
client(ImportChatInviteRequest('AAAAAEHbEkejzxUjAUCfYg'))
|
||||
)
|
||||
updates = client(ImportChatInviteRequest('AAAAAEHbEkejzxUjAUCfYg'))
|
||||
|
||||
|
||||
Adding someone else to such chat or channel
|
||||
@@ -70,19 +68,19 @@ use is very straightforward, or :tl:`InviteToChannelRequest` for channels:
|
||||
|
||||
# Note that ``user_to_add`` is NOT the name of the parameter.
|
||||
# It's the user you want to add (``user_id=user_to_add``).
|
||||
loop.run_until_complete(client(AddChatUserRequest(
|
||||
client(AddChatUserRequest(
|
||||
chat_id,
|
||||
user_to_add,
|
||||
fwd_limit=10 # Allow the user to see the 10 last messages
|
||||
)))
|
||||
))
|
||||
|
||||
# For channels (which includes megagroups)
|
||||
from telethon.tl.functions.channels import InviteToChannelRequest
|
||||
|
||||
loop.run_until_complete(client(InviteToChannelRequest(
|
||||
client(InviteToChannelRequest(
|
||||
channel,
|
||||
[users_to_add]
|
||||
)))
|
||||
))
|
||||
|
||||
|
||||
Checking a link without joining
|
||||
@@ -108,7 +106,7 @@ Here is the easy way to do it:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
participants = loop.run_until_complete(client.get_participants(group))
|
||||
participants = client.get_participants(group)
|
||||
|
||||
Now we will show how the method works internally.
|
||||
|
||||
@@ -133,9 +131,9 @@ a fixed limit:
|
||||
all_participants = []
|
||||
|
||||
while True:
|
||||
participants = loop.run_until_complete(client(GetParticipantsRequest(
|
||||
participants = client(GetParticipantsRequest(
|
||||
channel, ChannelParticipantsSearch(''), offset, limit, hash=0
|
||||
)))
|
||||
))
|
||||
if not participants.users:
|
||||
break
|
||||
all_participants.extend(participants.users)
|
||||
@@ -202,7 +200,7 @@ Giving or revoking admin permissions can be done with the :tl:`EditAdminRequest`
|
||||
# )
|
||||
|
||||
# Once you have a ChannelAdminRights, invoke it
|
||||
loop.run_until_complete(client(EditAdminRequest(channel, user, rights)))
|
||||
client(EditAdminRequest(channel, user, rights))
|
||||
|
||||
# User will now be able to change group info, delete other people's
|
||||
# messages and pin messages.
|
||||
@@ -261,7 +259,7 @@ banned rights of an user through :tl:`EditBannedRequest` and its parameter
|
||||
embed_links=True
|
||||
)
|
||||
|
||||
loop.run_until_complete(client(EditBannedRequest(channel, user, rights)))
|
||||
client(EditBannedRequest(channel, user, rights))
|
||||
|
||||
|
||||
Kicking a member
|
||||
@@ -276,12 +274,12 @@ is enough:
|
||||
from telethon.tl.functions.channels import EditBannedRequest
|
||||
from telethon.tl.types import ChannelBannedRights
|
||||
|
||||
loop.run_until_complete(client(EditBannedRequest(
|
||||
client(EditBannedRequest(
|
||||
channel, user, ChannelBannedRights(
|
||||
until_date=None,
|
||||
view_messages=True
|
||||
)
|
||||
)))
|
||||
))
|
||||
|
||||
|
||||
__ https://github.com/Kyle2142
|
||||
@@ -302,11 +300,11 @@ use :tl:`GetMessagesViewsRequest`, setting ``increment=True``:
|
||||
# Obtain `channel' through dialogs or through client.get_entity() or anyhow.
|
||||
# Obtain `msg_ids' through `.get_messages()` or anyhow. Must be a list.
|
||||
|
||||
loop.run_until_complete(client(GetMessagesViewsRequest(
|
||||
client(GetMessagesViewsRequest(
|
||||
peer=channel,
|
||||
id=msg_ids,
|
||||
increment=True
|
||||
)))
|
||||
))
|
||||
|
||||
|
||||
Note that you can only do this **once or twice a day** per account,
|
||||
|
@@ -19,12 +19,11 @@ you should use :tl:`GetFullUser`:
|
||||
|
||||
from telethon.tl.functions.users import GetFullUserRequest
|
||||
|
||||
async def main():
|
||||
full = await client(GetFullUserRequest(user))
|
||||
# or even
|
||||
full = await client(GetFullUserRequest('username'))
|
||||
full = client(GetFullUserRequest(user))
|
||||
# or even
|
||||
full = client(GetFullUserRequest('username'))
|
||||
|
||||
bio = full.about
|
||||
bio = full.about
|
||||
|
||||
|
||||
See :tl:`UserFull` to know what other fields you can access.
|
||||
@@ -40,9 +39,9 @@ request. Omitted fields won't change after invoking :tl:`UpdateProfile`:
|
||||
|
||||
from telethon.tl.functions.account import UpdateProfileRequest
|
||||
|
||||
loop.run_until_complete(client(UpdateProfileRequest(a
|
||||
client(UpdateProfileRequest(a
|
||||
bout='This is a test from Telethon'
|
||||
)))
|
||||
))
|
||||
|
||||
|
||||
Updating your username
|
||||
@@ -54,7 +53,7 @@ You need to use :tl:`account.UpdateUsername`:
|
||||
|
||||
from telethon.tl.functions.account import UpdateUsernameRequest
|
||||
|
||||
loop.run_until_complete(client(UpdateUsernameRequest('new_username')))
|
||||
client(UpdateUsernameRequest('new_username'))
|
||||
|
||||
|
||||
Updating your profile photo
|
||||
@@ -68,6 +67,6 @@ through :tl:`UploadProfilePhoto`:
|
||||
|
||||
from telethon.tl.functions.photos import UploadProfilePhotoRequest
|
||||
|
||||
loop.run_until_complete(client(UploadProfilePhotoRequest(
|
||||
client(UploadProfilePhotoRequest(
|
||||
client.upload_file('/path/to/some/file')
|
||||
)))
|
||||
|
@@ -20,32 +20,31 @@ Forwarding messages
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def main():
|
||||
# If you only have the message IDs
|
||||
await client.forward_messages(
|
||||
entity, # to which entity you are forwarding the messages
|
||||
message_ids, # the IDs of the messages (or message) to forward
|
||||
from_entity # who sent the messages?
|
||||
)
|
||||
# If you only have the message IDs
|
||||
client.forward_messages(
|
||||
entity, # to which entity you are forwarding the messages
|
||||
message_ids, # the IDs of the messages (or message) to forward
|
||||
from_entity # who sent the messages?
|
||||
)
|
||||
|
||||
# If you have ``Message`` objects
|
||||
await client.forward_messages(
|
||||
entity, # to which entity you are forwarding the messages
|
||||
messages # the messages (or message) to forward
|
||||
)
|
||||
# If you have ``Message`` objects
|
||||
client.forward_messages(
|
||||
entity, # to which entity you are forwarding the messages
|
||||
messages # the messages (or message) to forward
|
||||
)
|
||||
|
||||
# You can also do it manually if you prefer
|
||||
from telethon.tl.functions.messages import ForwardMessagesRequest
|
||||
# You can also do it manually if you prefer
|
||||
from telethon.tl.functions.messages import ForwardMessagesRequest
|
||||
|
||||
messages = foo() # retrieve a few messages (or even one, in a list)
|
||||
from_entity = bar()
|
||||
to_entity = baz()
|
||||
messages = foo() # retrieve a few messages (or even one, in a list)
|
||||
from_entity = bar()
|
||||
to_entity = baz()
|
||||
|
||||
await client(ForwardMessagesRequest(
|
||||
from_peer=from_entity, # who sent these messages?
|
||||
id=[msg.id for msg in messages], # which are the messages?
|
||||
to_peer=to_entity # who are we forwarding them to?
|
||||
))
|
||||
client(ForwardMessagesRequest(
|
||||
from_peer=from_entity, # who sent these messages?
|
||||
id=[msg.id for msg in messages], # which are the messages?
|
||||
to_peer=to_entity # who are we forwarding them to?
|
||||
))
|
||||
|
||||
The named arguments are there for clarity, although they're not needed because
|
||||
they appear in order. You can obviously just wrap a single message on the list
|
||||
@@ -72,7 +71,7 @@ into issues_. A valid example would be:
|
||||
from telethon.tl.types import InputMessagesFilterEmpty
|
||||
|
||||
filter = InputMessagesFilterEmpty()
|
||||
result = loop.run_until_complete(client(SearchRequest(
|
||||
result = client(SearchRequest(
|
||||
peer=peer, # On which chat/conversation
|
||||
q='query', # What to search for
|
||||
filter=filter, # Filter to use (maybe filter for media)
|
||||
@@ -85,7 +84,7 @@ into issues_. A valid example would be:
|
||||
min_id=0, # Minimum message ID
|
||||
from_id=None, # Who must have sent the message (peer)
|
||||
hash=0 # Special number to return nothing on no-change
|
||||
)))
|
||||
))
|
||||
|
||||
It's important to note that the optional parameter ``from_id`` could have
|
||||
been omitted (defaulting to ``None``). Changing it to :tl:`InputUserEmpty`, as one
|
||||
@@ -116,25 +115,24 @@ send yourself the very first sticker you have:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def main():
|
||||
# Get all the sticker sets this user has
|
||||
from telethon.tl.functions.messages import GetAllStickersRequest
|
||||
sticker_sets = await client(GetAllStickersRequest(0))
|
||||
# Get all the sticker sets this user has
|
||||
from telethon.tl.functions.messages import GetAllStickersRequest
|
||||
sticker_sets = client(GetAllStickersRequest(0))
|
||||
|
||||
# Choose a sticker set
|
||||
from telethon.tl.functions.messages import GetStickerSetRequest
|
||||
from telethon.tl.types import InputStickerSetID
|
||||
sticker_set = sticker_sets.sets[0]
|
||||
# Choose a sticker set
|
||||
from telethon.tl.functions.messages import GetStickerSetRequest
|
||||
from telethon.tl.types import InputStickerSetID
|
||||
sticker_set = sticker_sets.sets[0]
|
||||
|
||||
# Get the stickers for this sticker set
|
||||
stickers = await client(GetStickerSetRequest(
|
||||
stickerset=InputStickerSetID(
|
||||
id=sticker_set.id, access_hash=sticker_set.access_hash
|
||||
)
|
||||
))
|
||||
# Get the stickers for this sticker set
|
||||
stickers = client(GetStickerSetRequest(
|
||||
stickerset=InputStickerSetID(
|
||||
id=sticker_set.id, access_hash=sticker_set.access_hash
|
||||
)
|
||||
))
|
||||
|
||||
# Stickers are nothing more than files, so send that
|
||||
await client.send_file('me', stickers.documents[0])
|
||||
# Stickers are nothing more than files, so send that
|
||||
client.send_file('me', stickers.documents[0])
|
||||
|
||||
|
||||
.. _issues: https://github.com/LonamiWebs/Telethon/issues/215
|
||||
|
Reference in New Issue
Block a user