Skip to content

Commit ecb5d9b

Browse files
Added tests for __html_text, fixed the bug, added custom_emoji for entities
1 parent 26575dc commit ecb5d9b

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

telebot/types.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,7 @@ def __html_text(self, text, entities):
13131313
"strikethrough": "<s>{text}</s>",
13141314
"underline": "<u>{text}</u>",
13151315
"spoiler": "<span class=\"tg-spoiler\">{text}</span>",
1316+
"custom_emoji": "<tg-emoji emoji-id=\"{custom_emoji_id}\">{text}</tg-emoji>"
13161317
}
13171318

13181319
if hasattr(self, "custom_subs"):
@@ -1321,7 +1322,7 @@ def __html_text(self, text, entities):
13211322
utf16_text = text.encode("utf-16-le")
13221323
html_text = ""
13231324

1324-
def func(upd_text, subst_type=None, url=None, user=None):
1325+
def func(upd_text, subst_type=None, url=None, user=None, custom_emoji_id=None):
13251326
upd_text = upd_text.decode("utf-16-le")
13261327
if subst_type == "text_mention":
13271328
subst_type = "text_link"
@@ -1332,30 +1333,41 @@ def func(upd_text, subst_type=None, url=None, user=None):
13321333
if not subst_type or not _subs.get(subst_type):
13331334
return upd_text
13341335
subs = _subs.get(subst_type)
1336+
if subst_type == "custom_emoji":
1337+
return subs.format(text=upd_text, custom_emoji_id=custom_emoji_id)
13351338
return subs.format(text=upd_text, url=url)
13361339

13371340
offset = 0
1341+
start_index = 0
1342+
end_index = 0
13381343
for entity in entities:
13391344
if entity.offset > offset:
1345+
# when the offset is not 0: for example, a __b__
1346+
# we need to add the text before the entity to the html_text
13401347
html_text += func(utf16_text[offset * 2 : entity.offset * 2])
13411348
offset = entity.offset
1342-
html_text += func(utf16_text[offset * 2 : (offset + entity.length) * 2], entity.type, entity.url, entity.user)
1349+
1350+
new_string = func(utf16_text[offset * 2 : (offset + entity.length) * 2], entity.type, entity.url, entity.user, entity.custom_emoji_id)
1351+
start_index = len(html_text)
1352+
html_text += new_string
13431353
offset += entity.length
1354+
end_index = len(html_text)
13441355
elif entity.offset == offset:
1345-
html_text += func(utf16_text[offset * 2 : (offset + entity.length) * 2], entity.type, entity.url, entity.user)
1356+
new_string = func(utf16_text[offset * 2 : (offset + entity.length) * 2], entity.type, entity.url, entity.user, entity.custom_emoji_id)
1357+
start_index = len(html_text)
1358+
html_text += new_string
1359+
end_index = len(html_text)
13461360
offset += entity.length
13471361
else:
13481362
# Here we are processing nested entities.
13491363
# We shouldn't update offset, because they are the same as entity before.
13501364
# And, here we are replacing previous string with a new html-rendered text(previous string is already html-rendered,
13511365
# And we don't change it).
1352-
entity_string = utf16_text[entity.offset * 2 : (entity.offset + entity.length) * 2]
1353-
formatted_string = func(entity_string, entity.type, entity.url, entity.user)
1354-
entity_string_decoded = entity_string.decode("utf-16-le")
1355-
last_occurence = html_text.rfind(entity_string_decoded)
1356-
string_length = len(entity_string_decoded)
1357-
#html_text = html_text.replace(html_text[last_occurence:last_occurence+string_length], formatted_string)
1358-
html_text = html_text[:last_occurence] + formatted_string + html_text[last_occurence+string_length:]
1366+
entity_string = html_text[start_index : end_index].encode("utf-16-le")
1367+
formatted_string = func(entity_string, entity.type, entity.url, entity.user, entity.custom_emoji_id).replace("&amp;", "&").replace("&lt;", "<").replace("&gt;",">")
1368+
html_text = html_text[:start_index] + formatted_string + html_text[end_index:]
1369+
end_index = len(html_text)
1370+
13591371
if offset * 2 < len(utf16_text):
13601372
html_text += func(utf16_text[offset * 2:])
13611373

tests/test_types.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,23 +275,26 @@ def test_message_entity():
275275
# TODO: Add support for nesting entities
276276

277277

278-
#sample_string_1 = r'{"update_id":934522126,"message":{"message_id":1374510,"from":{"id":927266710,"is_bot":false,"first_name":">_run","username":"coder2020","language_code":"en","is_premium":true},"chat":{"id":927266710,"first_name":">_run","username":"coder2020","type":"private"},"date":1682177590,"text":"b b b","entities":[{"offset":0,"length":2,"type":"bold"},{"offset":0,"length":1,"type":"italic"},{"offset":2,"length":2,"type":"bold"},{"offset":2,"length":1,"type":"italic"},{"offset":4,"length":1,"type":"bold"},{"offset":4,"length":1,"type":"italic"}]}}'
279-
#update = types.Update.de_json(sample_string_1)
280-
#message: types.Message = update.message
281-
#assert message.html_text == "<b><i>b</i> </b><b><i>b</i> </b><b><i>b</i></b>"
278+
sample_string_1 = r'{"update_id":934522126,"message":{"message_id":1374510,"from":{"id":927266710,"is_bot":false,"first_name":">_run","username":"coder2020","language_code":"en","is_premium":true},"chat":{"id":927266710,"first_name":">_run","username":"coder2020","type":"private"},"date":1682177590,"text":"b b b","entities":[{"offset":0,"length":2,"type":"bold"},{"offset":0,"length":1,"type":"italic"},{"offset":2,"length":2,"type":"bold"},{"offset":2,"length":1,"type":"italic"},{"offset":4,"length":1,"type":"bold"},{"offset":4,"length":1,"type":"italic"}]}}'
279+
update = types.Update.de_json(sample_string_1)
280+
message: types.Message = update.message
281+
assert message.html_text == "<i><b>b </b></i><i><b>b </b></i><i><b>b</b></i>"
282282

283283
sample_string_2 = r'{"update_id":934522166,"message":{"message_id":1374526,"from":{"id":927266710,"is_bot":false,"first_name":">_run","username":"coder2020","language_code":"en","is_premium":true},"chat":{"id":927266710,"first_name":">_run","username":"coder2020","type":"private"},"date":1682179716,"text":"b b b","entities":[{"offset":0,"length":1,"type":"bold"},{"offset":2,"length":1,"type":"bold"},{"offset":4,"length":1,"type":"italic"}]}}'
284284
message_2 = types.Update.de_json(sample_string_2).message
285285
assert message_2.html_text == "<b>b</b> <b>b</b> <i>b</i>"
286286

287287

288288

289-
#sample_string_3 = r'{"update_id":934522172,"message":{"message_id":1374530,"from":{"id":927266710,"is_bot":false,"first_name":">_run","username":"coder2020","language_code":"en","is_premium":true},"chat":{"id":927266710,"first_name":">_run","username":"coder2020","type":"private"},"date":1682179968,"text":"This is a bold text with a nested italic and bold text.","entities":[{"offset":10,"length":4,"type":"bold"},{"offset":27,"length":7,"type":"italic"},{"offset":34,"length":15,"type":"bold"},{"offset":34,"length":15,"type":"italic"}]}}'
290-
#message_3 = types.Update.de_json(sample_string_3).message
291-
#assert message_3.html_text == "This is a <b>bold</b> text with a <i>nested </i><b><i>italic and bold</i></b> text."
289+
sample_string_3 = r'{"update_id":934522172,"message":{"message_id":1374530,"from":{"id":927266710,"is_bot":false,"first_name":">_run","username":"coder2020","language_code":"en","is_premium":true},"chat":{"id":927266710,"first_name":">_run","username":"coder2020","type":"private"},"date":1682179968,"text":"This is a bold text with a nested italic and bold text.","entities":[{"offset":10,"length":4,"type":"bold"},{"offset":27,"length":7,"type":"italic"},{"offset":34,"length":15,"type":"bold"},{"offset":34,"length":15,"type":"italic"}]}}'
290+
message_3 = types.Update.de_json(sample_string_3).message
291+
assert message_3.html_text == "This is a <b>bold</b> text with a <i>nested </i><i><b>italic and bold</b></i> text."
292292

293293

294-
assert True
294+
sample_string_4 = r'{"update_id":934522437,"message":{"message_id":1374619,"from":{"id":927266710,"is_bot":false,"first_name":">_run","username":"coder2020","language_code":"en","is_premium":true},"chat":{"id":927266710,"first_name":">_run","username":"coder2020","type":"private"},"date":1682189507,"forward_from":{"id":927266710,"is_bot":false,"first_name":">_run","username":"coder2020","language_code":"en","is_premium":true},"forward_date":1682189124,"text":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa😋😋","entities":[{"offset":0,"length":76,"type":"bold"},{"offset":0,"length":76,"type":"italic"},{"offset":0,"length":76,"type":"underline"},{"offset":0,"length":76,"type":"strikethrough"},{"offset":76,"length":2,"type":"custom_emoji","custom_emoji_id":"5456188142006575553"},{"offset":78,"length":2,"type":"custom_emoji","custom_emoji_id":"5456188142006575553"}]}}'
295+
message_4 = types.Update.de_json(sample_string_4).message
296+
assert message_4.html_text == '<s><u><i><b>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</b></i></u></s><tg-emoji emoji-id="5456188142006575553">😋</tg-emoji><tg-emoji emoji-id="5456188142006575553">😋</tg-emoji>'
297+
295298

296299

297300

0 commit comments

Comments
 (0)