Finish up asyncio docs

This commit is contained in:
Lonami Exo
2018-06-22 14:44:59 +02:00
parent 3d3698562b
commit f614d3836b
12 changed files with 255 additions and 109 deletions

View File

@@ -19,9 +19,9 @@ not *interact* with a voting message), by making use of the
from telethon.tl.functions.messages import GetInlineBotResultsRequest
bot_results = client(GetInlineBotResultsRequest(
bot_results = loop.run_until_complete(client(GetInlineBotResultsRequest(
bot, user_or_chat, 'query', ''
))
)))
And you can select any of their results by using
:tl:`SendInlineBotResultRequest`:
@@ -30,16 +30,27 @@ And you can select any of their results by using
from telethon.tl.functions.messages import SendInlineBotResultRequest
client(SendInlineBotResultRequest(
loop.run_until_complete(client(SendInlineBotResultRequest(
get_input_peer(user_or_chat),
obtained_query_id,
obtained_str_id
))
)))
Talking to Bots with special reply markup
*****************************************
Generally, you just use the `message.click()
<telethon.tl.custom.message.Message.click>` method:
.. code-block:: python
async def main():
messages = await client.get_messages('somebot')
await messages[0].click(0)
You can also do it manually.
To interact with a message that has a special reply markup, such as
`@VoteBot`__ polls, you would use :tl:`GetBotCallbackAnswerRequest`:
@@ -47,11 +58,11 @@ To interact with a message that has a special reply markup, such as
from telethon.tl.functions.messages import GetBotCallbackAnswerRequest
client(GetBotCallbackAnswerRequest(
loop.run_until_complete(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

View File

@@ -12,7 +12,7 @@ Joining a chat or channel
*************************
Note that :tl:`Chat` are normal groups, and :tl:`Channel` are a
special form of ``Chat``, which can also be super-groups if
special form of :tl:`Chat`, which can also be super-groups if
their ``megagroup`` member is ``True``.
@@ -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
client(JoinChannelRequest(channel))
loop.run_until_complete(client(JoinChannelRequest(channel)))
# In the same way, you can also leave such channel
from telethon.tl.functions.channels import LeaveChannelRequest
client(LeaveChannelRequest(input_channel))
loop.run_until_complete(client(LeaveChannelRequest(input_channel)))
For more on channels, check the `channels namespace`__.
@@ -51,7 +51,9 @@ example, is the ``hash`` of the chat or channel. Now you can use
.. code-block:: python
from telethon.tl.functions.messages import ImportChatInviteRequest
updates = client(ImportChatInviteRequest('AAAAAEHbEkejzxUjAUCfYg'))
updates = loop.run_until_complete(
client(ImportChatInviteRequest('AAAAAEHbEkejzxUjAUCfYg'))
)
Adding someone else to such chat or channel
@@ -68,19 +70,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``).
client(AddChatUserRequest(
loop.run_until_complete(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
client(InviteToChannelRequest(
loop.run_until_complete(client(InviteToChannelRequest(
channel,
[users_to_add]
))
)))
Checking a link without joining
@@ -102,6 +104,14 @@ Retrieving all chat members (channels too)
This method will handle different chat types for you automatically.
Here is the easy way to do it:
.. code-block:: python
participants = loop.run_until_complete(client.get_participants(group))
Now we will show how the method works internally.
In order to get all the members from a mega-group or channel, you need
to use :tl:`GetParticipantsRequest`. As we can see it needs an
:tl:`InputChannel`, (passing the mega-group or channel you're going to
@@ -123,10 +133,9 @@ a fixed limit:
all_participants = []
while True:
participants = client(GetParticipantsRequest(
channel, ChannelParticipantsSearch(''), offset, limit,
hash=0
))
participants = loop.run_until_complete(client(GetParticipantsRequest(
channel, ChannelParticipantsSearch(''), offset, limit, hash=0
)))
if not participants.users:
break
all_participants.extend(participants.users)
@@ -193,7 +202,7 @@ Giving or revoking admin permissions can be done with the :tl:`EditAdminRequest`
# )
# Once you have a ChannelAdminRights, invoke it
client(EditAdminRequest(channel, user, rights))
loop.run_until_complete(client(EditAdminRequest(channel, user, rights)))
# User will now be able to change group info, delete other people's
# messages and pin messages.
@@ -252,7 +261,7 @@ banned rights of an user through :tl:`EditBannedRequest` and its parameter
embed_links=True
)
client(EditBannedRequest(channel, user, rights))
loop.run_until_complete(client(EditBannedRequest(channel, user, rights)))
Kicking a member
@@ -267,9 +276,11 @@ is enough:
from telethon.tl.functions.channels import EditBannedRequest
from telethon.tl.types import ChannelBannedRights
client(EditBannedRequest(channel, user, ChannelBannedRights(
until_date=None,
view_messages=True
loop.run_until_complete(client(EditBannedRequest(
channel, user, ChannelBannedRights(
until_date=None,
view_messages=True
)
)))
@@ -291,11 +302,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.
client(GetMessagesViewsRequest(
loop.run_until_complete(client(GetMessagesViewsRequest(
peer=channel,
id=msg_ids,
increment=True
))
)))
Note that you can only do this **once or twice a day** per account,

View File

@@ -19,11 +19,12 @@ you should use :tl:`GetFullUser`:
from telethon.tl.functions.users import GetFullUserRequest
full = client(GetFullUserRequest(user))
# or even
full = client(GetFullUserRequest('username'))
async def main():
full = await client(GetFullUserRequest(user))
# or even
full = await client(GetFullUserRequest('username'))
bio = full.about
bio = full.about
See :tl:`UserFull` to know what other fields you can access.
@@ -39,7 +40,9 @@ request. Omitted fields won't change after invoking :tl:`UpdateProfile`:
from telethon.tl.functions.account import UpdateProfileRequest
client(UpdateProfileRequest(about='This is a test from Telethon'))
loop.run_until_complete(client(UpdateProfileRequest(a
bout='This is a test from Telethon'
)))
Updating your username
@@ -51,7 +54,7 @@ You need to use :tl:`account.UpdateUsername`:
from telethon.tl.functions.account import UpdateUsernameRequest
client(UpdateUsernameRequest('new_username'))
loop.run_until_complete(client(UpdateUsernameRequest('new_username')))
Updating your profile photo
@@ -65,6 +68,6 @@ through :tl:`UploadProfilePhoto`:
from telethon.tl.functions.photos import UploadProfilePhotoRequest
client(UploadProfilePhotoRequest(
loop.run_until_complete(client(UploadProfilePhotoRequest(
client.upload_file('/path/to/some/file')
))
)))

View File

@@ -13,38 +13,39 @@ Forwarding messages
.. note::
Use the `telethon.telegram_client.TelegramClient.forward_messages`
Use the `telethon.client.messages.MessageMethods.forward_messages`
friendly method instead unless you have a better reason not to!
This method automatically accepts either a single message or many of them.
.. code-block:: python
# 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?
)
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 have ``Message`` objects
client.forward_messages(
entity, # to which entity you are forwarding the messages
messages # the messages (or message) to forward
)
# 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
)
# 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()
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?
))
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?
))
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
@@ -56,7 +57,7 @@ Searching Messages
.. note::
Use the `telethon.telegram_client.TelegramClient.iter_messages`
Use the `telethon.client.messages.MessageMethods.iter_messages`
friendly method instead unless you have a better reason not to!
This method has ``search`` and ``filter`` parameters that will
@@ -71,7 +72,7 @@ into issues_. A valid example would be:
from telethon.tl.types import InputMessagesFilterEmpty
filter = InputMessagesFilterEmpty()
result = client(SearchRequest(
result = loop.run_until_complete(client(SearchRequest(
peer=peer, # On which chat/conversation
q='query', # What to search for
filter=filter, # Filter to use (maybe filter for media)
@@ -84,7 +85,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
@@ -99,7 +100,7 @@ you tried setting the ``from_id`` filter, and as the error says, you can't
do that. Leave it set to ``None`` and it should work.
As with every method, make sure you use the right ID/hash combination for
your ``InputUser`` or ``InputChat``, or you'll likely run into errors like
your :tl:`InputUser` or :tl:`InputChat`, or you'll likely run into errors like
``UserIdInvalidError``.
@@ -115,29 +116,25 @@ send yourself the very first sticker you have:
.. code-block:: python
# Get all the sticker sets this user has
sticker_sets = client(GetAllStickersRequest(0))
async def main():
# Get all the sticker sets this user has
from telethon.tl.functions.messages import GetAllStickersRequest
sticker_sets = await client(GetAllStickersRequest(0))
# Choose a sticker set
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 = client(GetStickerSetRequest(
stickerset=InputStickerSetID(
id=sticker_set.id, access_hash=sticker_set.access_hash
)
))
# Stickers are nothing more than files, so send that
client(SendMediaRequest(
peer=client.get_me(),
media=InputMediaDocument(
id=InputDocument(
id=stickers.documents[0].id,
access_hash=stickers.documents[0].access_hash
# Get the stickers for this sticker set
stickers = await 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])
.. _issues: https://github.com/LonamiWebs/Telethon/issues/215