Skip to content

Commit fda25f6

Browse files
bakatroubledelivrance
authored andcommitted
Add client.set_username() method (pyrogram#208)
* Add client.set_username() method * Rename set_username() to update_user_username(); allow None as username * Add client.update_chat_username() method * Update update_chat_username.py * Update update_user_username.py Rename update_user_username to update_username Add more details in docstrings Fix style * Rename update_user_username.py to update_username.py * Update __init__.py * Update 400_BAD_REQUEST.tsv
1 parent 5294c21 commit fda25f6

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

compiler/error/source/400_BAD_REQUEST.tsv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@ MEDIA_INVALID The media is invalid
8888
BOT_SCORE_NOT_MODIFIED The bot score was not modified
8989
USER_BOT_REQUIRED The method can be used by bots only
9090
IMAGE_PROCESS_FAILED The server failed to process your image
91+
USERNAME_NOT_MODIFIED The username was not modified
9192
CALL_ALREADY_ACCEPTED The call is already accepted
9293
CALL_ALREADY_DECLINED The call is already declined

pyrogram/client/methods/chats/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from .set_chat_title import SetChatTitle
3838
from .unban_chat_member import UnbanChatMember
3939
from .unpin_chat_message import UnpinChatMessage
40+
from .update_chat_username import UpdateChatUsername
4041

4142

4243
class Chats(
@@ -60,6 +61,7 @@ class Chats(
6061
GetChatMembersCount,
6162
GetChatPreview,
6263
IterDialogs,
63-
IterChatMembers
64+
IterChatMembers,
65+
UpdateChatUsername
6466
):
6567
pass
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Union
20+
21+
from pyrogram.api import functions, types
22+
from ...ext import BaseClient
23+
24+
25+
class UpdateChatUsername(BaseClient):
26+
def update_chat_username(self,
27+
chat_id: Union[int, str],
28+
username: Union[str, None]) -> bool:
29+
"""Use this method to update a channel or a supergroup username.
30+
31+
To update your own username (for users only, not bots) you can use :meth:`update_username`.
32+
33+
Args:
34+
chat_id (``int`` | ``str``)
35+
Unique identifier (int) or username (str) of the target chat.
36+
username (``str`` | ``None``):
37+
Username to set. Pass "" (empty string) or None to remove the username.
38+
39+
Returns:
40+
True on success.
41+
42+
Raises:
43+
:class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
44+
``ValueError`` if a chat_id belongs to a user or chat.
45+
"""
46+
47+
peer = self.resolve_peer(chat_id)
48+
49+
if isinstance(peer, types.InputPeerChannel):
50+
return bool(
51+
self.send(
52+
functions.channels.UpdateUsername(
53+
channel=peer,
54+
username=username or ""
55+
)
56+
)
57+
)
58+
else:
59+
raise ValueError("The chat_id \"{}\" belongs to a user or chat".format(chat_id))

pyrogram/client/methods/users/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
from .get_user_profile_photos import GetUserProfilePhotos
2222
from .get_users import GetUsers
2323
from .set_user_profile_photo import SetUserProfilePhoto
24+
from .update_username import UpdateUsername
2425

2526

2627
class Users(
2728
GetUserProfilePhotos,
2829
SetUserProfilePhoto,
2930
DeleteUserProfilePhotos,
3031
GetUsers,
31-
GetMe
32+
GetMe,
33+
UpdateUsername
3234
):
3335
pass
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Union
20+
21+
from pyrogram.api import functions
22+
from ...ext import BaseClient
23+
24+
25+
class UpdateUsername(BaseClient):
26+
def update_username(self,
27+
username: Union[str, None]) -> bool:
28+
"""Use this method to update your own username.
29+
30+
This method only works for users, not bots. Bot usernames must be changed via Bot Support or by recreating
31+
them from scratch using BotFather. To update a channel or supergroup username you can use
32+
:meth:`update_chat_username`.
33+
34+
Args:
35+
username (``str`` | ``None``):
36+
Username to set. "" (empty string) or None to remove the username.
37+
38+
Returns:
39+
True on success.
40+
41+
Raises:
42+
:class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
43+
"""
44+
45+
return bool(
46+
self.send(
47+
functions.account.UpdateUsername(
48+
username=username or ""
49+
)
50+
)
51+
)

0 commit comments

Comments
 (0)