Skip to content

Commit 9338dc4

Browse files
JosXatsnoam
authored andcommitted
Added utils.helpers.effective_message_type (python-telegram-bot#826)
1 parent 063704c commit 9338dc4

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

telegram/message.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ class Message(TelegramObject):
195195

196196
_effective_attachment = _UNDEFINED
197197

198+
ATTACHMENT_TYPES = ['audio', 'game', 'document', 'photo', 'sticker', 'video', 'voice',
199+
'video_note', 'contact', 'location', 'venue', 'invoice',
200+
'successful_payment']
201+
MESSAGE_TYPES = ['text', 'new_chat_members', 'new_chat_title', 'new_chat_photo',
202+
'delete_chat_photo', 'group_chat_created', 'supergroup_chat_created',
203+
'channel_chat_created', 'migrate_to_chat_id', 'migrate_from_chat_id',
204+
'pinned_message'] + ATTACHMENT_TYPES
205+
198206
def __init__(self,
199207
message_id,
200208
from_user,
@@ -354,11 +362,9 @@ def effective_attachment(self):
354362
if self._effective_attachment is not _UNDEFINED:
355363
return self._effective_attachment
356364

357-
for i in (self.audio, self.game, self.document, self.photo, self.sticker,
358-
self.video, self.voice, self.video_note, self.contact, self.location,
359-
self.venue, self.invoice, self.successful_payment):
360-
if i:
361-
self._effective_attachment = i
365+
for i in Message.ATTACHMENT_TYPES:
366+
if getattr(self, i, None):
367+
self._effective_attachment = getattr(self, i)
362368
break
363369
else:
364370
self._effective_attachment = None

telegram/utils/helpers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,34 @@ def mention_markdown(user_id, name):
113113
"""
114114
if isinstance(user_id, int):
115115
return '[{}](tg://user?id={})'.format(escape_markdown(name), user_id)
116+
117+
118+
def effective_message_type(entity):
119+
"""
120+
Extracts the type of message as a string identifier from a :class:`telegram.Message` or a
121+
:class:`telegram.Update`.
122+
123+
Args:
124+
entity (:obj:`Update` | :obj:`Message`) The ``update`` or ``message`` to extract from
125+
126+
Returns:
127+
str: One of ``Message.MESSAGE_TYPES``
128+
129+
"""
130+
131+
# Importing on file-level yields cyclic Import Errors
132+
from telegram import Message
133+
from telegram import Update
134+
135+
if isinstance(entity, Message):
136+
message = entity
137+
elif isinstance(entity, Update):
138+
message = entity.effective_message
139+
else:
140+
raise TypeError("entity is not Message or Update (got: {})".format(type(entity)))
141+
142+
for i in Message.MESSAGE_TYPES:
143+
if getattr(message, i, None):
144+
return i
145+
146+
return None

tests/test_helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
from telegram import Update
1920

21+
from telegram import Sticker
22+
from telegram import User
23+
from telegram.message import Message
2024
from telegram.utils import helpers
2125

2226

@@ -26,3 +30,28 @@ def test_escape_markdown(self):
2630
expected_str = '\*bold\*, \_italic\_, \`code\`, \[text\_link](http://github.com/)'
2731

2832
assert expected_str == helpers.escape_markdown(test_str)
33+
34+
def test_effective_message_type(self):
35+
test_message = Message(message_id=1,
36+
from_user=None,
37+
date=None,
38+
chat=None)
39+
40+
test_message.text = 'Test'
41+
assert helpers.effective_message_type(test_message) == 'text'
42+
test_message.text = None
43+
44+
test_message.sticker = Sticker('sticker_id', 50, 50)
45+
assert helpers.effective_message_type(test_message) == 'sticker'
46+
test_message.sticker = None
47+
48+
test_message.new_chat_members = [User(55, 'new_user', False)]
49+
assert helpers.effective_message_type(test_message) == 'new_chat_members'
50+
51+
test_update = Update(1)
52+
test_message.text = 'Test'
53+
test_update.message = test_message
54+
assert helpers.effective_message_type(test_update) == 'text'
55+
56+
empty_update = Update(2)
57+
assert helpers.effective_message_type(empty_update) is None

0 commit comments

Comments
 (0)