2018-01-05 02:59:53 +03:00
|
|
|
===============================
|
|
|
|
Working with Chats and Channels
|
|
|
|
===============================
|
|
|
|
|
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
.. note::
|
|
|
|
|
2019-05-09 13:24:37 +03:00
|
|
|
These examples assume you have read :ref:`full-api`.
|
2018-01-20 13:47:17 +03:00
|
|
|
|
2018-10-06 21:20:11 +03:00
|
|
|
.. contents::
|
|
|
|
|
2018-01-20 13:47:17 +03:00
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
Joining a chat or channel
|
2019-05-09 13:24:37 +03:00
|
|
|
=========================
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-03-27 19:22:01 +03:00
|
|
|
Note that :tl:`Chat` are normal groups, and :tl:`Channel` are a
|
2018-06-22 15:44:59 +03:00
|
|
|
special form of :tl:`Chat`, which can also be super-groups if
|
2019-07-06 13:10:25 +03:00
|
|
|
their ``megagroup`` member is `True`.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
Joining a public channel
|
2019-05-09 13:24:37 +03:00
|
|
|
========================
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
Once you have the :ref:`entity <entities>` of the channel you want to join
|
2018-04-08 15:15:26 +03:00
|
|
|
to, you can make use of the :tl:`JoinChannelRequest` to join such channel:
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
from telethon.tl.functions.channels import JoinChannelRequest
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(JoinChannelRequest(channel))
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
# In the same way, you can also leave such channel
|
|
|
|
from telethon.tl.functions.channels import LeaveChannelRequest
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(LeaveChannelRequest(input_channel))
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
For more on channels, check the `channels namespace`__.
|
|
|
|
|
|
|
|
|
2019-05-22 12:29:46 +03:00
|
|
|
__ https://tl.telethon.dev/methods/channels/index.html
|
2018-04-06 20:00:21 +03:00
|
|
|
|
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
Joining a private chat or channel
|
2019-05-09 13:24:37 +03:00
|
|
|
=================================
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
If all you have is a link like this one:
|
|
|
|
``https://t.me/joinchat/AAAAAFFszQPyPEZ7wgxLtd``, you already have
|
|
|
|
enough information to join! The part after the
|
|
|
|
``https://t.me/joinchat/``, this is, ``AAAAAFFszQPyPEZ7wgxLtd`` on this
|
|
|
|
example, is the ``hash`` of the chat or channel. Now you can use
|
2018-04-08 15:15:26 +03:00
|
|
|
:tl:`ImportChatInviteRequest` as follows:
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
from telethon.tl.functions.messages import ImportChatInviteRequest
|
2019-08-14 00:33:39 +03:00
|
|
|
updates = await client(ImportChatInviteRequest('AAAAAEHbEkejzxUjAUCfYg'))
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
Adding someone else to such chat or channel
|
2019-05-09 13:24:37 +03:00
|
|
|
===========================================
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
If you don't want to add yourself, maybe because you're already in,
|
2018-04-08 15:15:26 +03:00
|
|
|
you can always add someone else with the :tl:`AddChatUserRequest`, which
|
|
|
|
use is very straightforward, or :tl:`InviteToChannelRequest` for channels:
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
# For normal chats
|
|
|
|
from telethon.tl.functions.messages import AddChatUserRequest
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
# 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``).
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(AddChatUserRequest(
|
2018-06-20 12:05:33 +03:00
|
|
|
chat_id,
|
|
|
|
user_to_add,
|
|
|
|
fwd_limit=10 # Allow the user to see the 10 last messages
|
2018-06-25 22:14:58 +03:00
|
|
|
))
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
# For channels (which includes megagroups)
|
|
|
|
from telethon.tl.functions.channels import InviteToChannelRequest
|
2018-03-22 20:39:42 +03:00
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(InviteToChannelRequest(
|
2018-06-20 12:05:33 +03:00
|
|
|
channel,
|
|
|
|
[users_to_add]
|
2018-06-25 22:14:58 +03:00
|
|
|
))
|
2018-03-22 20:39:42 +03:00
|
|
|
|
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
Checking a link without joining
|
2019-05-09 13:24:37 +03:00
|
|
|
===============================
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
If you don't need to join but rather check whether it's a group or a
|
2018-04-08 15:15:26 +03:00
|
|
|
channel, you can use the :tl:`CheckChatInviteRequest`, which takes in
|
2018-01-08 16:04:04 +03:00
|
|
|
the hash of said channel or group.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-04-06 20:00:21 +03:00
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
Admin Permissions
|
2019-05-09 13:24:37 +03:00
|
|
|
=================
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-04-08 15:15:26 +03:00
|
|
|
Giving or revoking admin permissions can be done with the :tl:`EditAdminRequest`:
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from telethon.tl.functions.channels import EditAdminRequest
|
2019-01-24 16:12:35 +03:00
|
|
|
from telethon.tl.types import ChatAdminRights
|
2018-06-20 12:05:33 +03:00
|
|
|
|
|
|
|
# You need both the channel and who to grant permissions
|
|
|
|
# They can either be channel/user or input channel/input user.
|
|
|
|
#
|
2019-01-24 16:12:35 +03:00
|
|
|
# ChatAdminRights is a list of granted permissions.
|
2018-06-20 12:05:33 +03:00
|
|
|
# Set to True those you want to give.
|
2019-01-24 16:12:35 +03:00
|
|
|
rights = ChatAdminRights(
|
2018-06-20 12:05:33 +03:00
|
|
|
post_messages=None,
|
|
|
|
add_admins=None,
|
|
|
|
invite_users=None,
|
|
|
|
change_info=True,
|
|
|
|
ban_users=None,
|
|
|
|
delete_messages=True,
|
|
|
|
pin_messages=True,
|
|
|
|
invite_link=None,
|
|
|
|
edit_messages=None
|
|
|
|
)
|
|
|
|
# Equivalent to:
|
2019-01-24 16:12:35 +03:00
|
|
|
# rights = ChatAdminRights(
|
2018-06-20 12:05:33 +03:00
|
|
|
# change_info=True,
|
|
|
|
# delete_messages=True,
|
|
|
|
# pin_messages=True
|
|
|
|
# )
|
|
|
|
|
2019-01-24 16:12:35 +03:00
|
|
|
# Once you have a ChatAdminRights, invoke it
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(EditAdminRequest(channel, user, rights))
|
2018-06-20 12:05:33 +03:00
|
|
|
|
|
|
|
# User will now be able to change group info, delete other people's
|
|
|
|
# messages and pin messages.
|
2019-01-24 16:12:35 +03:00
|
|
|
#
|
|
|
|
# In a normal chat, you should do this instead:
|
|
|
|
from telethon.tl.functions.messages import EditChatAdminRequest
|
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(EditChatAdminRequest(chat_id, user, is_admin=True))
|
2019-01-24 16:12:35 +03:00
|
|
|
|
2018-04-06 20:00:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Thanks to `@Kyle2142`__ for `pointing out`__ that you **cannot** set all
|
2019-07-06 13:10:25 +03:00
|
|
|
parameters to `True` to give a user full permissions, as not all
|
2018-04-06 20:00:21 +03:00
|
|
|
permissions are related to both broadcast channels/megagroups.
|
|
|
|
|
|
|
|
E.g. trying to set ``post_messages=True`` in a megagroup will raise an
|
|
|
|
error. It is recommended to always use keyword arguments, and to set only
|
|
|
|
the permissions the user needs. If you don't need to change a permission,
|
|
|
|
it can be omitted (full list `here`__).
|
|
|
|
|
|
|
|
|
|
|
|
Restricting Users
|
2019-05-09 13:24:37 +03:00
|
|
|
=================
|
2018-04-06 20:00:21 +03:00
|
|
|
|
|
|
|
Similar to how you give or revoke admin permissions, you can edit the
|
2018-09-22 20:18:42 +03:00
|
|
|
banned rights of a user through :tl:`EditBannedRequest` and its parameter
|
2019-01-24 16:12:35 +03:00
|
|
|
:tl:`ChatBannedRights`:
|
2018-04-06 20:00:21 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from telethon.tl.functions.channels import EditBannedRequest
|
2019-01-24 16:12:35 +03:00
|
|
|
from telethon.tl.types import ChatBannedRights
|
2018-06-20 12:05:33 +03:00
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2018-09-22 20:18:42 +03:00
|
|
|
# Restricting a user for 7 days, only allowing view/send messages.
|
2018-06-20 12:05:33 +03:00
|
|
|
#
|
2019-07-06 13:10:25 +03:00
|
|
|
# Note that it's "reversed". You must set to `True` the permissions
|
|
|
|
# you want to REMOVE, and leave as `None` those you want to KEEP.
|
2019-01-24 16:12:35 +03:00
|
|
|
rights = ChatBannedRights(
|
2018-07-29 13:48:20 +03:00
|
|
|
until_date=timedelta(days=7),
|
2018-06-20 12:05:33 +03:00
|
|
|
view_messages=None,
|
|
|
|
send_messages=None,
|
|
|
|
send_media=True,
|
|
|
|
send_stickers=True,
|
|
|
|
send_gifs=True,
|
|
|
|
send_games=True,
|
|
|
|
send_inline=True,
|
|
|
|
embed_links=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# The above is equivalent to
|
2019-01-24 16:12:35 +03:00
|
|
|
rights = ChatBannedRights(
|
2018-06-20 12:05:33 +03:00
|
|
|
until_date=datetime.now() + timedelta(days=7),
|
|
|
|
send_media=True,
|
|
|
|
send_stickers=True,
|
|
|
|
send_gifs=True,
|
|
|
|
send_games=True,
|
|
|
|
send_inline=True,
|
|
|
|
embed_links=True
|
|
|
|
)
|
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(EditBannedRequest(channel, user, rights))
|
2018-04-06 20:00:21 +03:00
|
|
|
|
|
|
|
|
2019-06-11 12:09:22 +03:00
|
|
|
You can use a `datetime.datetime` object for ``until_date=``,
|
|
|
|
a `datetime.timedelta` or even a Unix timestamp. Note that if you ban
|
|
|
|
someone for less than 30 seconds or for more than 366 days, Telegram
|
|
|
|
will consider the ban to actually last forever. This is officially
|
|
|
|
documented under https://core.telegram.org/bots/api#restrictchatmember.
|
2018-07-29 13:48:20 +03:00
|
|
|
|
|
|
|
|
2018-04-06 20:00:21 +03:00
|
|
|
Kicking a member
|
2019-05-09 13:24:37 +03:00
|
|
|
================
|
2018-04-06 20:00:21 +03:00
|
|
|
|
2018-09-22 20:18:42 +03:00
|
|
|
Telegram doesn't actually have a request to kick a user from a group.
|
2018-04-06 20:00:21 +03:00
|
|
|
Instead, you need to restrict them so they can't see messages. Any date
|
|
|
|
is enough:
|
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
2018-04-06 20:00:21 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
from telethon.tl.functions.channels import EditBannedRequest
|
2019-01-24 16:12:35 +03:00
|
|
|
from telethon.tl.types import ChatBannedRights
|
2018-04-06 20:00:21 +03:00
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(EditBannedRequest(
|
2019-01-24 16:12:35 +03:00
|
|
|
channel, user, ChatBannedRights(
|
2018-06-22 15:44:59 +03:00
|
|
|
until_date=None,
|
|
|
|
view_messages=True
|
|
|
|
)
|
2018-06-25 22:14:58 +03:00
|
|
|
))
|
2018-04-06 20:00:21 +03:00
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
__ https://github.com/Kyle2142
|
|
|
|
__ https://github.com/LonamiWebs/Telethon/issues/490
|
2019-05-22 12:29:46 +03:00
|
|
|
__ https://tl.telethon.dev/constructors/channel_admin_rights.html
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
Increasing View Count in a Channel
|
2019-05-09 13:24:37 +03:00
|
|
|
==================================
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
It has been asked `quite`__ `a few`__ `times`__ (really, `many`__), and
|
|
|
|
while I don't understand why so many people ask this, the solution is to
|
2018-04-08 15:15:26 +03:00
|
|
|
use :tl:`GetMessagesViewsRequest`, setting ``increment=True``:
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
.. code-block:: python
|
2018-01-05 02:59:53 +03:00
|
|
|
|
|
|
|
|
2018-06-20 12:05:33 +03:00
|
|
|
# Obtain `channel' through dialogs or through client.get_entity() or anyhow.
|
|
|
|
# Obtain `msg_ids' through `.get_messages()` or anyhow. Must be a list.
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
await client(GetMessagesViewsRequest(
|
2018-06-20 12:05:33 +03:00
|
|
|
peer=channel,
|
|
|
|
id=msg_ids,
|
|
|
|
increment=True
|
2018-06-25 22:14:58 +03:00
|
|
|
))
|
2018-01-05 02:59:53 +03:00
|
|
|
|
2018-03-22 20:39:42 +03:00
|
|
|
|
|
|
|
Note that you can only do this **once or twice a day** per account,
|
|
|
|
running this in a loop will obviously not increase the views forever
|
|
|
|
unless you wait a day between each iteration. If you run it any sooner
|
|
|
|
than that, the views simply won't be increased.
|
|
|
|
|
2018-01-05 02:59:53 +03:00
|
|
|
__ https://github.com/LonamiWebs/Telethon/issues/233
|
|
|
|
__ https://github.com/LonamiWebs/Telethon/issues/305
|
|
|
|
__ https://github.com/LonamiWebs/Telethon/issues/409
|
|
|
|
__ https://github.com/LonamiWebs/Telethon/issues/447
|