Skip to content

Commit 840d537

Browse files
author
Pietro Albini
committed
Add the "name" computed attribute on Chat and User
This attribute comes really handy when you want to send an user/chat name in a message, because it contains the best name botogram can get: * If the chat has a just a title, this attribute contains it * If the chat has a just a first name, this attribute contains it * If the chat has both a first and last name, this attribute contains the two merged together
1 parent 107555d commit 840d537

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

botogram/objects/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class User(BaseObject, mixins.ChatMixin):
2525
"username": str,
2626
}
2727

28+
@property
29+
def name(self):
30+
"""Get the full name of the user"""
31+
result = self.first_name
32+
if self.last_name is not None:
33+
result += " "+self.last_name
34+
35+
return result
36+
2837

2938
class Chat(BaseObject, mixins.ChatMixin):
3039
"""Telegram API representation of a chat
@@ -43,6 +52,20 @@ class Chat(BaseObject, mixins.ChatMixin):
4352
"last_name": str,
4453
}
4554

55+
@property
56+
def name(self):
57+
"""Get the full name of the chat"""
58+
result = None
59+
60+
if self.title is not None:
61+
result = self.title
62+
elif self.first_name is not None:
63+
result = self.first_name
64+
if self.last_name is not None:
65+
result += " "+self.last_name
66+
67+
return result
68+
4669

4770
class PhotoSize(BaseObject, mixins.FileMixin):
4871
"""Telegram API representation of a photo size

docs/api/telegram.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ about its business.
5959

6060
*This attribute can be None if it's not provided by Telegram.*
6161

62+
.. py:attribute:: name
63+
64+
The computed name of the user. If someone has only the first name, this
65+
attribute contains it, but if someone also has a last name, this
66+
attribute contains the two merged.
67+
68+
You can't write to this attribute, but it automatically updates when you
69+
change :py:attr:`~botogram.User.first_name` or
70+
:py:attr:`~botogram.User.last_name`.
71+
72+
.. versionadded:: 0.2
73+
6274
.. py:method:: send(message, [preview=True, reply_to=None, syntax=None, extra=None])
6375
6476
Send the textual *message* to the user. You may optionally stop clients
@@ -228,6 +240,20 @@ about its business.
228240

229241
*This attribute can be None if it's not provided by Telegram.*
230242

243+
.. py:attribute:: name
244+
245+
The computed name of the chat. If this chat has a title this attribute
246+
contains it. If someone has only the first name, this attribute contains
247+
it, but if someone also has a last name, this attribute contains the two
248+
merged.
249+
250+
You can't write to this attribute, but it automatically updates when you
251+
change :py:attr:`~botogram.Chat.title`,
252+
:py:attr:`~botogram.Chat.first_name` or
253+
:py:attr:`~botogram.Chat.last_name`.
254+
255+
.. versionadded:: 0.2
256+
231257
.. py:method:: send(message, [preview=True, reply_to=None, syntax=None, extra=None])
232258
233259
Send the textual *message* to the chat. You may optionally stop clients

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ botogram 0.2
1616

1717
*Alpha release, not yet released*
1818

19+
* Added the :py:attr:`~botogram.User.name` computed attribute on the
20+
:py:class:`~botogram.User` objects
21+
* Added the :py:attr:`~botogram.Chat.name` computed attribute on the
22+
:py:class:`~botogram.Chat` objects
1923
* Renamed ``Bot.init_shared_memory`` to ``Bot.prepare_memory``
2024
* Renamed ``Component.add_shared_memory_initializer`` to
2125
``Component.add_memory_preparer``

tests/test_objects.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,32 @@ def test_photo_object():
4747
# And the items inside a list must be PhotoSize
4848
with pytest.raises(ValueError):
4949
botogram.objects.Photo([{"This": "isn't", "a": "PhotoSize"}])
50+
51+
52+
def test_user_name():
53+
# Create a dummy User object
54+
user = botogram.objects.User({"id": 123, "first_name": "John"})
55+
56+
# With only the first name
57+
assert user.name == "John"
58+
59+
# Also with a last name
60+
user.last_name = "Doe"
61+
assert user.name == "John Doe"
62+
63+
64+
def test_chat_name():
65+
# Create a dummy Chat object
66+
chat = botogram.objects.Chat({"id": 123, "type": "",
67+
"title": "Test", "first_name": "John"})
68+
69+
# With a title
70+
assert chat.name == "Test"
71+
72+
# Without a title
73+
chat.title = None
74+
assert chat.name == "John"
75+
76+
# Without a title and with a last name
77+
chat.last_name = "Doe"
78+
assert chat.name == "John Doe"

0 commit comments

Comments
 (0)